#P1700. Finding the Universal Sink Station
Finding the Universal Sink Station
Finding the Universal Sink Station
Farmer John's milk business is booming! His milk processing factory has \(N\) processing stations numbered \(1,2,\ldots,N\) (where \(1 \le N \le 100\)) and \(N-1\) channels connecting them such that the underlying undirected graph is a tree (i.e. there is a unique simple path between any two stations). In an effort to innovate, Farmer John installed conveyor belts on each channel. Unfortunately, he later discovered that each belt only works in one direction. As a result, the channels are now one-way and it is no longer guaranteed that every station can reach every other station.
However, Farmer John thinks things might not be entirely lost. He wishes to know whether there exists at least one station \(i\) such that every other station can reach \(i\) (possibly via some intermediate stations). In other words, is there a station such that for every other station \(j\) (\(j \neq i\)), there exists a directed path from \(j\) to \(i\)? If there are multiple candidates, you may output any one of them.
If no such station exists, output \(-1\).
inputFormat
The input begins with an integer \(N\) representing the number of processing stations. \(N\) is followed by \(N-1\) lines, each containing two integers \(u\) and \(v\) indicating a one-way channel that goes from station \(u\) to station \(v\).
Constraints: \(1 \le N \le 100\) and the underlying undirected graph is a tree.
outputFormat
Output a single integer which is the index of a processing station that can be reached from every other station. If there is no such station, output \(-1\).
sample
3
1 2
3 2
2