#P17449. 奶龙大战暴暴龙 3.2 / Nailoong vs. Bombloong 3.2

    ID: 19962 远端评测题 4000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>交互题Special Judge2026通信题高校校赛

奶龙大战暴暴龙 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 nn nodes, while Bombloong only knows the number of nodes nn. 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 T1(V1,E1)T_1(V_1,E_1) and T2(V2,E2)T_2(V_2,E_2) are isomorphic if and only if there exists a bijection f:V1→V2f:V_1\to V_2 such that for all {u,v}∈E1\{u,v\}\in E_1, {f(u),f(v)}∈E2\{f(u),f(v)\}\in E_2.

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 c1,c2,…,c2n−1c_1,c_2,\ldots,c_{2n-1} of length 2n−12n-1 to Bombloong.

2026_Kruskal_Cup_statement_12.png

Nailoong cannot directly send the node labels to Bombloong; she can only send the sequence cc. After receiving nn and the sequence cc, 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 TT (1≤T≤1041\le T\le 10^4), the number of test cases.

For each test case, the first line contains an integer nn (2≤n≤2×1052\le n\le 2\times 10^5), the number of nodes in the tree.

The next n−1n-1 lines each contain two integers u,vu,v (1≤u,v≤n1\le u,v\le n), representing an edge of the tree.

It is guaranteed that the sum of nn over all test cases does not exceed 2×1052 \times 10^5.

Output

Your output should contain TT lines. The ii-th line contains a sequence col1,col2,…,colncol_1,col_2,\ldots,col_n whose length equals the number of nodes nn of the ii-th tree (coli∈{0,1}col_i\in\{0,1\}, i=1,2,…,ni=1,2,\ldots,n), meaning Nailoong colors the ii-th node with colicol_i. Here, coli=0col_i=0 means white, and coli=1col_i=1 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 TT (1≤T≤1041\le T\le 10^4), the number of test cases.

For each test case, the first line contains an integer nn (2≤n≤2×1052\le n\le 2\times 10^5), the number of nodes in the tree.

The next line contains 2n−12n-1 integers, representing the sequence generated by the judge based on Nailoong’s coloring.

It is guaranteed that the sum of nn over all test cases does not exceed 2×1052 \times 10^5.

Output

For the ii-th test case, let the input number of nodes be nn. Output n−1n-1 lines, each containing two integers u,vu,v, representing an edge of the tree you reconstructed (1≤u,v≤n1\le u,v\le n). 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 t1,…,tTt_1,\ldots,t_T in order, and the output color sequences are col1,…,colTcol_1,\ldots,col_T. Let the color sequences obtained by the judge from ti,colit_i,col_i (1≤i≤T1\le i\le T) be aia_i. The judge will randomly generate a permutation p1,…,pTp_1,\ldots,p_T of 1∼T1\sim T, and then the order sent to the second input will be ap1,…,apTa_{p_1},\ldots,a_{p_T}. Let the output trees be t1′,t2′,…,tT′t'_1,t'_2,\ldots,t'_T in order; the judge will compare tpit_{p_i} and ti′t'_i 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