#P6591. [YsOI2020] 植树

[YsOI2020] 植树

Background

In response to the call, Ysuperman decides to plant trees in a kindergarten.

Problem Description

Ysuperman has an unrooted tree TT with nn nodes. If you do not know what a tree is, they are happy to tell you that a tree is an undirected connected graph without cycles.

Since the tree is unrooted, there is no way to “plant” it. After studying gardening for a long time, Ysuperman finds that for a node to be able to serve as the root, it must be very balanced. This means that when it is chosen as the root, among the nodes directly connected to it, the sizes of their subtrees are all the same.

As the leader of the kindergarten IT group, given a tree by Ysuperman, can you find all nodes that can possibly be the root within 1s1\text{s}?

Input Format

The first line contains a positive integer nn, the number of nodes in the tree.

The next n1n-1 lines each contain two positive integers ui,viu_i, v_i, indicating that there is an edge directly connecting uiu_i and viv_i in the tree. It is guaranteed that each edge is given exactly once.

Output Format

Output at most nn integers in increasing order, separated by spaces, representing every node that can possibly be the root.

2
1 2

1 2 
4
1 2
2 3
3 4

1 4 

9
1 2
1 3
4 1
5 1
1 6
1 9
8 1
1 7

1 2 3 4 5 6 7 8 9 

Hint

Sample Explanation

Sample Explanation 11.

When rooted at 11, the nodes directly connected to 11 are {2}\{2\}. Since there is only one, all subtree sizes are the same.

When rooted at 22, the nodes directly connected to 22 are {1}\{1\}. Since there is only one, all subtree sizes are the same.

So the answer is 1,21, 2.

Sample Explanation 22

When rooted at 11, the nodes directly connected to 11 are {2}\{2\}. Since there is only one, all subtree sizes are the same.

When rooted at 22, the nodes directly connected to 22 are {1,3}\{1, 3\}. Their subtree sizes are {1,2}\{1, 2\}, which are not the same.

When rooted at 33, the nodes directly connected to 33 are {2,4}\{2, 4\}. Their subtree sizes are {2,1}\{2, 1\}, which are not the same.

When rooted at 44, the nodes directly connected to 44 are {3}\{3\}. Since there is only one, all subtree sizes are the same.

So the answer is 1,41, 4.


Constraints

This problem uses bundled testdata.

subtask\rm{subtask} nn Score
11 5000\le 5000 4040
22 106\le 10^6 6060

For 100%100\% of the data, 1n1061 \le n \le 10^6.


Hint

Since the input and output sizes are large, you may need fast I/O.

Translated by ChatGPT 5