We need to simulate a process on a rooted tree. Each vertex i has a parent p_i (or -1 for root). Each vertex has a flag c_i: 1 means the vertex does not respect any of its ancestors (i.e., it does not respect its parent and all ancestors? Actually "does not respect any of its ancestors" meaning it does not respect any ancestor; but since respect is defined: if c_i=0, it respects all its ancestors; if c_i=1, it does not respect any of its ancestors. So it's a binary: either respects all ancestors (c_i=0) or respects none (c_i=1). In particular, for the root, c_i=0 (always). So a vertex i with c_i=1 is "disrespectful" to its ancestors.
Deletion condition: On each step we select a non-root vertex v such that:
-
v does not respect its parent. Since c_v = 1 indicates it doesn't respect any ancestors, it certainly doesn't respect its parent. But careful: c_v = 1 means it does not respect any ancestors, so it doesn't respect its parent. But could a vertex with c_v=0 ever not respect its parent? No, because respecting all ancestors includes parent. So condition "v does not respect its parent" is equivalent to c_v = 1.
-
And none of its children respects v. That means for every child u of v, we have that u does not respect v. Since children may have c_u = 1 or 0. But a child u respects its ancestors if c_u = 0 (respects all ancestors). If c_u = 0, then u respects all ancestors, including v (since v is an ancestor of u? Wait: In a rooted tree, ancestors of u are vertices on path from root to u, except u itself. Since v is parent of u, v is an ancestor. So if c_u = 0, u respects v. If c_u = 1, u respects no ancestors, so does not respect v. So condition "none of its children respects it" means for all children u of v, c_u = 1. (Because if any child has c_u = 0, that child respects v, breaking condition). Vacuous truth: if v has no children, then condition holds (since no child respects it).
So deletion condition: v is non-root, c_v = 1, and for all children u of v, c_u = 1. And if multiple such vertices, pick smallest index.
When we delete v, we remove it; its children become connected to its parent. That is, the parent of each child of v becomes v's parent (the original parent of v). So essentially we are splicing out v: its parent gets all of its children as new direct children.
After deletion, the tree structure changes; children's parents change. But note: The c values remain the same for each vertex (the original vertices, including those that were children of v). So after deletion, the children of v now have a new parent (the former grandparent). Their c values are unchanged. Also, the condition for future deletions might be affected: some vertices that previously didn't satisfy condition may now satisfy because children may have been removed or parent changed? But children list may change. Also the root remains the same (unless we delete it, but we never delete root because condition requires non-root). So we need to simulate deletions until no vertex meets the condition.
Given n up to 1e5, we need an efficient simulation. Possibly there is a simpler characterization: The process will delete all vertices that have c=1 and all their descendants also have c=1? Wait, condition: vertex v with c_v=1 can be deleted if all its children have c=1 (and v is not root). Then after deletion, its children become children of its parent. Those children (which have c=1) may later be deletable if they also have all children c=1, etc. So the process essentially deletes vertices in a bottom-up manner? But note that deletion can happen in some order that is not strictly bottom-up because after deletion, a child's parent changes and maybe it becomes deletable earlier? Actually condition for v depends only on its immediate children's c values and its own c value. If all children have c=1, then v is eligible. After deletion, those children become siblings of v's former parent? They become children of v's parent. So for those children, their parent changes, but that does not affect their own children's c's. They might become eligible for deletion later if all their children have c=1 and they have c=1 and are not root.
Now think about process: It resembles pruning leaves that are all "disrespectful". But note: leaves (no children) with c=1 are immediately eligible (provided they are not root). Since no children, condition holds. So such leaves will be deleted in some order (by smallest index). After deleting a leaf, it's gone. If a non-leaf v has all children c=1, and v has c=1, then v is eligible. But one might think: If v has children that are all leaves and c=1, then those children could be deleted first (since they are eligible). But the process picks the smallest index among all eligible vertices. So if both v and its child are eligible, the one with smaller index gets deleted first. After a child is deleted, v's children set changes (the child is removed). That might affect v's eligibility (if child was removed, and all remaining children have c=1, still all c=1? Actually if the child had c=1, it's gone. So v still has all children c=1? Possibly if child was the only one, then v becomes leaf? Wait, v's children after deletion: the children of the deleted child become v's children (since when you delete a vertex w, its children become connected to w's parent. If w is child of v, then when w is deleted, w's children become children of v). So that may introduce new children to v, which could have c=0, potentially making v ineligible later. However, note: w had c=1 (eligible), and all its children have c=1 (since w's eligibility required all its children c=1). So if w is deleted, its children (which all have c=1) become children of v. So v gains new children that all have c=1. So v's children set still consists entirely of vertices with c=1? Let's check: Initially, v's children may include w (c=1) and maybe others (c=1). After deleting w, w's children (c=1) become children of v. So all new children also c=1. So if v initially had all children c=1, after deletion of any child, the set of children remains all c=1 (because the children added are from a deleted vertex with all children c=1). Also children that are deleted disappear. So v's children remain all c=1 throughout, as long as we only delete vertices that are eligible (c=1 and all children c=1). Because any new children introduced to v come from eligible deletions, thus they also have c=1.
Therefore, if v is such that c_v = 1 and all its descendants (i.e., the entire subtree under v) have c=1, then eventually v will become eligible after all its descendants are deleted? But note that v might become eligible earlier if all immediate children have c=1, regardless of deeper descendants. However, if v has a child w that has c=1 but w has a child u with c=0, then w is not eligible (because w's children are not all c=1). But v's condition only cares about immediate children's c values. So v would still be eligible if all its children have c=1, even if deeper descendants have c=0? But can a child w have c=1 while having a child u with c=0? Yes. Then w is not eligible. v is eligible because all immediate children (including w) have c=1. So v could be deleted while w still exists. But after deleting v, w's parent becomes v's parent. That may affect w's future eligibility? w still has a child u with c=0, so w is not eligible. That's fine.
But is the process well-defined? The condition says "none of its children respects it" i.e., all children have c=1. So v can be deleted even if some descendant has c=0, as long as those are not direct children? Actually if a child w has c=1, but w's child u has c=0, then w respects? No, w's c=1 means w does not respect its ancestors, including v. So from v's perspective, w does not respect v, which is good. Also u is not a child of v, so doesn't matter. So v's deletion condition is satisfied.
But after deleting v, the tree structure changes: w becomes child of v's parent. That may affect the respect condition for w's parent? w still has c=1. The process continues.
We need to simulate deletions efficiently. Since n up to 1e5, we can simulate by repeatedly finding eligible vertices, deleting the smallest index one. But naive O(n^2) would be too slow. However, note that the condition only depends on c values and adjacency. We can think in terms of a set of candidates that satisfy: non-root, c=1, and all children have c=1. When we delete a vertex, we need to update the candidate status of its parent and possibly its new children (which were its grandchildren) and maybe others? Let's analyze effects:
Let v be the vertex to delete. Let parent = par(v). Let children = child(v). After deletion:
-
v is removed, so it's no longer a candidate.
-
For each child u in children: u becomes child of parent. So parent gains new children. Also u's parent changes, but that does not affect u's eligibility condition directly? u's eligibility depends on its own children (which remain unchanged) and its own c (unchanged) and that it's non-root. If parent becomes root? parent could be root? Possibly. But u may become child of root. If parent becomes root, u still non-root? Actually root is defined as the special vertex, which we never delete. So root remains. So u remains non-root as long as parent is not -1? Actually root is a vertex, so u becomes child of parent, which is a vertex, so u is non-root (unless parent is none, but root has no parent). So u remains eligible candidate if it meets its own condition: c_u=1 and all its children have c=1.
-
For parent: Its set of children changes: it loses v as a child, but gains all children u. Since v is deleted, parent's children list is updated. The eligibility condition for parent (if c_parent=1) depends on whether all its children have c=1. Before deletion, v may have had c=1 (since v is eligible, c_v=1). Also, children u all have c=1 because v's eligibility required all children have c=1. So the new children added to parent all have c=1. Also v, which had c=1, is removed. So if parent previously had all children with c=1 (including v), after deletion, it still has all children with c=1? Wait, we need to check if any other children of parent besides v might have c=0. If parent had a child w with c=0, then parent was not eligible because not all children c=1. After deletion, v (c=1) removed, but w remains (c=0). Also children u (c=1) are added. So still parent has a child w with c=0, so still not eligible. If parent had all children with c=1 (including v), then after deletion, its children are the previous children (all c=1) minus v (c=1) plus all u (c=1). So all children still c=1. So parent remains eligible if it already was, and remains ineligible if it wasn't. But could parent become eligible when it wasn't? Consider parent with c_parent=1. Initially, parent had some children, among which at least one had c=0, making it ineligible. After deletion, that child with c=0 might be removed? No, the only child removed is v, which has c=1. So the child with c=0 remains. Also new children u (c=1) are added. So parent still has a child with c=0, so no new eligibility. Could a child with c=0 be removed? Not by deleting v. So parent's eligibility status cannot change from ineligible to eligible due to deletion of v, except possibly if the only child that made it ineligible was v? But v has c=1, so that wouldn't make parent ineligible; only children with c=0 make parent ineligible. So v's c=1 doesn't harm eligibility. Therefore, parent's eligibility condition depends only on whether it has any child with c=0. Deleting v (c=1) does not remove any c=0 child. Adding u (c=1) also doesn't introduce c=0. So parent's eligibility remains unchanged. However, there is a nuance: what if parent is the root? Root is never deletable because must be non-root. So we don't care about root's eligibility. So parent's candidate status does not change.
But if parent had multiple children, some with c=0, then still has a c=0 child after deletion, so remains ineligible. So indeed parent's eligibility status does not change.
Thus, the only vertices that can change eligibility status after a deletion are those that had v as a child? Actually v's children become children of parent; they were previously grandchildren of parent. So for parent, children set changes but as argued, the condition only depends on whether any child has c=0. Since we are only deleting vertices with c=1 and all their children c=1, the added children are all c=1. The removed child v is c=1. So the set of children that have c=0 remains exactly the same (since any child with c=0 is untouched). So parent's eligibility remains unchanged.
What about the grandchildren? Their parent changes, but that does not affect their eligibility as they are not directly impacted. However, consider a vertex w that was a child of one of the u's? No effect.
So the candidate set (vertices satisfying condition) only loses the deleted vertex v. No new vertices become candidates? Wait, could some vertex become a candidate that wasn't before? Possibly if a vertex that previously had a child with c=0 loses that child? But the only deletions are vertices that have all children c=1, so they never delete a vertex that has a child with c=0. So a vertex with a child having c=0 cannot be deleted because it would not meet the condition (since that child respects it? Actually if child has c=0, then child respects its parent, so parent would have a child that respects it, violating condition "none of its children respects it". So such parent cannot be eligible. Therefore, any deletion only removes vertices with all children c=1. So any vertex that is deleted never had a child with c=0. So deletions never remove a child with c=0. So vertices that previously had a child with c=0 continue to have that child (still there). So no vertex loses a c=0 child. Therefore, no vertex can transition from having a c=0 child to having all children c=1. So eligibility cannot be gained by any vertex.
Thus, the candidate set (eligible vertices) is monotonic decreasing: once a vertex is eligible, it remains eligible until it gets deleted? Wait, can an eligible vertex become ineligible due to some change? Consider an eligible vertex v: c_v=1, all its children have c=1. If we delete some child u of v, does v remain eligible? When u is deleted, v loses a child, but gains u's children (which are all c=1 because u was eligible). So after deletion, v's children are still all c=1. So v remains eligible. Could v become ineligible due to something else? For example, if another vertex w (not child of v) gets attached as child of v? That could happen if we delete some vertex x that is a child of v's parent? Actually deletions can cause children to be reattached to parent, but they are attached to the parent of the deleted vertex. So for v to gain a new child, some vertex must be deleted whose parent is v (i.e., a child of v). That child would be eligible and have all children c=1, so the new children added are all c=1. So no problem. Could v gain a child from somewhere else? For v to get a new child, a deleted vertex's parent must be v. That would be one of its children. So again added children have c=1. So v never gets a child with c=0. So v remains eligible. Could v's own c change? No. So once a vertex satisfies the condition, it will remain eligible until it is deleted. Therefore, the process is simple: The set of vertices that ever become eligible are exactly those vertices that are non-root, have c=1, and all their children have c=1. And they stay eligible until deletion. The deletion order is then simply an order in which we delete these eligible vertices, respecting the constraint that when we delete a vertex, it must still be present (obviously). But since they remain eligible, we can delete them in any order? However, note that if we delete a parent before its child, the child becomes a child of the grandparent. But does that affect the child's eligibility? It remains eligible (if it was). But could it cause the child to become ineligible? According to earlier reasoning, no. So it seems that we could delete eligible vertices in any order and they'd remain eligible. But the problem says the order is unique, and they define the rule: at each step, choose the eligible vertex with smallest index. So the order is just sorted by index among all eligible vertices? But careful: Could there be a scenario where a vertex is eligible only after some other vertex is deleted? We argued that eligibility cannot be gained. But is that always true? Let's reexamine more carefully.
Define condition for vertex v (non-root): (c_v == 1) and for all children u in current tree, c_u == 1.
We need to check if after some deletions, a vertex that previously didn't satisfy could satisfy. That would require that initially, v had at least one child w with c_w = 0. For v to become eligible, that child w must be removed (deleted) or change its c value (impossible) or cease to be a child of v (i.e., w gets reattached to another parent). Could w cease to be a child of v? In the process, the only way a child changes parent is when its parent is deleted. So w could be reattached if v is deleted, but then v is not eligible because we consider v after deletion? Actually if v is deleted, it's gone, so not considered. So for v to become eligible while still existing, w must be removed as child via deletion of w? But if w is deleted, w is removed, so v loses that child. However, w can only be deleted if w itself is eligible. But w has c_w = 0? For w to be eligible, we need c_w=1. But w has c_w=0, so w is not eligible and cannot be deleted. Therefore, w cannot be deleted. So the child w with c_w=0 cannot be removed from the tree, and cannot change parent unless v is deleted. So as long as v exists, w remains its child with c_w=0, making v ineligible. Thus, v can never become eligible after the start if it initially has any child with c=0. So eligibility is indeed determined at the start: vertices with c=1 and all immediate children have c=1. And they stay eligible.
But is it possible that a vertex v initially has a child w with c=1 but w has a child with c=0. v's children are all c=1, so v is eligible. Later, w may be deleted because it is eligible? But w is not eligible because its child has c=0, so w cannot be deleted. So w stays. So v remains eligible. So fine.
What about a vertex v that initially has a child w with c=1, and w is eligible (i.e., all w's children c=1). v is eligible. If we delete w, v's children set changes: w is removed, w's children (c=1) become children of v. So v still has all children c=1. So v remains eligible. So no change.
Thus, the set of eligible vertices is exactly the set of vertices (non-root) with c=1 and for which all children have c=1. This set is static; vertices never leave the set (unless deleted). Therefore, the deletion process simply consists of repeatedly deleting the smallest-index vertex from this set until it's empty. Because after deleting one, the others remain eligible. However, we must ensure that after deletion, the tree's parent-child relationships might cause some new vertex to appear as a child of some other, but we argued that doesn't create new eligible vertices. Also, we must check if deletion of a vertex could cause some vertex that was previously eligible to become ineligible. That seems impossible as argued (since they only gain children with c=1). So the process is: compute all vertices that satisfy condition initially, then sort them by index? But wait, the process is defined as: at each step, pick the smallest index among the currently eligible vertices. If the set of eligible vertices is static, then the order is simply the sorted list of those vertices. But is that always true? Let's test with an example where a vertex becomes eligible only after some deletions, contrary to my reasoning.
Consider a tree: root (c=0) has children: A (c=1) and B (c=0). A has child C (c=1). B has child D (c=1). Let's compute initial eligible: For A: c_A=1, children: C (c=1) => all children c=1 => A eligible (non-root). For B: c_B=0, so not eligible (c must be 1). C: c_C=1, children: none => eligible (if non-root? parent is A, so yes). D: c_D=1, children: none => eligible. So eligible set = {A, C, D}. According to static set, we would delete them in sorted order (say IDs: 1:A, 2:B, 3:C, 4:D? Actually need to assign numbers). If we delete C first (smallest index among {A, C, D} depends on numbers). After deleting C, A's children become empty? Actually A loses C (since C deleted), but C had no children, so A's children now empty => still all children c=1 (vacuous). So A remains eligible. D remains eligible. Then delete A. After deleting A, its children (none) become children of root. So root gains no children. But D remains unchanged. So eventually delete D. So order would be C, A, D (if indices are such). That seems plausible. No new vertices become eligible.
Now consider a scenario that might break staticness: Suppose v is eligible, and has a child w that is also eligible. If we delete v before w, what happens? w becomes child of v's parent. w remains eligible. So both remain eligible after deletion? Actually after v is deleted, w is no longer child of v, but w still exists and is eligible. So eligible set after deletion: we removed v, but w remains. That's consistent: the set becomes {eligible vertices} \ {v} (since v gone). So it's still subset of initial eligible set? Not exactly, because w was in initial set, and remains. So eligible set is subset of initial set (plus maybe none added). So initial set is a superset of all vertices that will ever be eligible. But is it possible that some vertex not initially eligible becomes eligible later? Let's try to construct a counterexample.
We need a vertex v that initially has a child w with c=0, but later w ceases to be its child. That would require w's parent to change. w's parent can change only if v is deleted. But if v is deleted, then v is gone, so v can't become eligible after deletion. So v can't become eligible later. What about some other vertex u? Suppose u has a child x with c=0 initially. For u to become eligible, x must be removed as child. x could be removed only if u is deleted? Actually if x's parent changes, it's because its current parent (which is u) is deleted. Then u would be gone, so u can't become eligible later. If x itself is deleted, but x has c=0, so not eligible, cannot be deleted. So x cannot be removed. So u remains with child x (c=0) and thus never eligible. So indeed, no vertex gains eligibility.
But what about the case where a vertex initially has no children (leaf) and c=1, then it's eligible. That's fine.
Thus, the set of ever-eligible vertices is exactly the set of vertices that are non-root, c=1, and all children have c=1. And they stay eligible until deletion. So the deletion order is simply the increasing order of indices among those vertices? However, there is a nuance: The process says "if there are several such vertices, you select the one with the smallest number." If we delete them in increasing order, will we ever encounter a situation where after deleting some vertices, a vertex that was initially eligible is no longer eligible? According to reasoning, no. But let's double-check with a more complex case where a vertex's children change in a way that introduces a child with c=0? That would make it ineligible. Could that happen? Suppose v is eligible. It has children all c=1. After some deletions, could it gain a child with c=0? To gain a child with c=0, some vertex x with c=0 must become child of v. That would happen if we delete a vertex y whose parent is v, and x is a child of y, and then x becomes child of v. But if y is deleted, y must be eligible, which requires all its children have c=1. So y's children are all c=1. Therefore, x would have c=1, not 0. So v cannot gain a child with c=0. So it remains all children c=1.
Now could a vertex that is eligible become ineligible because one of its children changes its c? No, c's are fixed.
Thus, indeed, eligibility is permanent.
Therefore, the process reduces to: compute all vertices i (i != root) such that c_i == 1 and for all children j of i, c_j == 1. Then delete them in increasing order of i. But wait: Is there any ordering constraint due to the fact that when we delete a vertex, its children become attached to its parent, which might affect the "non-root" condition? For a vertex to be deletable, it must be non-root. That's true for all except root. All eligible vertices are non-root by definition. Could a vertex become root after deletions? The root never changes (we never delete root). So non-root stays non-root.
Thus, it seems we can just output the sorted list of those vertices. However, we need to be absolutely sure. Let's test with given examples.
Example 1:
n=5
1: p=3, c=1
2: p=1, c=1
3: p=-1, c=0 (root)
4: p=2, c=1
5: p=3, c=0
Let's list vertices and children:
Root = 3.
Children:
3's children: 1,5 (since p1=3, p5=3)
1's children: 2 (p2=1)
2's children: 4 (p4=2)
5's children: none
4's children: none
c: 1=1, 2=1, 3=0, 4=1, 5=0.
Now find eligible: non-root, c=1, all children c=1.
- Vertex 1: c=1, children: 2 (c=1) => all children c=1 -> eligible.
- Vertex 2: c=1, children: 4 (c=1) -> eligible.
- Vertex 4: c=1, children: none -> eligible.
- Vertex 5: c=0 -> not eligible (c=0)
- Vertex 3: root, not considered.
So eligible set = {1,2,4}. Sorted: 1,2,4. That matches output: "1 2 4". Good.
Example 2:
n=5
1: p=-1, c=0 (root)
2: p=1, c=1
3: p=1, c=1
4: p=2, c=0
5: p=3, c=0
Children:
1's children: 2,3
2's children: 4
3's children: 5
4,5 no children.
Eligibility:
- 2: c=1, children:4 (c=0) -> not all children c=1 => not eligible.
- 3: c=1, children:5 (c=0) -> not eligible.
- 4: c=0 -> not eligible.
- 5: c=0 -> not eligible.
- 1: root -> not considered.
So eligible set empty => output -1. matches.
Example 3:
n=8
Input lines:
1: p=2, c=1
2: p=-1, c=0 (root)
3: p=1, c=0
4: p=1, c=1
5: p=1, c=1
6: p=4, c=0
7: p=5, c=1
8: p=7, c=0
Let's assign vertices:
1: p2, c1
2: root c0
3: p1, c0
4: p1, c1
5: p1, c1
6: p4, c0
7: p5, c1
8: p7, c0
Children:
2: children:1
1: children:3,4,5
3: children: none
4: children:6
5: children:7
6: children: none
7: children:8
8: children: none
Now eligibility (non-root, c=1, all children c=1):
- Vertex 1: c=1? c1=1, children: 3(c0),4(c1),5(c1). Not all children c=1 because 3 is c0 -> not eligible.
- Vertex 3: c=0 -> no
- Vertex 4: c=1, children:6 (c=0) -> not eligible
- Vertex 5: c=1, children:7 (c=1) -> all children c=1? 7's c=1, so yes eligible. But we must also consider if 7's children? No, only immediate children. So vertex 5 is eligible.
- Vertex 6: c=0 -> no
- Vertex 7: c=1, children:8 (c=0) -> not eligible (since child c=0)
- Vertex 8: c=0 -> no
Also maybe others? Vertex? So eligible set = {5}. Output "5". matches example.
Thus, the simple solution seems to work for examples.
But is there any tricky case? Let's think about a scenario where a vertex is eligible but after deleting some other vertex, it becomes root? Root never changes, so can't become root unless we delete the root, which we never do. So fine.
What about vertices that are eligible but have parent that is also eligible. If we delete parent first, child becomes child of grandparent. The child remains eligible, as we argued. So no issue.
Now, is it possible that a vertex is eligible initially, but later after some deletions, it gains a child that has c=0? That would break its eligibility. Could that happen? As argued, to gain a child with c=0, a deleted vertex's parent must be our vertex, and that deleted vertex must have a child with c=0. But if that deleted vertex had a child with c=0, then the deleted vertex would not be eligible because it requires all its children c=1. So it wouldn't be deleted. Thus, no.
What if a vertex that is not eligible initially but has all children c=1 except it has a child w with c=1, but w has a child with c=0. So v's children are all c=1, so v is eligible. That's fine. Could later w become eligible? w is not eligible because its child is c=0. Could w's child with c=0 become eligible? No, c=0. Could that child be somehow removed? Not likely. So w stays. But v remains eligible. So nothing changes.
Thus, the static set approach appears correct.
But before finalizing, let's consider the definition of "respect". The statement: "if c_i = 1, then the vertex i does not respect any of its ancestors, and if c_i = 0, it respects all of them." This is consistent with our interpretation.
Condition: "none of its children respects it." That is, for every child u, u does not respect v. Since if c_u=0, u respects all ancestors, so respects v. So that condition is equivalent to no child has c_u=0, i.e., all children have c_u=1. Also we need that v does not respect its parent. That is c_v=1. So indeed condition: c_v=1 && for all children u of v, c_u=1.
Thus, the set of deletable vertices at any time is exactly those satisfying that property. And we argued it's monotonic: once a vertex satisfies it, it will always satisfy it until deletion; and vertices that don't satisfy initially will never become satisfiable.
Now we must be careful about root: root is never deletable, even if c_root=1? But root has p=-1 and c_root=0 as per problem statement. So fine.
Now, is there any scenario where a vertex that initially has all children c=1 but c_v=0? Not eligible because c must be 1.
Thus, solution: Iterate over all vertices (except root). For each vertex i, check if c_i == 1. If yes, then check all its children: if every child has c=1, then i is deletable. Collect all such i and sort them. If none, output -1; else output the list.
Complexity: O(n) to compute children and check, because total children count is n-1. So O(n).
But wait, is sorting needed? Since n up to 1e5, sorting is fine. But we could also just output in increasing order naturally if we iterate from 1 to n, collect those meeting condition, and print them in order. That's sorted by index. So we can simply iterate i=1..n, if condition holds, output i. But careful: The process might require that we delete them in that order, but is it guaranteed that if we delete them in increasing order, we never encounter a situation where a vertex that is earlier in order becomes ineligible because a deletion earlier (of a smaller index) affects it? According to reasoning, no. However, we should double-check the effect of deleting a vertex with smaller index on a vertex with larger index, to ensure that the larger index remains eligible. But if we delete vertices in any order, all eligible vertices remain eligible. So order doesn't matter for feasibility; but the problem mandates that at each step we pick the smallest index among current eligible. So the resulting sequence is exactly the sorted list of all initially eligible vertices. So we can just output them sorted (ascending). So the algorithm is trivial.
But before concluding, let's test with some custom cases that might reveal hidden complexity.
Case 1: A chain where all c=1 except root (c=0). For n=4: root=1? Actually root has p=-1, so say root=1 c=0, child 2 c=1, child3 c=1, child4 c=1. Children: 1->2, 2->3,3->4. Check eligible: vertex 2: c=1, children:3 (c=1) -> eligible. vertex3: c=1, children:4 (c=1) -> eligible. vertex4: c=1, children:none -> eligible. So eligible set {2,3,4}. Sorted ascending. If we delete in order 2,3,4: Deleting 2 first: children of 2 (3) becomes child of 1. Now tree: 1->3,3->4. Then delete 3 (still eligible? c=1, children:4 (c=1) => yes). Then delete 4. Works. If we delete 3 first: 3's child 4 becomes child of 2. Then tree: 1->2,2->4. Then delete 2: 2's child 4 becomes child of 1, then delete 4. All fine. So any order works, but smallest-index order is 2,3,4.
Case 2: Mixing with c=0 children. Suppose root c=0, child A (c=1) with child B (c=0). Also root has another child C (c=1) leaf. Then eligible: A? children: B c=0 -> not eligible. C: leaf c=1 -> eligible. So set {C}. Deleting C leaves A unchanged. Output C. That matches static set.
Case 3: Suppose we have two eligible vertices, one is ancestor of the other. Smallest index could be the ancestor. Delete ancestor first: child becomes child of grandparent. Child remains eligible. Then delete child later. So fine.
Case 4: Suppose a vertex v is eligible, but one of its children w is also eligible. If v has smaller index, we delete v first. Then w becomes child of v's parent. w's children unchanged; w still eligible (c=1, all children c=1). So then w will be deleted later. If w has smaller index, we delete w first; then v loses w as child, gains w's children (c=1). So v remains eligible. So works.
Case 5: Could there be a case where after deleting a vertex, some other vertex becomes root? No.
Thus, static set seems correct.
But wait, is it always true that a vertex that has all children c=1 initially will remain having all children c=1 regardless of deletions? We argued that the only changes to its children set are: removal of a child (if that child is deleted) and addition of the children of that deleted child (which are all c=1). So the children set after each such operation still consists only of vertices with c=1. However, what about the possibility that a child that is not deleted but whose parent changes? That would be when the child's parent (which is v) is deleted. But then v is being deleted, so v is removed, we don't care about its children set after that. For other vertices, could they gain a child that originally had c=1 but later, due to some other deletion, that child's c could change? No.
Thus, the property is invariant: For any vertex that has c=1 and all children c=1, it remains true.
Now, could there be a vertex that initially has all children c=1 but c=0? That vertex is not eligible. It could become eligible if its own c changes? No. So not.
Thus, the process is essentially identifying vertices that are "disrespectful" and whose entire immediate offspring are also disrespectful. Those can be pruned in any order, but the rule picks smallest index each time.
Hence solution: parse input, build children lists, find root. For each vertex i != root and c_i == 1, check if all children have c == 1. If yes, add i to answer list. If list empty, print -1; else print list in increasing order.
But is there any catch regarding the root's c? The problem says c_i=0 for root, guaranteed.
Edge cases: n=1. Only root, no non-root vertices. Then no deletions, output -1. That's fine.
Now check constraints: n up to 1e5, we can store children as vector of vectors.
Implementation steps:
- Read n.
- Initialize arrays p[n+1], c[n+1], and vector<vector> children(n+1).
- root = -1.
- For i from 1 to n: read p_i and c_i. If p_i == -1, root = i; else children[p_i].push_back(i). Also store c_i.
- Then iterate i=1..n, i != root:
if c[i] == 1:
bool ok = true;
for each child in children[i]:
if c[child] != 1: ok=false; break;
if ok: ans.push_back(i);
- If ans.empty(): print -1
- else: print ans elements separated by spaces.
Complexity O(n).
But we must be cautious: Are there any pitfalls regarding the definition of ancestors? The condition uses "does not respect its parent" and "none of its children respects it". We've matched to c values correctly. Double-check: If a child has c=0, it respects all ancestors, so respects the parent. So condition "none of its children respects it" indeed means no child with c=0. If a child has c=1, it doesn't respect any ancestor, so it does not respect the parent. So that matches.
Thus, solution is simple.
But wait: The problem statement for the third example says output is "5 " (with a space?) but we output "5". That's fine.
Now, is there any scenario where the static set approach fails because a vertex that initially has all children c=1 but later, due to deletion of some other vertex, gains a child that has c=1 but also has a child with c=0? That doesn't affect its own eligibility because eligibility only cares about the c of immediate children, not grandchildren. So it's fine.
But what about the condition "none of its children respects it"? That is exactly: for every child u, u does not respect v. If u has c=1, it does not respect any ancestor, so does not respect v. If u has c=0, it respects all ancestors, so respects v. So condition is exactly "all children have c=1". So it's fine.
Thus, I'm confident.
But let's double-check with a more complex scenario where after some deletions, a vertex that was initially not eligible becomes eligible because its child with c=0 gets reattached away. As argued, the only way for a child to be reattached is if its parent is deleted. So if v has a child w with c=0, to remove w from v's children, v must be deleted (since w's parent can only change if v is deleted). But then v is gone, so not eligible. So can't happen.
Thus, answer stands.
But wait: Could there be a case where v has a child w with c=0, but w itself is not a child of v after some deletions because w becomes child of another node without v being deleted? That would require that w's parent changes while v still exists. How can w's parent change? Only if the current parent (v) is deleted. So no.
Therefore, static set is correct.
Now, we should consider potential pitfalls: The input format: Each line for vertex i (1-indexed) contains p_i and c_i. For root, p_i = -1, and c_i = 0. It's guaranteed. So we must read accordingly.
Also, note that n can be up to 1e5, so use fast I/O.
Now, is there any scenario where the process never ends? If we delete vertices, eventually we'll have none eligible, and that matches our set. So it's fine.
Thus, solution is straightforward.
But wait, let's re-read the problem statement carefully: "You decided to delete vertices from the tree one by one. On each step you select such a non-root vertex that it does not respect its parent and none of its children respects it. If there are several such vertices, you select the one with the smallest number. When you delete this vertex v, all children of v become connected with the parent of v. Once there are no vertices matching the criteria for deletion, you stop the process. Print the order in which you will delete the vertices. Note that this order is unique."
They emphasize the order is unique. Our derived order (sorted indices of initial eligible vertices) is unique.
Now, I'll test a more complex random case to simulate and see if any change could happen. I'll write a quick mental simulation.
Suppose tree:
Root (1) c=0
Children: 2 (c=1), 3 (c=1)
2's children: 4 (c=1), 5 (c=0)
3's children: 6 (c=1), 7 (c=1)
4's children: none
5's children: none
6's children: 8 (c=0)
7's children: none
8's children: none
Now eligible initially:
- 2: children: 4(c=1),5(c=0) -> not eligible due to 5.
- 3: children:6(c=1),7(c=1) -> all children c=1? 6 c=1, 7 c=1 => yes, so 3 eligible.
- 4: leaf c=1 => eligible.
- 5: c=0 -> no
- 6: c=1, child:8(c=0) -> not eligible.
- 7: leaf c=1 => eligible.
- 8: c=0 -> no
Eligible set: {3,4,7}. Sorted: 3,4,7.
Now simulate according to process: Initially eligible vertices: 3,4,7. Choose smallest=3? Actually check also 4 and 7. 3 is smallest? Suppose indices: 1 root,2,3,4,5,6,7,8. So 3 is less than 4 and 7? Actually 3 < 4 < 7. So delete 3 first.
Delete 3: parent of 3 is root 1. Children of 3: 6 and 7 become children of root. So new children of root: 2,6,7. Also 3 removed.
Now tree: root children:2,6,7.
2 unchanged (children 4,5)
6 child:8
7 leaf
Now which vertices are eligible? Let's recalc:
- 2: still children 4(c=1),5(c=0) -> not eligible.
- 4: leaf c=1 -> eligible.
- 5: c=0 -> no.
- 6: c=1, child:8(c=0) -> not eligible.
- 7: leaf c=1 -> eligible.
- 8: c=0 -> no.
So eligible: {4,7}. That's subset of initial set (3 removed, 4 and 7 remain). No new ones.
Now smallest among {4,7} is 4. Delete 4: parent of 4 is 2. Children of 4: none. So after deletion, 2 loses child 4. Now tree: root children:2,6,7. 2 children:5. 5 leaf c=0. 6 child:8, etc.
Now eligible:
- 2: children:5(c=0) -> not eligible.
- 7: leaf c=1 -> eligible.
- Others unchanged.
So eligible: {7}. Delete 7: parent of 7 is root. children none. After deletion, root loses 7. No other changes. Now eligible: none. Process stops. Order: 3,4,7. That's sorted order of initial eligible {3,4,7}. Works.
Now what if we had a different index assignment: say eligible set {2,5} and 5 is child of 2. Suppose 2 and 5 are both eligible, but 5 has smaller index? Actually 2<5. So we would delete 2 first. After deletion, 5 becomes child of 2's parent. 5 remains eligible. Then delete 5. So order 2,5. Sorted order is 2,5. That's fine.
Now consider if 5 is eligible, 2 is eligible but 2 is not ancestor of 5? irrelevant.
Thus, static set works.
But wait, is there any scenario where a vertex that is eligible initially might become ineligible because after deletion of some other vertex, it gains a child that has c=0? As argued, to gain a child with c=0, a deleted vertex with a child c=0 would have to be attached. But that deleted vertex must be eligible to be deleted, which requires all its children c=1. So that can't happen.
But what about a scenario where a vertex v is eligible, and it gains a child w that has c=1 but w's children include c=0? That's fine; v's eligibility only cares about immediate children's c, not grandchildren. So w having c=1 is okay. So v remains eligible.
Thus, no loss of eligibility.
Hence, the set of vertices that are ever deletable is exactly the set we computed initially.
Now, one more nuance: The problem states: "if c_i = 1, then the vertex i does not respect any of its ancestors". That means it does not respect any ancestor, including its parent and all higher ancestors. So indeed, condition "does not respect its parent" is equivalent to c_i=1. And "none of its children respects it" is equivalent to all children have c=1.
Thus, the solution is indeed trivial. However, could there be a subtlety regarding the root? Root has no parent, but we never consider it. The condition requires non-root.
Now, let's think about the possibility of a vertex that is not initially eligible but could become eligible after its parent is deleted? No, because condition does not depend on parent; it's about children and its own c. So parent deletion doesn't affect its eligibility. But if its parent is deleted, the vertex becomes child of its grandparent. That might change its own children? No.
Thus, no.
Therefore, answer is straightforward.
But wait: The example in the problem description includes an image and they mention ancestors of vertex 6 are vertices 4 and 5, the ancestors of vertex 7 are vertices 8,3 and 5. That indicates the tree in the image. But that's just context.
Now, could there be any scenario where a vertex has c=1 but does not respect its parent? That's always true. So condition holds.
Now, what about the case of "vacuous truth": a vertex with no children automatically satisfies "none of its children respects it". So leaf with c=1 is always eligible.
Thus, the set includes all leaves with c=1.
Now, confirm with a random test using a brute force simulation for small n to see if any discrepancy arises. We can conceive a small tree and manually simulate the process exactly as per rules, comparing to the initial set. But given reasoning, it should match.
But maybe there is a twist: When we delete a vertex, its children become connected with the parent of v. This could cause the parent of v to become the new parent of those children. Now, consider a vertex w that is one of those children. Its parent changed. Could that affect w's respect for its new parent? w's c remains same. w does not respect any of its ancestors if c=1. So w will not respect its new parent (if c=1). That's fine. But could that affect the eligibility of the new parent (the original parent of v)? The new parent might have had c=1, and if w has c=0, that would affect eligibility. But w has c=1 (since v was eligible, all children have c=1). So w's c=1. So the new parent, if it had c=1, its children set may gain a child with c=1, which is fine (still all children c=1). If the new parent had c=1 but previously had a child with c=0, then it remains ineligible. So no change.
Thus, consistent.
Now, is it possible that after deletions, a vertex that was not eligible initially becomes eligible because its children that had c=0 are deleted? But children with c=0 cannot be deleted because they are not eligible. So they persist. So no.
Hence, static set stands.
Thus, solution is extremely simple. However, I'm a bit suspicious because if it's that simple, why would the problem be included in a contest? Possibly there is a catch: The condition "does not respect its parent" might be interpreted differently: c_i=1 means the vertex i does not respect any of its ancestors, but could it still respect its parent if something else? No, "does not respect any of its ancestors" means it doesn't respect any, so specifically doesn't respect parent. So it's clear.
Maybe the catch is about the definition of "respects" being directional: a vertex respects its ancestors, not its descendants. So a child respecting its parent is a thing. So condition "none of its children respects it" means for each child u, u does not respect v. Since u respects v iff c_u=0. So indeed.
Maybe there is nuance: "if c_i = 1, then the vertex i does not respect any of its ancestors" meaning it does not respect ANY of them, so it doesn't respect its parent. But what if c_i=0, it respects all of them, so respects parent. That's consistent.
Thus, all good.
But wait, there is a possibility: The process might stop earlier than deleting all initially eligible vertices if some become ineligible due to structural changes. But we argued they don't. But let's double-check a scenario where v is eligible, but after deleting some other vertex u, v gains a child that has c=1 but that child's children include c=0? That doesn't affect v's eligibility. So fine.
What about scenario where v is eligible, and after deletion of some vertex, v's parent changes? That would happen only if v's parent is deleted. But if v's parent is deleted, v becomes child of its grandparent. v's own children unchanged. Does v remain eligible? Eligibility condition for v does not involve its parent. So it still is eligible (c_v=1, all children c=1). So v remains eligible even if its parent is deleted. So no problem.
Thus, indeed, the set of eligible vertices is monotonically decreasing (only removed when deleted), never increases.
Therefore, the process just deletes all initially eligible vertices in increasing order.
Thus, answer is straightforward.
Now, what about the root's c? Could it be 1? The problem says "also, c_i=0 for the root". So guaranteed.
Implementation: Read n. For i from 1 to n, read p_i and c_i. If p_i == -1, root = i, and we might ignore c_i (should be 0). Then add i as child of p_i if not -1. Then after building children, we compute answer.
Potential pitfalls: Input might have extra spaces, but that's fine.
Now, we should also consider memory: children list as vector children[n+1] is fine.
Edge Cases: n=1, no deletions, output -1.
Now, let's confirm with another test: Suppose tree:
Root 1 c=0
2 c=1 (child of 1)
3 c=1 (child of 2)
4 c=1 (child of 3)
All c=1. So eligible: 2,3,4. Output 2 3 4. Simulate: delete 2 first -> child 3 becomes child of 1. Then tree: 1->3,4 (since 3's child 4). Eligible: 3? c=1, children:4 (c=1) eligible. 4 leaf eligible. Delete 3 (smallest? after deletion of 2, candidates:3,4. 3<4, delete 3). After deletion, 4 becomes child of 1. Then delete 4. Order 2,3,4. Sorted order.
If we had indices: 4,2,3? Actually the set {2,3,4} sorted is 2,3,4. So matches.
Now suppose some vertices with c=1 but not all children c=1, like a node with one child c=0. That node never becomes eligible. Its child with c=0 may have children with c=1, but that doesn't affect.
Thus, solution stands.
However, before finalizing, I'll read the problem statement once more to ensure no misinterpretation: "if c_i = 1, then the vertex i does not respect any of its ancestors, and if c_i = 0, it respects all of them." So respect is defined relative to ancestors. So a vertex's respect for its parent is determined solely by its own c. Good.
Now, "You decided to delete vertices from the tree one by one. On each step you select such a non-root vertex that it does not respect its parent and none of its children respects it." So the vertex must not respect its parent (so c_v=1) and for every child u, u does not respect v (so c_u=1). Yes.
Then, after deletion, children of v become connected with the parent of v. That is, they become direct children of v's parent.
Thus, our reasoning is correct.
Now, there is a nuance: The phrase "none of its children respects it" means that there is no child that respects it. So if there is no child, it's vacuously true. So leaf with c=1 works.
Thus, answer set is exactly vertices i (i != root) with c_i=1 and for all children j of i, c_j=1.
Now, I'm thinking about the possibility that the process may delete vertices in a different order than sorted indices if some vertices become eligible later. But we argued they don't. But let's test a scenario that might break monotonicity.
Consider a vertex v with c_v=1 and all children have c=1 except that one child w has c=1 but w has children? That's fine. So v eligible.
Now, suppose v has a child w that is not eligible because w has a child x with c=0. So w's children: x with c=0. So w not eligible. v eligible.
Now, can w become eligible after some deletions? For w to become eligible, we need all its children have c=1. Its only child x has c=0. To make w eligible, x must be removed or change c. x cannot be deleted because c_x=0 (not eligible). Could x become not a child of w? Only if w is deleted, but then w is gone. So w cannot become eligible. So no.
Thus, no new eligible.
What about a vertex that initially has a child with c=1, but that child later gets deleted, and after that, the vertex's children become all c=1? But if that child had c=1, it could be deleted only if it is eligible, meaning all its children have c=1. After deletion, its children (all c=1) become children of the vertex. So the vertex's children set now: it loses the child (c=1) but gains its grandchildren (c=1). So still all children c=1. So if the vertex had some other child with c=0, that child remains, so not all children c=1. So still not eligible. If the vertex initially had all children c=1 except maybe one child with c=0? That child cannot be deleted, so vertex never becomes eligible.
Thus, monotonic.
Now, maybe there is a scenario where a vertex becomes eligible because its only child with c=0 is not actually its child after some deletion because the child becomes child of another node? That would require the vertex to be deleted. So no.
Thus, it's safe.
Given the simplicity, perhaps the intended solution is indeed O(n) with a single pass. I recall a Codeforces problem similar to this: "Deletion of vertices" maybe problem "D. Tree Elimination" or something? Actually this looks like Codeforces Round #... It might be problem "C. Tree Cutting" or "D. Tree". I recall a problem "C. Strange Game On Matrix"? Not sure. But I think there is a known problem: "Deleting Vertices" where you delete leaves that disrespect etc. Possibly the solution is that you output all vertices with c=1 that have no child with c=0. I've seen similar.
But I'm slightly uneasy because the problem statement includes images and appears to be from Codeforces: "Deletion of vertices" maybe problem 1100E? Actually I recall a problem "D. Treelabeling"? Not that.
Nevertheless, I'm confident.
Now, one more thought: Could the process delete a vertex that is not in the initial eligible set because after some deletions, its children's c values change? The c values are fixed, so no. So fine.
Thus, I'll implement as described.
But let's double-check the input format: It says "The next n lines describe the tree: the i-th line contains two integers p_i and c_i". So we need to read lines in order from vertex 1 to n. Each line corresponds to vertex i. So p_i is given for vertex i, not that i is child of p_i. So yes, parent of i is p_i. So we can build children by for each i, if p_i != -1, children[p_i].push_back(i). That's what we did.
Now, output format: If at least one vertex to delete, print the only line containing the indices in order. Otherwise print -1.
In examples, they have trailing spaces after last number? In first output: "1 2 4 " with a trailing space. Usually that's acceptable. We'll print without trailing space or with? Usually in Codeforces, printing with spaces between numbers and maybe a space at end is fine. But better to print without trailing space. We'll join by spaces.
Now, implementation details:
Pseudo:
int main() {
int n;
cin >> n;
vector parent(n+1);
vector c(n+1);
vector<vector> children(n+1);
int root = -1;
for (int i = 1; i <= n; ++i) {
int p, ci;
cin >> p >> ci;
parent[i] = p;
c[i] = ci;
if (p == -1) {
root = i;
} else {
children[p].push_back(i);
}
}
vector ans;
for (int i = 1; i <= n; ++i) {
if (i == root) continue;
if (c[i] == 1) {
bool ok = true;
for (int child : children[i]) {
if (c[child] != 1) {
ok = false;
break;
}
}
if (ok) {
ans.push_back(i);
}
}
}
if (ans.empty()) {
cout << -1 << endl;
} else {
for (int i = 0; i < ans.size(); ++i) {
if (i) cout << " ";
cout << ans[i];
}
cout << endl;
}
return 0;
}
Now, check time: O(n) with sum of children O(n). So fine.
But could there be a case where the root has c=1? Problem says c_i=0 for root, so ignore.
Now, is there any possibility that the process might delete vertices in a different order if two vertices have the same index? No duplicates.
Thus, finalize.
But I'll do one more mental test with a more complex tree to ensure no hidden dynamic.
Consider n=6:
Root=1 c=0
2 c=1 child of 1
3 c=1 child of 1
4 c=1 child of 2
5 c=0 child of 2
6 c=1 child of 3
Also maybe other.
Children: 1: {2,3}
2: {4,5}
3: {6}
4: {}
5: {}
6: {}
c: 2=1,3=1,4=1,5=0,6=1.
Eligible:
- 2: children:4(c=1),5(c=0) -> not eligible
- 3: children:6(c=1) -> eligible
- 4: leaf c=1 -> eligible
- 5: c=0 -> no
- 6: leaf c=1 -> eligible
Set = {3,4,6}. Sorted = 3,4,6.
Simulate: eligible initially: {3,4,6}. Delete 3 (smallest? 3 < 4 < 6). Delete 3: parent of 3 is 1, children {6} become child of 1. So tree: 1 children: {2,6}; 2 children: {4,5}; 6 leaf.
Now eligible: 4 (still leaf) yes; 6 leaf yes; 2? children 4(c=1),5(c=0) -> not. So {4,6}. Delete 4 (smallest? 4<6). Delete 4: parent of 4 is 2, 4 has no children, so 2 loses child 4. Now tree: 1 children: {2,6}; 2 children: {5}; 6 leaf.
Eligible: 6 leaf; 2? children:5(c=0) -> no. So {6}. Delete 6. Done. Order: 3,4,6 matches sorted.
Now if indices were such that 4<3<6, initial set {4,3,6} sorted = 3,4,6 again. But if 4 is less than 3, then the set in ascending order is 4,3,6? Actually if indices: vertex 2=2,3=4? Wait indices are fixed numbers 1..n. Let's assign indices as numbers: Suppose vertices: 1 root, 2 is vertex 2, 3 is vertex 3, 4 is vertex 4, 5 is vertex 5, 6 is vertex 6. So indices are as given. So 3 is index 3, 4 is index 4, 6 is index 6. So sorted is 3,4,6. So consistent.
Now, consider a scenario where a vertex is eligible but its child is also eligible and has smaller index. Then sorted order will have child first. That's okay.
Now, what about a vertex that is eligible but its parent is also eligible and parent has smaller index; then parent gets deleted first. After deletion, child becomes child of grandparent. Then later child gets deleted. That matches sorted order.
Now, is there any possibility that after deleting parent, child becomes attached to a vertex that is its ancestor, and maybe some condition about respecting ancestors changes? The child's c=1 means it doesn't respect any ancestor, including the new parent. So it still doesn't respect its new parent. That's fine.
Thus, the process is as described.
Thus, solution is indeed O(n). I'll produce final answer.
But wait, I recall a Codeforces problem "C. Tree Generator?" Not exactly.
Maybe there is a nuance: When we delete a vertex, its children become connected with the parent of v. This could cause the parent to have new children that might have been previously its descendants. But that doesn't affect eligibility.
Now, one more check: Could a vertex become eligible after deletion because its child with c=0 gets reattached away due to the child's parent (which is not v) being deleted? Consider v has child w (c=0). The only way w ceases to be a child of v is if v is deleted. So if v is not deleted, w stays. So v cannot lose that child. So v cannot become eligible.
Thus, final.
Now, I will provide the solution explanation and code.
But before finalizing, let's also think about the possibility that the root could be considered as having no parent, and the condition "does not respect its parent" is not defined for root. So we don't consider root.
Now, what about vertices that become root after deletions? The root is fixed? The problem says "rooted tree" and we never delete root. So root remains same.
Thus, all good.
Given the reasoning, I'll write the final solution.
But wait, I see a potential subtlety: The phrase "none of its children respects it" might be interpreted as "there is no child that respects it". But what about the case where a child has c=0 (respects ancestors) but because of some earlier deletions, the child's ancestor set changes such that v is no longer an ancestor? That's impossible: if v is the parent of the child, then v is always an ancestor of that child, regardless of tree modifications? Actually, after deletion of some nodes, the parent-child relationship changes. Consider child u whose original parent was v. After some deletions, u might become child of another node, but v is no longer its parent. In the context of checking condition for v at a given moment, we consider current children of v. So if u is no longer child of v, it doesn't count. So the condition "none of its children respects it" is evaluated with respect to current children. That's what we did. So when we argue about v gaining a child with c=0, that could make v ineligible, but we argued it can't happen. But could v lose a child with c=0? Only if that child is no longer its child, which would require v to be deleted or the child's parent to change. To change child's parent, v must be deleted. So v cannot lose a child with c=0 while still existing. So if v has a child with c=0, it remains its child forever, making v permanently ineligible. So indeed.
Thus, our reasoning stands.
Now, one more nuance: Could a vertex that initially has all children c=1 but c=0? That's not eligible. Could it later become c=1? No. So not.
Thus, solution is final.
Now, I'll write answer in final channel. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> parent(n + 1), c(n + 1);
vector<vector<int>> children(n + 1);
int root = -1;
for (int i = 1; i <= n; ++i) {
int p, ci;
cin >> p >> ci;
parent[i] = p;
c[i] = ci;
if (p == -1) {
root = i;
} else {
children[p].push_back(i);
}
}
vector<int> ans;
for (int i = 1; i <= n; ++i) {
if (i == root) continue;
if (c[i] == 1) {
bool ok = true;
for (int child : children[i]) {
if (c[child] != 1) {
ok = false;
break;
}
}
if (ok) {
ans.push_back(i);
}
}
}
if (ans.empty()) {
cout << -1 << "\n";
} else {
for (size_t j = 0; j < ans.size(); ++j) {
if (j) cout << " ";
cout << ans[j];
}
cout << "\n";
}
return 0;
}