Special graph
Time limit1sMemory limit64 MB
Edge deletions hit a functional graph while queries ask for the directed distance from a to b along the single outgoing walk.
- Level
Hard8 of 10
- Topics
- Graph, Tree, Union-find
- Solved
- No attempts yet
Problem
You are given a directed graph with vertices. The graph is special because every vertex has at most one outgoing edge. Process two kinds of queries.
1 a: delete the edge going out of vertex . This edge is guaranteed to exist.2 a b: print the length of the shortest path from vertex to vertex . If there is no such path, print -1.
The length of a path is the number of edges on it. Since a vertex has at most one outgoing edge, only one walk starts at , and the answer is the number of edges you follow along that walk until you first reach . If and are the same, the answer is 0.
Input
The first line contains the number of vertices ().
The second line contains integers (). The value means there is an edge from vertex to vertex . If , vertex has no outgoing edge.
The third line contains the number of queries ().
Each of the next lines contains one query in the format described above, with .
Output
For each 2 a b query, print one answer per line, in the order the queries are given.