题目描述
Problem Background
Ysuperman responds to the call and decides to plant trees in the kindergarten.
Problem Description
Ysuperman has an undirected tree \( T \) with \( n \) nodes. If you are unfamiliar with what a tree is, Ysuperman is happy to inform you that a tree is an undirected, connected graph without cycles.
Since the tree is rootless, it cannot be planted. After extensive research in horticulture, Ysuperman discovered that a node can serve as a root only if it is very balanced. This means that when it is the root, the subtree sizes of all nodes directly connected to it must be the same.
As the head of the kindergarten's information group, Ysuperman gives you a tree, and you need to find all nodes that can potentially serve as the root within 1 second.
Input Format
The first line contains a positive integer \( n \), representing the number of nodes in the tree.
The next \( n-1 \) lines each contain two positive integers \( u_i \) and \( v_i \), indicating an edge directly connecting nodes \( u_i \) and \( v_i \) in the tree. Each edge is guaranteed to be listed only once.
Output Format
A list of up to \( n \) integers in ascending order, separated by spaces, representing each node that can potentially serve as the root.
Sample Input and Output
Sample Input #1
2
1 2
Sample Output #1
1 2
Sample Input #2
4
1 2
2 3
3 4
Sample Output #2
1 4
Sample Input #3
9
1 2
1 3
4 1
5 1
1 6
1 9
8 1
1 7
Sample Output #3
1 2 3 4 5 6 7 8 9
Notes
Sample Explanation
Sample Explanation #1
When node 1 is the root, the nodes directly connected to it are {2}, and since there is only one, their sizes are the same.
When node 2 is the root, the nodes directly connected to it are {1}, and since there is only one, their sizes are the same.
Thus, the answer is 1, 2.
Sample Explanation #2
When node 1 is the root, the nodes directly connected to it are {2}, and since there is only one, their sizes are the same.
When node 2 is the root, the nodes directly connected to it are {1, 3}, and their subtree sizes are {1, 2}, which are not the same.
When node 3 is the root, the nodes directly connected to it are {2, 4}, and their subtree sizes are {2, 1}, which are not the same.
When node 4 is the root, the nodes directly connected to it are {3}, and since there is only one, their sizes are the same.
Thus, the answer is 1, 4.
Data Range
This problem uses batched testing.
| Subtask | \( n \) | Score |
|---|---|---|
| 1 | \(\le 5000\) | 40 |
| 2 | \(\le 10^6\) | 60 |
For 100% of the data, \( 1 \le n \le 10^6 \).
Hints
Due to the large amount of input and output, you may need to use fast input/output methods.
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分