Universal Cup Judging System

Universal Cup

시간 제한: 2.0 s 메모리 제한: 512 MB 총점: 100 커뮤니케이션
통계

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 crucial for correctly solving the problem in the second run.

There are two characters in this problem: "Nailoong" and "Bombloong".

Nailoong has the complete structural information of a tree with $n$ nodes, while Bombloong only knows the number of nodes $n$. Since Bombloong has been imprisoned by the evil Little Leopard, Nailoong can only help Bombloong reconstruct a tree that is isomorphic to the original tree through a special one-way communication method.

The rules of communication are as follows:

  • Initially, all nodes in the tree are white.
  • Nailoong can perform $m \le n - 3$ operations (the value of $m$ is determined by Nailoong).
  • In the $i$-th operation, Nailoong needs to choose a node $x_i$ on the tree and flip its color (white to black, black to white).
  • After each flip, let $a_i$ be the number of edges in the current tree whose two endpoints have different colors.
  • After $m$ operations, Nailoong obtains a sequence $a_1, a_2, \dots, a_m$ of length $m$.

Nailoong cannot directly send node indices to Bombloong; she can only send the total number of operations $m$ and the sequence $a$ to Bombloong. After receiving $n$, $m$, and the sequence $a$, Bombloong needs to construct and output a tree that is isomorphic to Nailoong's tree.

Communication Protocol

In each test case, the contestant's program will be run twice. A testing tool is provided in the handout files for local debugging.

First Run

In the first run, you will play the role of "Nailoong".

Input: The first line of input contains an integer $1$, which allows your program to identify that this is the first run. The second line contains an integer $n$ ($4 \le n \le 3 \times 10^5$), representing the number of nodes in the tree. The next $n - 1$ lines each contain two integers $u, v$ ($1 \le u, v \le n$), representing an edge in the tree.

Output: The first line of output should contain an integer $m$ ($0 \le m \le n - 3$), representing the number of operations. The second line contains $m$ integers $x_1, x_2, \dots, x_m$ ($1 \le x_i \le n$), representing the indices of the nodes flipped by Nailoong in each operation in sequence.

Second Run

In the second run, you will play the role of "Bombloong".

Input: The first line of input contains an integer $2$, which allows your program to identify that this is the second run. The second line contains two integers $n, m$ ($4 \le n \le 3 \times 10^5$, $0 \le m \le n - 3$), representing the number of nodes and the number of operations performed by Nailoong, respectively. If $m > 0$, the third line contains $m$ integers $a_1, a_2, \dots, a_m$ ($0 \le a_i \le n$), representing the sequence generated by the judge based on Nailoong's operations; if $m = 0$, the third line does not exist.

Output: Output $n - 1$ lines, each containing two integers $u, v$, representing an edge of the tree you reconstructed ($1 \le u, v \le n$). You must ensure that the output tree is isomorphic to the tree provided in the input of the first run.

Examples

Sample Input 1

1
4
1 2
2 3
3 4

Sample Output 1

1
2

Sample Input 2

2
4 1
2

Sample Output 2

1 3
3 2
2 4

Note

The two samples demonstrate the two runs within the same test case.

Nailoong flipped node $2$. At this point, the number of edges with different colored endpoints is $2$, so $a_1 = 2$.

Bombloong received the sequence $a = [2]$. Since Bombloong and Nailoong share a deep connection, she immediately guessed the structure of the tree. Although the tree obtained by Bombloong differs from the tree Nailoong held in terms of node indexing, they are isomorphic, so it is still considered correct.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#2753EditorialOpen$\frac{201}{202}n-O(1)$ 次翻点题解 by ChatGPTAnonymousView

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.