#P17448. 奶龙大战暴暴龙 3.1 / Nailoong vs. Bombloong 3.1
奶龙大战暴暴龙 3.1 / Nailoong vs. Bombloong 3.1
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 characters in this problem: “Nailoong” and “Bombloong”. Nailoong knows 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 help Bombloong restore a tree isomorphic to the original one through a special one-way communication method.
Isomorphic: Two trees and are called 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 as black, white, or gray.
- 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 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, a testing tool is included 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 of length equal to the number of nodes in the -th tree (, ), meaning that Nailoong colors the -th node with . Here, means white, means black, and means gray.
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 restored (). You need to guarantee that the output tree is isomorphic to the tree in the input of 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 2
second
2
3
0 1 2 1 0
2
0 1 0
1 3
2 3
1 2
Hint
The two samples demonstrate the two runs within the same test point.
Note that if the trees in the first input are in order, and the output color sequences are . Let the sequences obtained by the judge from () be . The judge will randomly generate a permutation of , and then the order sent in the second input will be . The output trees are in order, and the judge will compare with for judging.
How to Use the Provided Files
python3 tree_communication_testing_tool1.py data.in ./solution
python3 tree_communication_testing_tool1.py --trials 10 data.in ./solution
python3 tree_communication_testing_tool1.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 from the first input, and the trials parameter indicates the number of repeated tests.
Translated by ChatGPT 5