Find it!

AtCoder
IDabc311_c
Time2000ms
Memory256MB
Difficulty
There is a directed graph with $N$ vertices and $N$ edges. The $i$\-th edge goes from vertex $i$ to vertex $A_i$. (The constraints guarantee that $i \neq A_i$.) Find a directed cycle without the same vertex appearing multiple times. It can be shown that a solution exists under the constraints of this problem. #### Notes The sequence of vertices $B = (B_1, B_2, \dots, B_M)$ is called a directed cycle when all of the following conditions are satisfied: * $M \geq 2$ * The edge from vertex $B_i$ to vertex $B_{i+1}$ exists. $(1 \leq i \leq M-1)$ * The edge from vertex $B_M$ to vertex $B_1$ exists. * If $i \neq j$, then $B_i \neq B_j$. ## Constraints * All input values are integers. * $2 \le N \le 2 \times 10^5$ * $1 \le A_i \le N$ * $A_i \neq i$ ## Input The input is given from Standard Input in the following format: $N$ $A_1$ $A_2$ $\dots$ $A_N$ [samples]
Samples
Input #1
7
6 7 2 1 3 4 5
Output #1
4
7 5 3 2

$7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7$ is indeed a directed cycle.
Here is the graph corresponding to this input:
![image](https://img.atcoder.jp/abc311/0ab396c54e276edb27de02ad3b20cf7f.png)
Here are other acceptable outputs:

4
2 7 5 3

3
4 1 6

Note that the graph may not be connected.
Input #2
2
2 1
Output #2
2
1 2

This case contains both of the edges $1 \rightarrow 2$ and $2 \rightarrow 1$.  
In this case, $1 \rightarrow 2 \rightarrow 1$ is indeed a directed cycle.
Here is the graph corresponding to this input, where $1 \leftrightarrow 2$ represents the existence of both $1 \rightarrow 2$ and $2 \rightarrow 1$:
![image](https://img.atcoder.jp/abc311/97e8121c1e36bbcefae0b68e1b8fbfd2.png)
Input #3
8
3 7 4 7 3 3 8 2
Output #3
3
2 7 8

Here is the graph corresponding to this input:
![image](https://img.atcoder.jp/abc311/c31a7153e0ca36e8c0e1dd4c7bfe5329.png)
API Response (JSON)
{
  "problem": {
    "name": "Find it!",
    "description": {
      "content": "There is a directed graph with $N$ vertices and $N$ edges.   The $i$\\-th edge goes from vertex $i$ to vertex $A_i$. (The constraints guarantee that $i \\neq A_i$.)   Find a directed cycle without the s",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc311_c"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "There is a directed graph with $N$ vertices and $N$ edges.  \nThe $i$\\-th edge goes from vertex $i$ to vertex $A_i$. (The constraints guarantee that $i \\neq A_i$.)  \nFind a directed cycle without the s...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments