#P1394. Reservoir Village
Reservoir Village
Reservoir Village
A mysterious small country lies atop the green mountains of the south. It is said that only during twilight, when the dazzling afterglow of the setting sun pierces the thin veil of mist, can a select few witness the beauty of n villages on the hill. In this ancient land, there exist m venerable hub pipes connecting pairs of villages, channels that have, for centuries, provided water flow between them.
Among the n villages, only one houses a reservoir. Water, as everyone knows, flows downhill. This ensures that starting from the reservoir, water can propagate along these pipes and reach every other village. Your task is to determine which village should host the reservoir so that every village can receive water.
Mathematically, you are given a directed graph with $$n$$ nodes (villages) and $$m$$ directed edges (pipes). It is guaranteed that there is exactly one node that has no incoming edge and that all other nodes are reachable from this node. Find and output the index of this node.
inputFormat
The input consists of multiple lines. The first line contains two integers $$n$$ and $$m$$, representing the number of villages and the number of pipes respectively. Each of the next $$m$$ lines contains two integers $$u$$ and $$v$$, indicating that there is a pipe directing water from village $$u$$ to village $$v$$.
outputFormat
Output the index of the village that can serve as the reservoir.
sample
3 2
1 2
1 3
1
</p>