Root paths

2.7

6 votes
Graphs, Algorithms, Depth First Search
Problem

Alice and Bob play a game. The game starts with a graph that contains n nodes and no edges. You are required to process q queries of two types:

  • Add a directed edge from u[i] to v[i].
  • Print x[i] if there is a directed path from vertex 1 to vertex x[i]. Otherwise, print 0.

Input format

  • The first line contains n, q, and t (1n, q106, 0t1)(1n, q106, 0t1) denoting the number of nodes, the number of queries, and a constant number t.
  • Next q lines contain the queries, each with one of the following formats:
    1. Queries of the first type: 1 a[i] b[i] (0a[i],b[i]2109)
    2. Queries of the second type: 2 a[i] (0a[i]2109).

Nodes u[i] and v[i] for queries of the first type and node x[i] for queries of the second type are generated as follows:

u[i]=(a[i](tlastans)) mod n+1, v[i] = (b[i](tlastans))mod n+1 u[i]=(a[i](tlastans)) mod n+1, v[i]=(b[i](tlastans)) mod n+1

x[i]=(a[i](tlastans)) mod n+1 x[i]=(a[i](tlastans)) modn+1

where lastans denotes the answer for the last query of the second type (initially lastans=0)

Output format

For each query of type 2, print one integer denoting the answer to the query.

Sample Input
5 9 1
2 0
2 0
1 0 1
2 1
1 0 1
1 0 1
2 1
1 1 2
2 3
Sample Output
1
0
2
0
4
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

generated Queries:

2 1 where node 1 is reachable 
2 2 where node 2 is not reachable
1 1 2 add edge from 1 to 2
2 2 where node 2 is reachable 
1 3 4  add edge from 3 to 4
1 3 4  add edge from 3 to 4
2 4 where node 4 is not reachable
1 2 3  add edge from 2 to 3
2 4 where node 4 is reachable

Editor Image

?