#P9498. 「RiOI-2」equals

    ID: 10109 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>洛谷原创Special JudgeO2优化构造洛谷月赛

「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 nn nodes rooted at 11, define the depth did_i 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 ci={0,1}c_i = \{0, 1\} represent that node ii is black or white, respectively. Then this is ci=0di=ci=1di\displaystyle\sum_{c_i=0} d_i = \sum_{c_i=1} d_i.

If there is no solution, output a single line with one integer 1-1.

Input Format

The first line contains one positive integer nn.

The next n1n - 1 lines each contain two integers ui,viu_i, v_i, indicating that there is an edge between node uiu_i and node viv_i in the tree. It is guaranteed that the given edges are not repeated.

Output Format

If there is a solution, output one line with nn numbers c1cnc_1 \dots c_n.

If there is no solution, output a single line with one integer 1-1.

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 d=[1,2,2,3,3,3]d = [1, 2, 2, 3, 3, 3]. The sum of depths of black nodes is d1+d5+d6=1+3+3=7d_1 + d_5 + d_6 = 1 + 3 + 3 = 7, and the sum of depths of white nodes is d2+d3+d4=2+2+3=7d_2 + d_3 + d_4 = 2 + 2 + 3 = 7. 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.

Subtask\rm Subtask Score nn\le Special Properties
00 55 2020 /
11 1515 500500
22 2020 5×1035\times 10^3
33 1010 / nn is even
44 55 The tree is a star (the root is not guaranteed to be the center)
55 The tree is a chain (the root is not guaranteed to be an endpoint of the chain)
66 4040 /

A slash means there is no special restriction in that column.

For 100%100\% of the data, 1n1061 \le n \le 10^6, 1ui,vin1 \le u_i, v_i \le n, and the input forms a tree.

Translated by ChatGPT 5