#P6706. [COCI 2010/2011 #7] KUGLICE
[COCI 2010/2011 #7] KUGLICE
Background
Mirko and Slavko like playing marbles. On an exciting Friday, Mirko invented a marble game and wanted to show it to Slavko.
In the game, Mirko builds a directed graph where each vertex has outdegree at most . Then he places a marble on one vertex. As long as the marble is at some vertex , it moves to the adjacent vertex connected by the outgoing edge (if there is no such edge, it stays). The marble keeps moving until it reaches a vertex with outdegree . It is also possible that the marble travels around the graph forever because there is no vertex with outdegree . To make sure Slavko understands the rules, Mirko asks a series of queries.
The query types are:
-
1 X: Unless the marble gets stuck in a cycle, find the index of the final stopping vertex after placing the marble at . -
2 X: Delete the outgoing edge of vertex . (It is guaranteed to exist.)
Note that the queries are ordered.
Problem Description
You are given a directed graph where each vertex has outdegree or .
There are the following queries:
-
1 X: Given the marble start vertex , find where the marble stops. -
2 X: Delete the outgoing edge of vertex . (It is guaranteed to exist.)
Marble movement rules:
Starting from the initial vertex, follow outgoing edges until reaching a vertex whose outdegree is .
Input Format
The first line contains a positive integer , the number of vertices in the graph.
The second line contains positive integers. The -th number is the index of the vertex that the outgoing edge of points to; if it is , then the edge does not exist.
The next line contains a positive integer , the number of queries.
The next lines each contain one query in one of the formats above.
Output Format
For each query of type , output the index of the vertex where the marble stops, in order, one per line.
If the marble never stops, output CIKLUS.
3
2 3 1
7
1 1
1 2
2 1
1 2
1 1
2 2
1 2
CIKLUS
CIKLUS
1
1
2
5
0 3 5 3 4
6
1 1
1 2
2 4
1 2
2 3
1 2
1
CIKLUS
4
3
Hint
Constraints
For of the testdata, .
Notes
This problem is worth points.
Translated from COCI2010-2011 CONTEST #7 T5 KUGLICE。
Translated by ChatGPT 5