We need to solve problem: Given a tree (n vertices, edges given by parent list? Actually input: second line contains n integers p1...pn. If pi != 0, there is an edge between i and pi. So it's like each node i (1-indexed) has a parent pi? Actually it says "If pi ≠ 0 there is an edge between vertices i and pi." So it's an undirected tree? But typical representation: a rooted tree with root indicated by pi=0. But note that edges are undirected; pi indicates an edge to pi, but if we treat pi as parent, then it's a directed parent relationship, but the tree is still undirected; but input ensures it forms a tree (connected, n-1 edges). So we have a tree.
We need to destroy vertices one by one. A vertex can be destroyed if its degree (number of adjacent edges currently present) is even. When we destroy a vertex, we remove it and all edges incident to it (so its neighbors lose that edge, decreasing their degree by 1). We need to destroy all vertices (i.e., remove all vertices from the tree), printing order if possible.
This is a known problem: "Destroying the tree" / "Even degrees" maybe from Codeforces (Problem 963B? Actually there is a problem "Destroying The Tree" but with different rules? Let me recall: there is Codeforces Round #... Actually this is Codeforces problem 964B? No, it's "Destroying Array"? No.
Wait, the problem: "Destroy it!"? Not sure.
We need to determine if it's possible to delete all vertices by repeatedly deleting a vertex of even degree.
Observations: The degree parity matters. Initially, the degrees of vertices in a tree: sum of degrees = 2*(n-1). So total degree sum is even, but individual degrees can be odd or even.
When we delete a vertex v with even degree d (even), we remove d edges. For each neighbor u of v, its degree decreases by 1, so parity flips (since subtracting 1 flips parity). So the parity of neighbors toggles. The vertex v is removed, so its degree no longer matters.
We need to see if there is a sequence that empties the tree.
This is reminiscent of a problem about "leaf removal" but with parity condition. Another viewpoint: Consider the tree as rooted arbitrarily. We can try to decide if we can delete leaves? But leaf has degree 1 (odd) so cannot be deleted unless its degree becomes even. So leaf can be deleted only after its neighbor loses some edges making leaf's degree become 0? Actually leaf's degree is 1 (edge to its parent). To become even, degree must become 0 (even) or 2 (even) but since leaf only has one neighbor, can't gain edges; only can lose its only edge when its neighbor is deleted? Wait, when we delete a vertex, we remove all edges incident to it. If we delete a leaf's neighbor, the leaf loses its only edge and becomes isolated with degree 0 (even), then we can delete the leaf. So typical strategy: delete internal nodes first to make leaves isolated.
But condition is even degree at time of deletion.
This is similar to the concept of "Elimination game" with parity constraints.
I recall a known result: For a tree, it is possible to delete all vertices if and only if the number of vertices with odd degree is even? Let's test: For n=1, tree with one vertex and no edges: degree 0 (even). So can delete. For n=2: two vertices connected by edge: degrees 1 and 1 (both odd). Is it possible? Let's try: Initially, both have degree 1 (odd). Cannot delete any. So impossible. So n=2 impossible. So condition cannot be simply "number of odd-degree vertices is even" because in a tree with 2 vertices, odd count =2 even but impossible. So need more.
Let's try small trees:
n=3: a path of length 2 (vertices 1-2-3). Degrees: 1,2,1. Vertex 2 degree 2 even, can delete first. Delete 2 -> edges (1-2) and (2-3) removed, left vertices 1 and 3 isolated each degree 0 (even). Then delete 1,3 in any order. So possible. So path of 3 possible.
n=2: path of length 1: degrees 1,1 -> impossible.
n=4: chain 1-2-3-4: degrees:1,2,2,1? Actually edges: 1-2,2-3,3-4 -> degrees:1,2,2,1. Vertex 2 degree 2 even; vertex 3 degree 2 even. Can we delete? If delete 2 first, then edges (1-2) and (2-3) removed. Remaining vertices: 1 (degree 0), 3-4 edge still (3-4) because 3 still connected to 4? Actually after removing 2, edges incident to 2 removed: that includes (2-3). So that edge is gone. So left graph: vertex1 isolated, vertex3 connected to vertex4 via edge (3-4). So degrees:1:0,3:1,4:1. Now degrees odd for 3 and4. Can't delete any (0 is even, so vertex1 can be deleted? Wait vertex1 degree 0 even, can delete vertex1! Actually after removal of 2, vertex1 becomes isolated with degree 0 even, so we can delete vertex1. That doesn't affect others? Delete vertex1: since degree 0, no edges to remove. Then remaining vertices 3 and4 still connected with degree 1 each. Now degrees 1 each (odd). No vertex with even degree. So stuck. So sequence fails. What if we delete vertex3 first? Symmetric, similar outcome? Possibly we could delete vertex2 then vertex3? But after deleting vertex2, we could delete vertex3? Let's simulate: start: 1-2-3-4. Delete vertex2: as before, left vertices 1 (0),3-4 (edge). Now we have vertex1 (0 even) and vertices 3,4 (both odd). Could we delete vertex3? Vertex3 degree 1 (odd) -> can't. Vertex4 degree 1 -> can't. So only vertex1 can be deleted. Delete vertex1 -> remaining 3-4 edge (both odd). Stuck. So this sequence fails.
What if we delete vertex2 then vertex4? vertex4 still odd, can't. So fails.
Maybe another order: Delete vertex2 and then vertex? Could we delete vertex3 after vertex1? Still odd. So fails. So chain of 4 seems impossible. Check if any sequence works? Could we delete vertex2 then vertex? Perhaps if we delete vertex2, then vertex3's degree becomes 1 (odd), not even. So cannot delete. So chain of 4 impossible. That matches example 2: Input 4, tree is a chain? Actually example 2 input: "4 \n0 1 2 3". That means: vertex1: p1=0 (maybe root?), vertex2: p2=1 => edge 2-1, vertex3: p3=2 => edge 3-2, vertex4: p4=3 => edge 4-3. So it's a chain 1-2-3-4. Output NO. So indeed chain of 4 is impossible.
Now chain of 5: 1-2-3-4-5. Degrees:1,2,2,2,1. Vertex2,3,4 even. Could we? Let's attempt: maybe delete vertex3 first. Delete 3 => edges (2-3) and (3-4) removed, left components: 1-2 still connected, 4-5 still connected. Graph: component A: vertices1-2 edge, degrees:1:1,2:1; component B: vertices4-5 edge, degrees:4:1,5:1. Both odd degrees, stuck? But also vertex? there are isolated? No. So after deleting 3, we have two edges each with odd-degree vertices; no even-degree vertex except maybe if a vertex becomes isolated? Not here. So that fails. Maybe delete vertex2 first: delete 2 removes edges (1-2),(2-3). Left: vertex1 isolated (0), and chain 3-4-5 with edges (3-4),(4-5) -> degrees:3:1,4:2,5:1. Now even-degree vertices: vertex1 (0), vertex4 (2). We can delete vertex1 (doesn't affect) or vertex4. Delete vertex4: remove edges (3-4),(4-5). Left vertices: 1 (already removed? Wait we haven't removed vertex1 yet; we could delete vertex1 or vertex4 in any order). Suppose after deleting vertex2, we have vertices 1,3,4,5. Delete vertex4: removes edges (3-4) and (4-5). Then left vertices: 1 (0), 3 (0? Actually after removal, 3 loses its only edge, becomes isolated degree 0), 5 (isolated degree 0). So then we have 1,3,5 all degree 0 even, can delete in any order. So sequence works: 2,4, then 1,3,5. Or 2,1,4,3,5? Let's test: after deleting vertex2, we could delete vertex1 (0) then delete vertex4 (2) then delete 3 and 5. So yes possible. So chain of 5 works.
Thus pattern: For a path of length n, possibility depends on n mod? n=2 NO, n=3 YES, n=4 NO, n=5 YES. So maybe n odd works? n=1 odd YES, n=3 YES, n=5 YES. n=2 even NO, n=4 even NO. But what about n=6? Let's test quickly: chain 1-2-3-4-5-6. Degrees:1,2,2,2,2,1. Even-degree vertices:2,3,4,5. We need to see if there's a sequence. Intuition: Maybe only odd number of vertices possible? But wait, there might be other trees where even n works. For instance, a star with 3 leaves: center degree 3 (odd), leaves degree 1 (odd) -> all odd, no even? Actually star with 4 vertices: center connected to 3 leaves: degrees: center 3 (odd), leaves each 1 (odd). So all odd, no even vertex initially. So impossible? But maybe we can delete something later? Since initially no even, cannot start. So star with 4 is impossible. What about star with 5 vertices? center degree 4 (even), leaves degree 1 (odd). So we can delete center first (degree 4 even). Then leaves become isolated degree 0 even, then delete leaves. So star with 5 works (n=5 odd). Star with 4 (n=4 even) impossible. So again odd n works? But there could be trees with even n that work if they have some even-degree vertex initially? For even n, maybe still possible? Let's test a tree that is not a simple chain but with even number of vertices, e.g., two stars joined? Consider tree with 6 vertices: suppose it's like a path of length 2 plus two leaves attached to middle? Actually let's design: vertex 1 connected to 2, vertex2 connected to 3 and also to 4 and 5? Let's do: root 2 with children 1,3,4,5? That's 5 vertices. For 6, add another leaf to somewhere. But we need to test systematically.
Maybe the necessary and sufficient condition is that the number of vertices with odd degree is even? But we saw chain of 2 has 2 odd (even) but impossible. So not that. Another known problem: "Delete nodes with even degree" is equivalent to existence of a "parity" assignment? Might be related to the concept of "Eulerian subgraph"? There's a known problem: Given a tree, can we partition edges into paths? Not sure.
Let's reframe: Operation: delete a vertex of even degree, removing all incident edges. This is like in graph theory, a "destruction sequence" where each vertex is removed when its current degree is even. This is reminiscent of the "parity game" where we can think of a bottom-up strategy: For a tree, we can root it and decide which vertices to delete before their children, etc. Possibly we can always destroy all vertices if and only if n is odd? Let's test: n=1 (odd) yes, n=3 yes, n=5 yes, n=2 no, n=4 no. What about n=6? We need to find a counterexample where n=6 works, if any.
Let's attempt to construct a tree with 6 vertices that might work. Start with even degree vertices initially? For n=6, sum degrees = 2*(6-1)=10, which is even. Number of odd-degree vertices must be even (by handshaking lemma). So possible odd count: 0,2,4,6. To have any even-degree vertex initially, we need at least one vertex with even degree. But in chain of 6, we have even-degree vertices (2,3,4,5) so initial moves possible. Yet we suspect chain 6 may be impossible? Let's test chain of 6 manually.
Chain: vertices 1-2-3-4-5-6.
Degrees:1:1,2:2,3:2,4:2,5:2,6:1.
We need a sequence. Let's try to find one via search? Let's simulate systematically.
We can think of the process as: we can delete any vertex with even degree. When we delete, neighbors' degree parity toggles.
This is like a parity removal game. There might be an invariant.
Consider labeling each vertex with 0/1 representing something like "need to be removed after parent" etc. Alternatively, think of the graph as rooted. There is known Codeforces problem "Destroying The Tree" but that one is about something else.
I recall a problem: "B. Odd-Even Subsequence" not.
Maybe it's Codeforces Round #453 (Div. 2) problem D? Actually there is a problem "D. Destroy the Tree" from Codeforces Round #... No.
Search memory: There is a problem "C. Destroying Array" not.
Another similar: "A. Even Odds" no.
Maybe it's "Problem 1023E" no.
Wait, I think I've seen this: "CF 274B" no.
Let's think from perspective of linear algebra over GF(2). Represent state by parity of degrees? Actually degree parity changes when neighbors are removed. Operation of deleting a vertex v (with even degree) toggles parity of all neighbors. This resembles a process where we have a binary vector indicating whether a vertex is present? Or maybe we can think of the process as we need to delete all vertices, and each deletion flips parity of neighbors. We can choose order. This is similar to the "Lights Out" game on a graph, but here the condition to delete a vertex is that its current degree (the number of remaining neighbors) is even. That is like the vertex's parity must be 0 before pressing it? Actually in Lights Out, you press a button and it toggles itself and neighbors. Here, pressing (deleting) a vertex v removes it and toggles neighbor's degree parity. But the condition to press is that the vertex's degree (number of incident edges) is even. This is reminiscent of the "chip-firing" or "elimination" but not exactly.
Alternate viewpoint: Consider the tree as rooted. For each vertex, think about the subtree. The requirement for being able to delete the root eventually might depend on subtree sizes parity. Perhaps we can do a DFS and decide for each vertex whether its subtree can be completely removed, and if so, whether the vertex can be removed before/after its children. This resembles a known solution: we need to output order via DFS, removing a vertex only after its children of certain parity are removed. I've seen a problem: "Delete a vertex of even degree" is similar to "removing vertices with even degree" and it's always possible if n is odd, and we can construct order via post-order traversal where we remove children first, then parent if its current degree (which is number of children not yet removed) is even. That suggests a recursive algorithm: For a rooted tree, process each child subtree. After processing a child, we may have removed the child or not? Actually when we delete a vertex, it's gone, so its parent loses that edge. So if we process children recursively, we can aim to delete the child before its parent, leaving the parent with fewer children (edges) as we go.
Let's attempt to design algorithm: Root the tree arbitrarily (say at node 1). For each node, we want to determine if its subtree can be completely cleared (i.e., all nodes in subtree removed) with the condition that when we finish, the node itself might or might not be removed yet. But the goal is to remove all nodes overall. We can try to remove nodes in a post-order fashion: For a node, we first try to remove all its children subtrees completely, which would involve deleting the children nodes themselves (since they are in the subtrees). After processing all children, the node will be connected only to its parent (if any) and maybe some children that were not removed? Actually if we completely remove a child subtree, that means all nodes in that subtree, including the child, have been deleted. So the edge to that child is gone because the child was deleted. So after processing all children, all children are gone, so the node becomes either isolated (if no parent) or only connected to its parent. Then its degree will be either 0 (if root) or 1 (if not root). If degree is 1 (odd), we cannot delete it yet. So we need some alternative: maybe we need to delete the node before some children? Because after deleting children, node becomes leaf (degree 1) which is odd, cannot delete. So we might need to delete the node before some of its children, to use its even degree initially? Let's consider a leaf: leaf degree 1 (odd) initially, cannot be deleted until its parent is gone (making it isolated degree 0). So leaf can only be deleted after its parent is deleted. So for any non-root node, its parent must be deleted before it (or at least before it becomes leaf with odd degree?). Actually leaf can be deleted after parent is gone, because then its degree becomes 0 (even). Similarly, a node with children: if we delete the node before its children, then children become isolated or connected among themselves? When we delete a node, all edges to its children are removed, so each child loses one edge; if a child had degree 1 (only connection to parent) then it becomes isolated (degree 0) and can be deleted later. If a child had degree >1 (i.e., it has its own children), then after removing parent edge, its degree decreases by 1, so parity flips. So we have options.
Thus perhaps we can adopt a strategy: For each node, we try to arrange deletion order within its subtree such that after processing, the node is deleted (or maybe not) and the rest of the tree is unaffected except that the edge to its parent might be still present or not. However, the overall process must eventually delete the root without leaving any nodes. So we need to produce a sequence that deletes all nodes.
Let's try to derive necessary and sufficient condition.
Observation: When we delete a vertex, we remove it and all incident edges. This is equivalent to removing the vertex and effectively toggling the parity of its neighbors. So the process can be modeled as: we have a graph with vertices; each vertex has a state: present or removed. Initially all present. At each step, we choose a present vertex v whose current degree (number of present neighbors) is even, and then we remove v (set it to removed) and also remove all edges incident to v, which means that for each neighbor u of v that is still present, its degree decreases by 1 (parity flips). We want to remove all vertices.
If we think of the removal order as a permutation of vertices such that when we consider vertices in that order, each vertex at its turn has even number of neighbors among the vertices that are still present (i.e., not yet removed). This is reminiscent of the concept of "elimination order by even degree" which is known as "sequence where each vertex is simplicial?" Not exactly.
We can also think of the complementary process: adding vertices in reverse order. If we reverse the deletion sequence, we start with an empty graph, and add vertices in reverse order along with their incident edges to already added vertices. The condition that at deletion time v had even number of neighbors among still-present vertices translates to: when we add v in reverse, it must be added to an even number of already present vertices (since those were its neighbors at deletion time). Because in reverse, we start with no vertices, then add vertices in reverse order; when we add a vertex, we also restore its edges to vertices that are already added (since those were its neighbors that were present at deletion time). So the condition becomes: In the reverse order, each vertex when added must have an even number of neighbors among the vertices already added.
Thus we need to find an ordering of vertices (the reverse of deletion) such that for each vertex v, the number of neighbors of v that appear earlier in the ordering is even. This is equivalent to finding an ordering where each vertex has an even number of neighbors before it. That's a more tractable condition: we need a permutation π of vertices 1..n such that for every vertex v, the count of neighbors u with π(u) < π(v) is even.
This is exactly the concept of "odd-even ordering" or "parity-friendly ordering". This is like a linear layout with parity constraints.
Now the problem reduces to: Does there exist an ordering of vertices of a tree such that each vertex has an even number of neighbors that come before it? If yes, then we can destroy all vertices, and the reverse of that order is a valid destruction sequence. Conversely, if a destruction sequence exists, reversing it gives such an ordering.
So we need to determine existence and construct such an ordering.
Now we have a tree (undirected, acyclic). The condition is a combinatorial property. Let's analyze.
Let’s denote for each vertex v, the parity of its "predecessor neighbor count". We need it even. So we need to assign each vertex a position in a sequence. This is similar to labeling vertices with 0/1 indicating whether they are placed in odd or even positions? Not exactly.
We can think of building the sequence incrementally from the end? Actually the reverse order construction: we add vertices one by one, each new vertex must connect to an even number of already added vertices. Starting from empty set, we can add vertices arbitrarily as long as the condition holds.
In a tree, each edge connects two vertices. The condition implies something about the parity of the number of times each edge contributes to the "before" relation.
Consider the final ordering. For each edge (u,v), exactly one of u or v comes before the other. That edge contributes 1 to the predecessor count of the later vertex (the one that comes later). So the predecessor count of a vertex v is the number of incident edges where the other endpoint appears before v. Since we need this count to be even for each v, that means for each vertex v, the number of incident edges oriented towards v (i.e., from earlier to later) is even. So if we orient each edge from the earlier vertex to the later vertex, we obtain an orientation of the tree edges. The condition is that each vertex has even indegree (or even outdegree? Let's define: if we orient from earlier to later, then the later vertex receives an incoming edge. The number of neighbors before v equals indegree of v. So condition: indegree(v) is even for all vertices.
Thus we need an acyclic orientation (since it's a total order, orientation is acyclic) of the tree such that every vertex has even indegree. Since it's a tree, edges = n-1. Also sum of indegrees = n-1 (since each edge contributes exactly one indegree to one endpoint). So sum of indegrees = n-1. If each indegree is even, then sum of indegrees is even, so n-1 must be even => n is odd. So a necessary condition is that n is odd.
Indeed, sum of even numbers is even, so n-1 even => n odd. This matches our observations: n=1,3,5 possible; n=2,4 impossible. So necessary condition: n is odd.
Is it also sufficient for trees? For a tree with odd number of vertices, does there always exist a total order (i.e., a linear extension) such that each vertex has even indegree? Let's test for some trees. For star with 4 leaves (n=5) works? n=5 odd, we already found star works. For a more complex tree, say a perfect binary tree with 7 nodes (odd). Likely works. For any tree with odd n, can we always find such an ordering? This is reminiscent of the concept of "parity orientation" or "even indegree orientation" with acyclic constraint? But note that the orientation must be acyclic because it comes from a total order. However, maybe we can relax: if we can find any orientation of edges (not necessarily acyclic) such that each vertex has even indegree? But we need an orientation derived from a total order (i.e., no cycles). However, if we can assign directions to edges such that each vertex indegree even, and also the orientation is acyclic? Actually any orientation of a tree is automatically acyclic because the underlying graph is a tree (no cycles). Wait orientation of a tree can have directed cycles? A directed cycle would require a cycle in the underlying undirected graph, but a tree has no cycles, so any orientation of a tree yields a directed acyclic graph (DAG) because any directed cycle would correspond to an undirected cycle. However, it's possible to have a directed path that goes from u to v and also from v to u? That would require two distinct paths, not in a tree. So any orientation of a tree is acyclic. So we don't need to worry about cycles; any assignment of direction to each edge yields an acyclic orientation (since tree is acyclic). Good! So the condition reduces to: can we orient each edge of the tree such that every vertex has even indegree? Because then we can topologically sort the DAG to get a total order consistent with the orientation (i.e., if we orient u->v, then u must come before v). And that order will satisfy that each vertex's indegree equals the number of neighbors before it (since all incoming edges are from earlier vertices). However, we must ensure that the total order is consistent with the orientation; but any linear extension of the partial order defined by the orientation (where edges are directed) works. Since the orientation is a DAG, a topological order exists, and in that order, for each edge u->v, u comes before v, so v gets an incoming edge from u. So the indegree of v in the orientation equals the number of neighbors before v in that topological order. So if we can find an orientation with even indegree for all vertices, we can then produce a valid order by any topological sort (any order that respects the direction). Also note: In a tree, the orientation that yields even indegree for all vertices might not exist for all odd n? Let's see.
We need an orientation of the edges such that each vertex has even indegree. Let indeg(v) ∈ {0,1,...,deg(v)} and must be even. Since sum indeg = n-1 (total edges). For n odd, n-1 even, sum is even. But we need each indeg even. That is a system of linear equations mod 2? Over integers, indeg(v) even means indeg(v) ≡ 0 mod 2. Over GF(2), indeg(v) mod 2 = 0. The sum of indeg over all vertices is number of edges, which mod 2 is (n-1) mod 2. Since we need each indeg even, sum even => n-1 even => n odd, as necessary. But is it sufficient for trees? Not necessarily; we need to assign orientations edge by edge such that each vertex's indegree parity is 0. This is equivalent to finding an orientation where each vertex has even indegree mod 2. Over GF(2), we can think of each edge contributing 1 to the indegree of exactly one of its endpoints. This is like a parity orientation problem: Given a graph, assign a direction to each edge so that each vertex's indegree parity is specified (here 0). This is possible if and only if for every component, the sum of specified parities equals number of edges mod 2? Actually there's a known result: For any undirected graph, there exists an orientation such that each vertex has a prescribed indegree parity (0 or 1) if and only if the sum of prescribed parities equals the number of edges modulo 2. Because each edge contributes exactly 1 to the sum of indegrees (mod 2) (since indegree mod 2 sum = each edge contributes 1 mod 2 to exactly one endpoint's indegree). So sum of prescribed parities must equal |E| mod 2. Here prescribed parity = 0 for all vertices, so sum = 0. So we need |E| mod 2 = 0, i.e., |E| even, which is n-1 even => n odd. So the parity condition is both necessary and sufficient for existence of an orientation with even indegree modulo 2. But we also require actual integer indegree to be even, not just parity. However, parity is the only condition; any orientation that satisfies parity 0 for each vertex will have indegree even (since indegree is integer, but parity even means it's even). So we just need to find an orientation where each vertex gets an even number of incoming edges. Over GF(2), it's solvable given the sum condition. But is there always such an orientation for a tree? The parity condition is sufficient for any graph? For general graphs, there is a theorem: Given a graph, and a function f: V -> {0,1}, there exists an orientation such that indegree(v) ≡ f(v) (mod 2) for all v if and only if sum_v f(v) ≡ |E| (mod 2). This can be proven by constructing via spanning tree and then adjusting cycles? For a tree, it's simpler because there are no cycles. But we still need to assign directions. Actually for a tree, we can do a greedy approach from leaves: We can root the tree arbitrarily and then decide orientation of edges to satisfy parity constraints. Since tree has no cycles, we can treat it as a bipartite? Let's try: Suppose we want each vertex indegree even. Then for each vertex, the number of edges oriented towards it must be even. In a tree, we can think of a bottom-up assignment: For a leaf, its degree is 1. To have even indegree, indegree must be 0 or 2 etc., but maximum indegree is 1, so indegree must be 0 (since even and ≤1). So for a leaf, the edge incident to it must be oriented away from the leaf (i.e., leaf -> neighbor). So every leaf must have its edge outgoing. That forces orientation on all leaves. Then consider internal vertices: after processing children (in a rooted tree), we can determine orientation of the edge to its parent based on parity needed. This suggests that for a tree, such an orientation exists if and only if the condition n odd holds? Let's test on a simple tree: n=2, we have two leaves (both degree 1). To have each vertex indegree even, each leaf must have indegree 0, so both edges must be oriented away from leaves. But there is only one edge connecting them. If we orient leaf1 -> leaf2, then leaf1 has outdegree 1, indegree 0 (even), leaf2 has indegree 1 (odd) -> fails. If orient leaf2 -> leaf1, leaf2 indegree 0, leaf1 indegree 1 -> fails. So impossible. Indeed n=2 (even) fails condition n odd. So for n odd, does this greedy always work? Let's try a tree where a vertex has degree 3. Example: star with center degree 3 and 3 leaves (n=4). n=4 even, so condition fails, indeed no orientation can make all indegree even because sum indegree =3 odd, but parity condition would be sum f=0, |E|=3 mod2=1, not equal. So no. For n=5 star (center degree 4, leaves degree 1). Leaves must have indegree 0, so each leaf edge oriented away from leaf (leaf->center). Then center receives indegree 4 (from four leaves). 4 is even, so center indegree even. So orientation works: all leaves indegree 0, center indegree 4. So valid. So star with 5 works. For a more complex odd tree, we can try to orient edges from leaves outward. But need to ensure each vertex ends up with even indegree.
Let's design algorithm: Root the tree at an arbitrary node (say 1). We'll compute for each node whether the edge to its parent should be oriented towards parent or towards node, to satisfy parity condition for the subtree. But we need global condition: each vertex indegree even. This is equivalent to: For each vertex, the number of incident edges oriented towards it is even. That's a local condition. We can treat it as a system of equations on edges (binary variables x_{u,v} indicating direction: 0 if oriented u->v, 1 if v->u, for some ordering of endpoints). But since tree, we can solve by DP.
One approach: Do a DFS from leaves, each leaf forces its incident edge to be oriented away from leaf (since indegree of leaf must be 0). So we set that edge direction as leaf->parent. Then remove leaf, update parent's required indegree parity? Actually after fixing orientation of leaf edge, we can consider the leaf removed, and now the parent's degree reduces by 1, but the orientation of that edge is fixed: it contributes 1 indegree to parent (since leaf->parent means parent receives incoming edge). So parent now has one incoming edge from that leaf. The goal is to make parent's total indegree even. We can then treat the parent with remaining edges to other children, and we can decide orientation of its edge to its own parent later to make its total indegree even.
Thus we can process tree in a bottom-up fashion: For each node, after processing all children, we know how many incoming edges from children have been fixed (since each child edge orientation is determined when processing child: child will orient its edge to parent either as child->parent or parent->child depending on child's need? Wait leaf: child is leaf, we set edge leaf->parent, so parent gets indegree+1. For a general child, after processing its subtree, we might have already oriented the edge between child and parent? Actually the orientation of the edge between child and parent is not yet fixed when processing child's subtree; we can decide it based on child's parity requirement. The idea: we treat the tree as rooted. For each node v (except root), we need to decide orientation of edge (v, parent(v)). We'll compute whether v needs this edge to be oriented towards its parent or towards v in order for v to have even indegree from its subtree (including children) and this edge.
Let’s define: For node v, consider all edges incident to v: one to its parent (if any), and several to its children. For v to have even total indegree, the number of those edges oriented towards v must be even.
When processing v, we assume we have already processed its children. For each child c, we will have decided orientation of edge (v, c) such that c's even indegree condition is satisfied within its subtree and including this edge. But note that the orientation of (v,c) affects both v and c. So we need to decide orientation for each child edge while ensuring c's condition holds. This suggests a DP where we treat each child subtree independently and then decide orientation of the edge to satisfy child's parity, and then v accumulates indegree contributions.
Simplify: Since the condition is global, we can treat the problem as: assign direction to each edge such that each vertex has even indegree. This is a system of linear equations over GF(2). For each vertex v: sum_{e incident to v} x_{e,v} ≡ 0 (mod 2), where x_{e,v}=1 if e is oriented towards v, 0 otherwise. For each edge (u,v), we have x_{u,v} + x_{v,u} = 1 (since exactly one direction). So we can substitute: Let variable y_{u,v} = 1 if edge oriented from u to v (i.e., v gets indegree), else 0. Then condition for u: sum_{v neighbor} y_{v,u} ≡ 0; condition for v: sum_{u neighbor} y_{u,v} ≡ 0. Actually careful: For edge (u,v), we have two variables? We can use a single binary variable d_{uv} indicating direction: say d_{uv}=0 means u->v, then indegree: v gets 1, u gets 0; d_{uv}=1 means v->u. Then indegree of u = sum_{w neighbor} d_{wu} (where d_{wu}=1 if edge w->u, i.e., orientation from w to u). Condition: indegree(u) mod 2 = 0 => sum_{w neighbor} d_{wu} ≡ 0.
Now for a tree, we can solve by choosing a root and determining d for edges from children to parent.
We can treat each edge variable as unknown. The equations are linear modulo 2. The number of variables = n-1, number of equations = n (one per vertex). However, the equations are not independent; sum of all vertex equations gives each edge counted twice? Let's sum all equations: For each edge (u,v), it contributes d_{uv} to indegree(v) and d_{vu} to indegree(u), but d_{uv}+d_{vu}=1 (since exactly one direction). So total sum = number of edges mod 2 = n-1 mod 2. Since we want each equation =0 mod2, total sum=0 mod2. So we need n-1 even => n odd. So when n odd, the system has a solution (since rank deficiency, etc). For a tree, we can find a solution by fixing orientation of edges arbitrarily and then correcting? Actually because tree is bipartite? Let's try to construct a solution explicitly.
A known construction: Perform a DFS, and orient edges from leaves up such that each node (except root) ends up with even indegree? But we need all nodes even indegree, including root. In the leaf-to-root approach, leaves have indegree 0 (by orienting away). Then for an internal node, after processing children, it will have some number of incoming edges from children (those where we oriented child->node). Since each child subtree ensures the child's indegree even, and we oriented the edge to satisfy child, the node's indegree from children is determined. Then we can decide orientation of the edge to its parent to make node's total indegree even. That will impose a condition on the parent later. Finally, at the root, after processing all children, we have no parent edge. So root's indegree must be even based solely on children contributions. If it's not even, then we cannot satisfy the root condition. So the condition for existence reduces to: after processing, root's indegree from children must be even. This is a parity condition that depends on the structure. Let's test with n odd: For a tree, the root's indegree from children, under the greedy algorithm where we force each child to have even indegree by possibly orienting the child-parent edge accordingly, may turn out to be even automatically if n odd? Let's see.
Define DP: For each node v, we consider the subtree rooted at v. We will decide orientation of edges within the subtree (including the edge to its parent, if any) such that all nodes in the subtree satisfy even indegree, except possibly v's condition relative to its parent? Actually we want all nodes globally even. So we need to ensure v's indegree from its children and possibly parent is even. If we treat v's parent edge as not yet decided, we can aim to have v's indegree from its children be even, and then later orientation of parent edge can be chosen to make total even. That would mean that we need the parity of indegree from children to be something we can adjust with parent edge. Specifically, if we denote x = (number of children edges oriented towards v) mod 2, then to make total indegree of v even, the edge to parent must be oriented such that it contributes 1 to v's indegree if x is odd (so total even) or 0 if x is even. That means the orientation of the parent edge is forced by parity of x. So if we can ensure that for each non-root node, the orientation of its edge to its parent can be set accordingly, and for root, we need x even (since no parent edge). So the existence reduces to whether the root's x (from children) is even. So we need to compute recursively whether we can assign orientations to children edges so that this works.
Now, for each child c of v, we need to decide orientation of edge (v,c). The child c also has its own children; to satisfy c's even indegree, the orientation of edge (v,c) relative to c will be determined by the parity of indegree contributions from c's children. Specifically, for child c, after processing its subtree, we can compute the parity of indegree contributions from its children (excluding parent) that are needed to satisfy c's condition if we had the parent edge oriented in a certain way. Let's formalize:
We root the tree at an arbitrary root (say 1). For each node u, we consider its children in the rooted tree. Let f(u) be the parity (0/1) of the number of incoming edges from children that we will orient towards u, assuming that the edge to its parent (if any) will be used to adjust u's parity. But we need to decide orientation of each child edge to satisfy child's condition.
Alternatively, define DP that returns whether the subtree can be oriented such that all nodes in it have even indegree, and such that the edge connecting the subtree root (v) to its parent (if any) is oriented in a particular direction (or we have a degree of freedom). But since we have to satisfy all nodes, we can compute the required orientation of the edge to parent based on the subtree.
Consider v with parent p. We want to assign directions to edges in the subtree of v (including edge (v,p)) such that all nodes in this subtree (including v) have even indegree. However, the edge (v,p) also affects p, which is outside the subtree; we can't fully control p's indegree yet. So we treat this edge as a "boundary" where we decide direction, but p's condition will impose a parity requirement later. For the subtree, we can compute the parity of the required contribution to v's indegree from the edge (v,p) to make v's total indegree even, given the orientations of edges to children.
Specifically, suppose we have processed all children of v. For each child c, we will have determined the orientation of edge (v,c) and also ensured that c and its descendants have even indegree. How do we decide orientation of (v,c)? It will be based on child's needs. For child c, after processing its children, we know the parity of incoming edges from its children (excluding v). Let's denote inc_child_parity(c) = parity of number of incoming edges from c's children (i.e., edges oriented from child_of_c to c) that we have already fixed. To make c's total indegree even, the sum of indegree from its children plus indegree from its parent (v) must be even. So if inc_child_parity(c) = a (0 or 1), then we need the edge (v,c) to be oriented towards c (i.e., v->c) if a is 0? Actually indegree of c = indegree from children (already fixed) + (1 if edge (v,c) is oriented towards c else 0). We need total even: (a + orientation_towards_c) mod 2 = 0 => orientation_towards_c ≡ a (mod 2)? Wait: a + x ≡ 0 mod 2 => x ≡ a mod 2? Actually a + x ≡ 0 => x ≡ a (since -a mod 2 = a because mod 2, a = -a). So orientation_towards_c must be equal to a mod 2? Let's check: If a=0, then we need x=0 to have even total (0+0=0). So edge must not be oriented towards c, i.e., oriented from c->v (so that c gets indegree 0 from that edge). If a=1, then we need x=1 to make total even (1+1=2 even). So edge must be oriented towards c (v->c). So orientation of edge (v,c) is forced: it must be oriented towards c if and only if the parity of indegree from c's children is odd. So we can compute this recursively: For each child c, we compute the parity of indegree from its children (i.e., number of children edges oriented towards c modulo 2). Then we set the orientation of edge (v,c) accordingly: if that parity is 0, orient edge c->v; if parity is 1, orient edge v->c. Then after setting, c's total indegree becomes even. And also, v receives an indegree from that child if the orientation is v<-c (i.e., c->v) then v gets +1 indegree; if orientation is v->c, then v gets 0 from that edge.
Thus, for node v, after processing all children, the total indegree contributed to v from its children edges is the number of children c for which we oriented c->v. According to the rule, we orient c->v exactly when inc_child_parity(c) = 0. So for each child c, we need to know inc_child_parity(c) which is the parity of incoming edges from its children after they have been processed. That parity is computed based on its own children. So we can define recursively: For a node v, we process its children, and for each child we compute something that tells us both the orientation of the edge (v,c) and the contribution to v's indegree.
Let’s define for each node v (with respect to its parent p) the value "req" which is the orientation of the edge (v,p) that would be required to make v's indegree even? Actually we can compute the parity of indegree from children for v. Let's denote parity_contrib(v) = (number of children c such that we oriented c->v) mod 2, i.e., the parity of incoming edges from children. Then, to make v's total indegree even, the edge to its parent (if any) must be oriented such that it contributes indegree to v equal to parity_contrib(v) (since parity_contrib(v) + (incoming from parent?) must be even => incoming_from_parent ≡ parity_contrib(v) mod 2). So the required orientation of edge (v,p) is: if parity_contrib(v) = 0, then we need the edge not to be oriented towards v (i.e., v->p) to keep v's indegree even? Wait: v's indegree = parity_contrib(v) + (1 if edge is oriented towards v else 0). Want even: parity_contrib(v) + x ≡ 0 mod 2 => x ≡ parity_contrib(v) mod 2? As before: x = parity_contrib(v) (since adding gives 0 mod2). So if parity_contrib(v) = 0, we need x=0, i.e., edge oriented away from v (v->p). If parity_contrib(v)=1, we need x=1, i.e., edge oriented towards v (p->v). So the required orientation of the edge (v,p) is: orient from v to p if parity_contrib(v)=0, and from p to v if parity_contrib(v)=1.
Now, note that this requirement is exactly what we used for children when processing v as parent: for a child c, we set orientation based on parity_contrib(c). So recursively, we can compute parity_contrib(v) after processing its children. Then for the root (with no parent), we need parity_contrib(root) = 0 (because there is no parent edge to adjust, so its total indegree must be even, which is just parity_contrib(root) must be 0). So the condition for existence is parity_contrib(root) = 0.
Now, what is parity_contrib(v)? It depends on children: For each child c, we orient edge (v,c) based on parity_contrib(c). Specifically, if parity_contrib(c)=0, we orient c->v (so v gets +1 indegree from that child). If parity_contrib(c)=1, we orient v->c (so v gets 0 indegree from that child). Therefore, the contribution to v's indegree from child c is exactly (1 if parity_contrib(c)=0 else 0). So parity_contrib(v) = (sum over children c of (1 if parity_contrib(c)=0 else 0)) mod 2.
So we can compute this recursively: For leaf node (no children), parity_contrib(leaf) =? Let's compute: leaf has no children, so sum over children is 0, so parity_contrib(leaf) = 0 mod 2 = 0. Then according to rule, for leaf, parity_contrib=0, so when processing its parent, orientation for edge leaf-parent will be: since parity_contrib(leaf)=0, we orient leaf->parent. That gives parent indegree +1 from this leaf. That matches earlier reasoning: leaf must have indegree 0, so edge oriented away from leaf.
Now, compute parity_contrib for internal nodes using this recurrence. Then the condition for a solution is parity_contrib(root)=0.
Now, does this condition always hold for trees with odd number of vertices? Let's test on chain of 4 (n=4). Root at one end? Let's compute.
Take a chain 1-2-3-4. Root at 1 (or any). But the recurrence's result might depend on choice of root? However, the existence of orientation should be independent of root choice; the condition parity_contrib(root)=0 should be equivalent to something global. Let's compute for chain 1-2-3-4 rooted at 1 (or 4?). Better to root at an arbitrary node; the condition for root must be satisfied if a solution exists.
We can compute parity_contrib for each node bottom-up.
Let’s root at 1 (as parent of 2, 2 parent of 3, 3 parent of 4). So children: node4 leaf, node3 has child4, node2 has child3, node1 has child2.
Compute:
- leaf4: parity_contrib(4)=0.
- node3: children: 4 with parity_contrib(4)=0, so contribution from child = 1 (since parity_contrib=0). So sum =1, parity_contrib(3)=1 mod2 =1.
- node2: child: 3 with parity_contrib(3)=1, contribution =0 (since parity_contrib=1 => orient parent->child? Wait contribution to parent (node2) from child 3 is 1 if parity_contrib(child)=0 else 0). So contribution=0. So sum=0, parity_contrib(2)=0.
- node1: child: 2 with parity_contrib(2)=0, contribution=1. So sum=1, parity_contrib(1)=1.
Root =1 has parity_contrib=1, not 0, so condition fails -> no orientation, i.e., impossible. Indeed chain of 4 is impossible.
If we root at 2 instead? Let's test: root at 2, with children 1 and 3? Actually tree is a path, root at 2: neighbors 1 and 3. Let's define children as 1 and 3 (assuming we direct edges away from root). Leaves: 1 and 4. But 4 is child of 3. Compute:
- leaf1: parity_contrib(1)=0.
- leaf4: parity_contrib(4)=0.
- node3: children: 4 (parity_contrib=0) -> contribution=1, so parity_contrib(3)=1.
- node2: children: 1 (parity_contrib=0) contribution=1; 3 (parity_contrib=1) contribution=0; sum=1, parity_contrib(2)=1.
Root=2 => parity_contrib=1, fails.
Root at 3 symmetric, fails. So indeed condition fails for n=4.
Now chain of 3 (n=3). Root at 1 (or any). Path 1-2-3.
- leaf3: parity_contrib=0.
- node2: child3 with parity_contrib=0 => contribution=1, parity_contrib(2)=1.
- node1: child2 with parity_contrib=1 => contribution=0, parity_contrib(1)=0. Root parity_contrib=0 -> success. So possible.
Chain of 5 (n=5). Root at 1? 1-2-3-4-5.
Leaves:5. Compute:
- node5:0
- node4: child5(0) -> contrib=1 => parity_contrib(4)=1
- node3: child4(1) -> contrib=0 => parity_contrib(3)=0
- node2: child3(0) -> contrib=1 => parity_contrib(2)=1
- node1: child2(1) -> contrib=0 => parity_contrib(1)=0 -> success.
So condition parity_contrib(root)=0 matches n odd? For chain, we got root parity_contrib=0 for odd n, 1 for even n. That suggests that parity_contrib(root) = n mod 2? Actually for chain of length n, if we root at an endpoint, parity_contrib(root) = (n mod 2)? Let's compute: for n=1, leaf root parity_contrib=0 (n odd 1 -> 0). n=2: root leaf? Actually if root at endpoint of 2, then leaf child? Let's compute: n=2: nodes 1-2, root at 1: node2 leaf, parity_contrib(2)=0; node1 child2 (0) contrib=1 => parity_contrib(1)=1 -> fail. So root parity_contrib=1 when n even (2). So it's like parity_contrib(root) = (n+1) mod2? Actually n=2 even gives 1; n=3 odd gives 0; n=4 even gives 1; n=5 odd gives 0. So root parity_contrib(root) = (n%2==0)? maybe 1 when n even, 0 when n odd. So condition reduces to n odd? Let's test for other trees.
Consider star with 5 nodes (center degree 4, leaves 4). Root at center. Leaves parity_contrib=0 each. For center: children are 4 leaves each parity_contrib=0, so each contributes 1. Sum contributions =4, parity_contrib(center)=0 (since 4 mod2=0). So root parity_contrib=0 -> success. So n odd works.
What about a tree with odd n but root parity_contrib might not be 0? Let's try a more complex tree: e.g., a tree where root has 2 children, one child subtree has even number of nodes? We need to compute parity_contrib recursively. Let's derive formula for parity_contrib(v) in terms of subtree sizes? Maybe parity_contrib(v) is related to the parity of the size of its subtree? Let's explore.
From recurrence: parity_contrib(v) = (number of children c with parity_contrib(c)=0) mod 2.
We can compute parity_contrib bottom-up. For leaf, parity_contrib=0.
Now consider a node v. Suppose we know for each child c, parity_contrib(c). Then parity_contrib(v) = XOR over children of (parity_contrib(c)==0 ? 1 : 0). Equivalent to parity_contrib(v) = (number of children with parity_contrib(c)=0