Hiding a Tree
Time limit2sMemory limit512 MB
Relabel the changeable vertices of a tree so that the XOR of n and all edge endpoints is 0, or report -1 if no relabeling works.
- Level
Medium7 of 10
- Topics
- Tree, Bit manipulation, Implementation
- Solved
- No attempts yet
Problem
XOR-scanner is a device that checks a sequence of integers. It accepts the sequence if and only if the bitwise XOR of all numbers in the sequence is .
You have a tree with vertices, labeled with integers from to . You want to write this tree in the following standard format.
Here is the number of vertices, and , are the vertices connected by the -th edge.
You want the XOR-scanner to accept your output. It may not accept it at first, so you can change the labels of some vertices. After relabeling, all vertices must have distinct integer labels from to , inclusive.
For each vertex, it is known whether its label can be changed. Relabel some of the vertices that can be changed (you may keep some or all of their labels as they are) so that the XOR-scanner accepts the output. If this is impossible, report it.
Input
The first line contains an integer (), the number of vertices in the tree.
The next line contains integers. The -th of them is if the label of vertex is fixed, and if it can be changed.
Each of the next lines contains two integers and (), the endpoints of an edge.
Output
If a valid relabeling exists, print the relabeled tree in the same format as the statement. Keep the order of the edges and the order of the endpoints within each edge. The bitwise XOR of all printed numbers must be .
If it is impossible, print a single number -1.