The stations and transitions form a graph with vertices and n - 1 edges in which any two vertices are connected by a path if directions are ignored. Hence, without orientation, the graph is a tree. Each conveyor belt fixes the direction of its edge, so we obtain a directed tree.
A sink is a vertex with no outgoing edges, that is, a vertex with \mathit{out}[i] = 0.
Claim. Station can be reached from every other station if and only if is the only sink of the graph.
Necessity. Suppose can be reached from every vertex. Then every vertex j \ne i has an outgoing edge, otherwise it is impossible to leave at all. Assume there is an edge i \to k leaving . Vertex must also reach , but in a tree the only path between and is the edge itself, and it is directed from to . This is a contradiction, so \mathit{out}[i] = 0 and is the only sink.
Sufficiency. Suppose vertex is the only sink. Take any vertex and move from it along outgoing edges as long as possible. The vertices on this walk never repeat: a tree has no cycles, and an edge cannot be traversed backwards. Therefore, after at most n - 1 steps the walk stops at a vertex with no outgoing edges. The only such vertex is , so is reachable from .
The same argument shows that the graph always has at least one sink. Therefore, it is enough to compute the number of outgoing edges for each vertex and the number of sinks :
if \mathit{cnt} = 1, the answer is the only sink;
if \mathit{cnt} \ge 2, the required station does not exist, and the answer is .
Since at most one station can satisfy the condition, the requirement to print the smallest one holds automatically. For n = 1 there are no edges, the only station is a sink, and the answer is . The number of incoming edges is not needed for the answer.
Complexity. Each edge is processed once, and then all vertices are scanned once. The total complexity is .
Example
In the sample, the belts move along the edges 1 \to 2 and 3 \to 2. We get \mathit{out}[1] = 1, \mathit{out}[2] = 0, \mathit{out}[3] = 1. There is exactly one sink — vertex , \mathit{cnt} = 1. Station can be reached both from station and from station . The answer is .
Let us reverse the orientation of the edges: let the belts move 2 \to 1 and 2 \to 3. Then \mathit{out}[1] = 0, \mathit{out}[2] = 2, \mathit{out}[3] = 0, and there are two sinks — vertices and . Station cannot be reached from sink , and station cannot be reached from sink , so the answer is .
Algorithm Implementation
Declare the working arrays: and store the number of incoming and outgoing edges of vertex .
#define MAX 101
int in[MAX], out[MAX];Read the number of stations and the n - 1 directed edges.
scanf("%d", &n);
for (i = 0; i < n - 1; i++)
{
scanf("%d %d", &a, &b);For the edge a \to b, increase by the number of outgoing edges of vertex and the number of incoming edges of vertex .
out[a]++; in[b]++;
}Iterate over the vertices in increasing order of their numbers. The variable counts the sinks — vertices with \mathit{out}[i] = 0, and the variable stores the number of a sink.
cnt = 0;
for (i = 1; i <= n; i++)
if (out[i] == 0)
{
cnt++;
pos = i;
}If \mathit{cnt} = 1, the sink is unique and can be reached from every station, so print its number . Otherwise there are several sinks, the required station does not exist, and we print .
if (cnt != 1) printf("-1\n");
else printf("%d\n", pos);