A field team maintains a communications network of n outposts, numbered 1 through n. The network was built as a star: exactly one outpost — the hub — has a direct radio link to every other outpost, and there are no other links in the network. In total there are exactly n - 1 links.
You are given the n - 1 links in some order, each listed as an unordered pair of outpost labels; the two endpoints of a link may appear in either order, and the links themselves may be listed in any order. Determine the label of the hub outpost — the one outpost that appears in every single link.
The first line contains a single integer n.
Each of the next n - 1 lines contains two integers u and v — the labels of the two outposts joined by that link.
Print a single integer: the label of the hub outpost.
n - 1 other outposts, and no other links.Example 1
Input
3 1 2 2 3
Expected
2
Explanation
The two links are (1,2) and (2,3). Outpost 2 appears in both links, so it must be the hub connecting to every other outpost (1 and 3). The answer is 2.
Example 2
Input
5 1 5 2 5 3 5 4 5
Expected
5
Explanation
All four links list outpost 5 as one endpoint, so outpost 5 is directly connected to outposts 1, 2, 3, and 4 — every other outpost in the network. Outpost 5 is therefore the hub.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →