The Centroid
The centroid of a tree is a node whose removal leaves all remaining components with at most N/2 nodes. Print the centroid with the smallest index (there may be one or two centroids).
Input: the first line has N; the next N−1 lines have the edges.
Output: the smallest-indexed centroid.
Constraints:
- 1 ≤ N ≤ 105
Sample Tests
Input 1
4 1 2 2 3 2 4
Output 1
2
Input 2
1
Output 2
1
Discussion (opens after you solve — no spoilers before)
No comments yet.