#C3537. Optimal Capital Selection

    ID: 46975 Type: Default 1000ms 256MiB

Optimal Capital Selection

Optimal Capital Selection

You are given a tree with n cities (numbered from 1 to n) and n-1 bidirectional roads connecting these cities. The graph is guaranteed to be a tree.

The task is to choose a city as the capital such that the maximum distance from the capital to any other city is minimized. In other words, if for a given city i we denote by d(i, j) the distance from city i to city j, then we wish to choose a city c that minimizes: \[ \max_{1 \le j \le n} d(c, j) \]

If there are multiple optimal candidates, choose the one that first achieves this minimum maximum distance when examined in increasing order of city number.

inputFormat

The input is given from stdin and has the following format:

  • The first line contains a single integer n, denoting the number of cities.
  • The next n-1 lines each contain two integers u and v, indicating that there is a bidirectional road connecting cities u and v.

outputFormat

Print a single integer from stdout which is the number of the city chosen as the capital.

## sample
4
1 2
2 3
2 4
2

</p>