#P4228. [清华集训 2017] 榕树之心

[清华集训 2017] 榕树之心

Description

A banyan can be abstracted as a rooted tree with nn nodes, numbered 1n1 \sim n, where node 11 is the root. Initially, the tree has only node 11, and the heart is at node 11. After that, at each step, the tree grows a new node: specifically, a node adjacent to some currently existing node is added to the tree. Then, the heart moves one step along the simple path from the heart to the newly added node. This tree with nn nodes has many possible growth orders, and different orders may lead to different final positions of the heart. Now Evan and Lyra want to know: which nodes could possibly be the final resting position of the heart when the growth process ends?

For example, consider a tree of size 44 with edges {<1,2>,<1,3>,<1,4>}\{<1,2>,<1,3>,<1,4>\}. There are three different growth orders that make the heart end at nodes 22, 33, and 44, respectively:

Finally ending at node 22:

  • Grow 33 from 11, the heart moves from 11 to 33,
  • Grow 44 from 11, the heart moves from 33 back to 11,
  • Grow 22 from 11, the heart moves from 11 to 22.

Finally ending at node 33:

  • Grow 22 from 11, the heart moves from 11 to 22,
  • Grow 44 from 11, the heart moves from 22 back to 11,
  • Grow 33 from 11, the heart moves from 11 to 33.

Finally ending at node 44:

  • Grow 22 from 11, the heart moves from 11 to 22,
  • Grow 33 from 11, the heart moves from 22 back to 11,
  • Grow 44 from 11, the heart moves from 11 to 44.

We can prove that there is no growth order that makes the heart end at node 11.

Input Format

Read from standard input.

The first line contains two positive integers W,TW, T, representing the subtask identifier (in the samples W=0W = 0) and the number of test cases. Then follow TT test cases. For each test case:

  • The first line contains a positive integer nn, the number of nodes in the tree.
  • The next n1n - 1 lines each contain two positive integers ai,bia_i, b_i, indicating there is a tree edge between nodes numbered aia_i and bib_i. It is guaranteed that aibia_i \neq b_i and the n1n - 1 edges form a tree.

Output Format

Output to standard output.

If the input WW is not equal to 33, for each test case output a line containing a length-nn 0101 string. The ii-th character indicates whether node ii could be the final position of the heart. A 11 means possible, and a 00 means impossible.

If the input WW equals 33, then for each test case output a single character indicating the answer for node 11.

0 3
4
1 2
1 3
1 4
6
1 2
1 3
1 4
4 5
5 6
10
1 2
1 3
3 4
3 5
3 6
4 7
7 8
8 9
9 10
0111
000101
0000001010

Hint

TestPoint 1 [10 pts]

  • T50;n15T \leq 50; n \leq 15.

TestPoint 2 [10 pts]

  • T20;n105T \leq 20; n \leq 10^5. For every node except node 11, the degree (including its parent) is at most 22.

TestPoint 3 [10 pts]

  • T200;n100T \leq 200; n \leq 100. Only output a single character indicating the answer for node 11, i.e., it suffices to ensure the correctness for node 11.

TestPoint 4 [35 pts]

  • T20;n103T \leq 20; n \leq 10^3.

TestPoint 5 [35 pts]

  • T20;n105T \leq 20; n \leq 10^5.

Translated by ChatGPT 5