Under construction

Still being built. Things may change

Hiding a Tree

Time limit2sMemory limit512 MB

Summary
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 00.

You have a tree with nn vertices, labeled with integers from 11 to nn. You want to write this tree in the following standard format.

nn u1u_1 v1v_1 …\dots un−1u_{n-1} vn−1v_{n-1}

Here nn is the number of vertices, and uiu_i, viv_i are the vertices connected by the ii-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 11 to 10910^9, 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 nn (2≤n≤1000002 \leq n \leq 100000), the number of vertices in the tree.

The next line contains nn integers. The ii-th of them is 00 if the label of vertex ii is fixed, and 11 if it can be changed.

Each of the next n−1n-1 lines contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n), 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 00.

If it is impossible, print a single number -1.

Examples1

  1. Example 1

    Input
    5
    0 1 0 1 0
    1 3
    1 4
    2 5
    3 5
    
    Expected output
    5
    1 3
    1 7
    2 5
    3 5