#P17449. 奶龙大战暴暴龙 3.2 / Nailoong vs. Bombloong 3.2
奶龙大战暴暴龙 3.2 / Nailoong vs. Bombloong 3.2
Problem Description
This is a communication problem. In this problem, your program will be run twice. Between the two runs, all variables stored in memory will be lost, but the information obtained in the first run may be very important for solving the problem correctly in the second run.
There are two roles in this problem: “Nailoong” and “Bombloong”. Nailoong has the complete structure of a tree with nodes, while Bombloong only knows the number of nodes . Since Bombloong is locked up by an evil little leopard, Nailoong can only use a special one-way communication method to help Bombloong reconstruct a tree that is isomorphic to the original tree.
Isomorphic: Two trees and are isomorphic if and only if there exists a bijection such that for all , .
The communication rules are as follows:
- Nailoong needs to color each node of the tree either black or white.
- The little leopard will generate a sequence according to the following code.
- Nailoong can only send this sequence of length to Bombloong.

Nailoong cannot directly send the node labels to Bombloong; she can only send the sequence . After receiving and the sequence , Bombloong needs to construct and output a tree that is isomorphic to Nailoong’s tree.
Communication Protocol
For each test point, the contestant’s program will be run twice. In the provided files, there is a testing tool for local debugging.
First Run
In the first run, you will play the role of “Nailoong”.
Input
The first line contains a string first, which allows your program to recognize that this is the first run.
The second line contains an integer (), the number of test cases.
For each test case, the first line contains an integer (), the number of nodes in the tree.
The next lines each contain two integers (), representing an edge of the tree.
It is guaranteed that the sum of over all test cases does not exceed .
Output
Your output should contain lines. The -th line contains a sequence whose length equals the number of nodes of the -th tree (, ), meaning Nailoong colors the -th node with . Here, means white, and means black.
Second Run
In the second run, you will play the role of “Bombloong”.
Input
The first line contains a string second, which allows your program to recognize that this is the second run.
The second line contains an integer (), the number of test cases.
For each test case, the first line contains an integer (), the number of nodes in the tree.
The next line contains integers, representing the sequence generated by the judge based on Nailoong’s coloring.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For the -th test case, let the input number of nodes be . Output lines, each containing two integers , representing an edge of the tree you reconstructed (). You need to ensure that the tree you output is isomorphic to the tree given in the first run.
Input Format
See the communication protocol in the problem description.
Output Format
See the communication protocol in the problem description.
first
2
2
1 2
3
1 2
2 3
0 1
0 1 0
second
2
3
0 1 0 1 0
2
0 1 0
1 3
2 3
1 2
Hint
The two samples demonstrate the two runs in the same test point.
Note that if the trees in the first input are in order, and the output color sequences are . Let the color sequences obtained by the judge from () be . The judge will randomly generate a permutation of , and then the order sent to the second input will be . Let the output trees be in order; the judge will compare and to judge correctness.
How to Use the Provided Files
python3 tree_communication_testing_tool2.py data.in ./solution
python3 tree_communication_testing_tool2.py --trials 10 data.in ./solution
python3 tree_communication_testing_tool2.py data.in python3 solution.py
Here, solution or solution.py is the executable compiled from C/C++ or the Python source code, data.in is the data for the first input, and the trials parameter indicates the number of repeated tests.
Translated by ChatGPT 5