#K40207. Find the Center of a Tree

    ID: 26591 Type: Default 1000ms 256MiB

Find the Center of a Tree

Find the Center of a Tree

You are given an undirected tree with (n) nodes and (n-1) edges. A tree is a connected acyclic graph. The task is to find one of the center nodes of the tree. A center of a tree is defined as a node that minimizes the maximum distance to all other nodes. One standard way to find a center is to repeatedly remove all leaf nodes (nodes with degree 1) until at most two nodes remain. In this problem, you are required to output the 1-based index of one of the remaining center nodes.

Mathematically, if the tree is represented as (T=(V,E)), where (V) is the set of vertices and (E) the set of edges, then by removing the leaves layer by layer, you obtain a set (C \subseteq V) with (|C| \le 2). Any node (c \in C) is considered a valid center.

inputFormat

The input is provided via standard input (stdin). The first line contains a single integer (n) (the number of nodes). The following (n-1) lines each contain two space-separated integers (u) and (v) indicating an undirected edge between nodes (u) and (v).

outputFormat

Print a single integer to standard output (stdout) which is the 1-based index of one of the center nodes of the tree.## sample

5
1 2
2 3
3 4
4 5
3

</p>