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

    ID: 3183 远端评测题 3000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2017O2优化CTT(清华集训/北大集训)

[清华集训 2017] 榕树之心

Background

Late autumn. The cold wind has blown away the last trace of summer heat and the leaves of the bushes under the banyan tree. Evan and Lyra, who have known each other for years, have returned once again to the lush banyan tree where they met as children. The brook is the same, the stone bridge is the same; though the banyan tree has experienced cycles of thriving and withering, it still stands tall like a green canopy. Only Evan and Lyra are no longer the inexperienced teenagers they were seven or eight years ago.

...

“It’s almost winter, yet the banyan still hasn’t shed its leaves...”

“The banyan is an evergreen; you won’t see a distinct season of leaf fall...”

“Sigh... I didn’t expect it’s already been seven years. The banyan is the same as it was back then, but you are no longer who you were...”

“Is there really anything that never changes? The banyan is evergreen, and its seemingly everlasting green canopy is made up of countless leaves cycling through life and death. In the passage of time, everything is constantly changing...”

“But look at this banyan—day after day, season after season, year after year—it seems unchanged since ancient times, with gnarled roots and lush foliage. I’m thinking, perhaps it would be better to be a tree, letting time flow between my branches and leaves, while I simply keep this patch of shade.”

“The banyan may endure, but in this infinite span of time, it will eventually return to dust. Rather than rooting in one place and experiencing the same seasonal cycle year after year like a banyan, why not see as much of the world as possible in limited time? Besides, although the banyan grows slowly, it still sends out a new branch each spring to explore outward...”

“Really? Over its long life, the banyan explores step by step like that?”

“Even if the canopy appears unchanged, the banyan follows periodic changes over time. When spring comes, it naturally grows; it should show that, shouldn’t it...”

“Compared to instinctive growth in response to the seasons, I’d rather believe the banyan has a lively, exploring heart.”

“In fact, the banyan does have a heart. When it’s first planted, the heart sprouts at the root. Later, each spring when the banyan grows a new branch, its heart moves a little toward the direction of that new branch, getting closer to the outside world. Look at the branches above your head—crisscrossing in all directions—the heart has been moving among these boughs for decades...”

“Wow, so somewhere among this dense network of branches, the banyan’s heart is hidden?”

“Exactly. But to know where it is, you’ll need to put in some extra effort...”

“Ah, thinking about it, a tree still isn’t as good as a person... For example, you—if you lean in like this, you can hear the heartbeat...”

...

Problem 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