#P6591. Balanced Root in a Tree
Balanced Root in a Tree
Balanced Root in a Tree
Ysuperman gives you an unrooted tree \(T\) with \(n\) nodes (i.e. a connected acyclic undirected graph). A node \(x\) can be chosen as a root if and only if when \(x\) is considered as the root, all subtrees rooted at its directly connected neighbors have the same size. Formally, if \(x\) has degree \(d\) and each subtree (obtained by a DFS from one neighbor while excluding \(x\)) has size \(s\), then it must hold that \(s = \frac{n-1}{d}\) (when \(d>0\)); note that if \(d=0\) (i.e. the tree has only one node), \(x\) is trivially balanced.
Your task is to find all nodes that can be chosen as the root under this condition. The answer should list the valid nodes in increasing order.
inputFormat
The first line contains an integer \(n\) (the number of nodes). Each of the next \(n-1\) lines contains two integers \(u\) and \(v\) denoting an edge between nodes \(u\) and \(v\). It is guaranteed that the input forms a tree.
outputFormat
Output all candidate nodes (in increasing order) separated by a single space.
sample
1
1