#P9498. 「RiOI-2」equals
「RiOI-2」equals
Background
On a small tree stands a fantasy castle. This is the territory of Country E, and Little E is the king of Country E.
To build a perfect Country E, he needs to tell right from wrong and move toward justice.
However, he seems a bit too idealistic. Sometimes there is no perfect rule. Is it black or white? Who can tell?
Problem Description
Given a tree with nodes rooted at , define the depth of a node as the number of nodes on the simple path from it to the root.
You need to color each node black or white such that the sum of depths of black nodes equals the sum of depths of white nodes. Let represent that node is black or white, respectively. Then this is .
If there is no solution, output a single line with one integer .
Input Format
The first line contains one positive integer .
The next lines each contain two integers , indicating that there is an edge between node and node in the tree. It is guaranteed that the given edges are not repeated.
Output Format
If there is a solution, output one line with numbers .
If there is no solution, output a single line with one integer .
This problem uses a Special Judge. As long as your solution satisfies the condition or correctly determines that there is no solution, you can get the score for this test point.
6
1 2
1 3
2 4
2 5
2 6
0 1 1 1 0 0
5
1 2
1 3
2 4
2 5
-1
Hint
Sample Explanation
For the first set of testdata, the depths of each node are . The sum of depths of black nodes is , and the sum of depths of white nodes is . They are equal, so the sample output is correct. Possible correct outputs include but are not limited to the sample output, 0 1 1 0 0 1, 1 0 0 1 0 1, and so on.
Constraints and Notes
This problem uses bundled tests.
| Score | Special Properties | ||
|---|---|---|---|
| / | |||
| / | is even | ||
| The tree is a star (the root is not guaranteed to be the center) | |||
| The tree is a chain (the root is not guaranteed to be an endpoint of the chain) | |||
| / |
A slash means there is no special restriction in that column.
For of the data, , , and the input forms a tree.
Translated by ChatGPT 5