atcoder#ABC296E. [ABC296E] Transition Game

[ABC296E] Transition Game

Score : 500500 points

Problem Statement

You are given a sequence of NN numbers: A=(A1,A2,,AN)A=(A_1,A_2,\ldots,A_N). Here, each AiA_i (1iN)(1\leq i\leq N) satisfies 1AiN1\leq A_i \leq N.

Takahashi and Aoki will play NN rounds of a game. For each i=1,2,,Ni=1,2,\ldots,N, the ii-th game will be played as follows.

  1. Aoki specifies a positive integer KiK_i.
  2. After knowing KiK_i Aoki has specified, Takahashi chooses an integer SiS_i between 11 and NN, inclusive, and writes it on a blackboard.
  3. Repeat the following KiK_i times.
    • Replace the integer xx written on the blackboard with AxA_x.

If ii is written on the blackboard after the KiK_i iterations, Takahashi wins the ii-th round; otherwise, Aoki wins. Here, KiK_i and SiS_i can be chosen independently for each i=1,2,,Ni=1,2,\ldots,N.

Find the number of rounds Takahashi wins if both players play optimally to win.

Constraints

  • 1N2×1051\leq N\leq 2\times 10^5
  • 1AiN1\leq A_i\leq N
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

Output

Find the number of rounds Takahashi wins if both players play optimally to win.

3
2 2 3
2

In the first round, if Aoki specifies K1=2K_1=2, Takahashi cannot win whichever option he chooses for S1S_1: 11, 22, or 33.

For example, if Takahashi writes S1=1S_1=1 on the initial blackboard, the two operations will change this number as follows: 12(=A1)1\to 2(=A_1), 22(=A2)2\to 2(=A_2). The final number on the blackboard will be 2(1)2(\neq 1), so Aoki wins.

On the other hand, in the second and third rounds, Takahashi can win by writing 22 and 33, respectively, on the initial blackboard, whatever value Aoki specifies as KiK_i.

Therefore, if both players play optimally to win, Takashi wins two rounds: the second and the third. Thus, you should print 22.

2
2 1
2

In the first round, Takahashi can win by writing 22 on the initial blackboard if K1K_1 specified by Aoki is odd, and 11 if it is even.

Similarly, there is a way for Takahashi to win the second round. Thus, Takahashi can win both rounds: the answer is 22.