atcoder#NIKKEI2019QUALD. Restore the Tree
Restore the Tree
Score : points
Problem Statement
There is a rooted tree (see Notes) with vertices numbered to . Each of the vertices, except the root, has a directed edge coming from its parent. Note that the root may not be Vertex .
Takahashi has added new directed edges to this graph. Each of these edges, , extends from some vertex to its descendant .
You are given the directed graph with vertices and edges after Takahashi added edges. More specifically, you are given pairs of integers, , which represent that the -th edge extends from Vertex to Vertex .
Restore the original rooted tree.
Notes
For "tree" and other related terms in graph theory, see the article in Wikipedia
, for example.
Constraints
- If , .
- The graph in input can be obtained by adding edges satisfying the condition in the problem statement to a rooted tree with vertices.
Input
Input is given from Standard Input in the following format:
Output
Print lines.
In the -th line, print 0
if Vertex is the root of the original tree, and otherwise print the integer representing the parent of Vertex in the original tree.
Note that it can be shown that the original tree is uniquely determined.
3 1
1 2
1 3
2 3
0
1
2
The graph in this input is shown below:
It can be seen that this graph is obtained by adding the edge to the rooted tree .
6 3
2 1
2 3
4 1
4 2
6 1
2 6
4 6
6 5
6
4
2
0
6
2