atcoder#ABC295G. [ABC295G] Minimum Reachable City
[ABC295G] Minimum Reachable City
Score : points
Problem Statement
We have a directed graph with vertices numbered to . It has edges. The -th edge goes from vertex to vertex .
We have another directed graph with vertices numbered to . Initially, equals . Process queries on in the order they are given. There are two kinds of queries as follows.
1 u v
: Add an edge to that goes from vertex to vertex . It is guaranteed that the following conditions are satisfied.- .- On , vertex is reachable from vertex via some edges.
- .
- On , vertex is reachable from vertex via some edges.
2 x
: Print the smallest vertex number of a vertex reachable from vertex via some edges on (including vertex ).
Constraints
- For each query in the first format:- .
- .
- On , vertex is reachable from vertex via some edges.
- .
- .
- On , vertex is reachable from vertex via some edges.
- For each query in the second format, .
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Here, denotes the -th query and is in one of the following formats:
Output
Print lines, where is the number of queries in the second format. The -th line should contain the answer to the -th query in the second format.
5
1 2 3 3
5
2 4
1 4 2
2 4
1 5 1
2 4
4
2
1
- At the time of the first query, only vertex is reachable from vertex via some edges on .
- At the time of the third query, vertices , , , and are reachable from vertex via some edges on .
- At the time of the fifth query, vertices , , , , and are reachable from vertex via some edges on .
7
1 1 2 2 3 3
10
2 5
1 5 2
2 5
1 2 1
1 7 1
1 6 3
2 5
2 6
2 1
1 7 1
5
2
1
1
1