#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 11. Then he places a marble on one vertex. As long as the marble is at some vertex XX, 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 00. It is also possible that the marble travels around the graph forever because there is no vertex with outdegree 00. To make sure Slavko understands the rules, Mirko asks a series of queries.

The query types are:

  1. 1 X: Unless the marble gets stuck in a cycle, find the index of the final stopping vertex after placing the marble at XX.

  2. 2 X: Delete the outgoing edge of vertex XX. (It is guaranteed to exist.)

Note that the queries are ordered.

Problem Description

You are given a directed graph where each vertex has outdegree 00 or 11.

There are the following queries:

  1. 1 X: Given the marble start vertex XX, find where the marble stops.

  2. 2 X: Delete the outgoing edge of vertex XX. (It is guaranteed to exist.)

Marble movement rules:

Starting from the initial vertex, follow outgoing edges until reaching a vertex whose outdegree is 00.

Input Format

The first line contains a positive integer nn, the number of vertices in the graph.

The second line contains nn positive integers. The ii-th number is the index of the vertex that the outgoing edge of ii points to; if it is 00, then the edge does not exist.

The next line contains a positive integer QQ, the number of queries.

The next QQ lines each contain one query in one of the formats above.

Output Format

For each query of type 11, 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 100%100\% of the testdata, 1n,Q3×1051 \le n,Q \le 3 \times 10^5.

Notes

This problem is worth 120120 points.

Translated from COCI2010-2011 CONTEST #7 T5 KUGLICE。

Translated by ChatGPT 5