102964: [Atcoder]ABC296 E - Transition Game

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:3 Solved:0

Description

Score : $500$ points

Problem Statement

You are given a sequence of $N$ numbers: $A=(A_1,A_2,\ldots,A_N)$. Here, each $A_i$ $(1\leq i\leq N)$ satisfies $1\leq A_i \leq N$.

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

  1. Aoki specifies a positive integer $K_i$.
  2. After knowing $K_i$ Aoki has specified, Takahashi chooses an integer $S_i$ between $1$ and $N$, inclusive, and writes it on a blackboard.
  3. Repeat the following $K_i$ times.

    • Replace the integer $x$ written on the blackboard with $A_x$.

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

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

Constraints

  • $1\leq N\leq 2\times 10^5$
  • $1\leq A_i\leq N$
  • All values in the input are integers.

Input

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

$N$
$A_1$ $A_2$ $\ldots$ $A_N$

Output

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


Sample Input 1

3
2 2 3

Sample Output 1

2

In the first round, if Aoki specifies $K_1=2$, Takahashi cannot win whichever option he chooses for $S_1$: $1$, $2$, or $3$.

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

On the other hand, in the second and third rounds, Takahashi can win by writing $2$ and $3$, respectively, on the initial blackboard, whatever value Aoki specifies as $K_i$.

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


Sample Input 2

2
2 1

Sample Output 2

2

In the first round, Takahashi can win by writing $2$ on the initial blackboard if $K_1$ specified by Aoki is odd, and $1$ 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 $2$.

Input

题意翻译

给定 $n$ 个数 $A=(A_1,A_2,\cdots,A_n)$($1\leq A_i\leq n$),一共有 $n$ 轮游戏。 每一轮,先手选定 $k$,表示将要进行 $k$ 次操作。 后手选定一个数 $s(1\leq s\leq n)$,写在黑板上。一共进行 $k$ 次操作,设黑板上现在的数是 $x$,则每次操作都用 $a_x$ 替换成 $x$。 若第 $i$ 轮游戏后 $i$ 被写在黑板上,后手获胜,否则先手获胜。 问后手最多能赢多少轮。

Output

分数:500分

问题描述

给你一个包含$N$个数字的序列:$A=(A_1,A_2,\ldots,A_N)$。在这里,每个$A_i$ $(1\leq i\leq N)$满足$1\leq A_i \leq N$。

高桥和青木将进行$N$轮游戏。对于每个$i=1,2,\ldots,N$,第$i$轮游戏将按照以下方式进行。

  1. 青木指定一个正整数$K_i$。
  2. 在知道了青木指定的$K_i$后,高桥从1到$N$,包括两者在内,选择一个整数$S_i$并将其写在黑板上。
  3. 重复$K_i$次以下操作。

    • 将黑板上写的数字$x$替换为$A_x$。

如果经过$K_i$次迭代后黑板上写的是$i$,那么高桥赢得了第$i$轮游戏;否则,青木赢得比赛。

在这里,$K_i$和$S_i$可以在每一轮中独立选择,即对于每个$i=1,2,\ldots,N$。

如果两人都为了赢而优化地玩游戏,请找出高桥赢得的轮数。

约束条件

  • $1\leq N\leq 2\times 10^5$
  • $1\leq A_i\leq N$
  • 输入中的所有值都是整数。

输入

输入将从标准输入以以下格式给出:

$N$
$A_1$ $A_2$ $\ldots$ $A_N$

输出

如果两人都为了赢而优化地玩游戏,请找出高桥赢得的轮数。


样例输入1

3
2 2 3

样例输出1

2

在第一轮中,如果青木指定$K_1=2$,那么无论高桥选择哪个选项作为$S_1$:$1$、$2$或$3$,他都无法获胜。

例如,如果高桥在初始黑板上写下$S_1=1$,这两个操作将使这个数字如下变化:$1\to 2(=A_1)$,$2\to 2(=A_2)$。最终黑板上的数字将是$2(\neq 1)$,所以青木获胜。

另一方面,在第二轮和第三轮中,无论青木指定的$K_i$值为多少,高桥都可以通过在初始黑板上分别写下$2$和$3$来获胜。

因此,如果两人都为了赢而优化地玩游戏,高桥将赢得两轮:第二轮和第三轮。因此,你应该输出$2$。


样例输入2

2
2 1

样例输出2

2

在第一轮中,如果青木指定的$K_1$为奇数,那么高桥可以在初始黑板上写下$2$来获胜;如果$K_1$为偶数,则写下$1$来获胜。

同样地,高桥在第二轮中也有一种方式可以获胜。因此,高桥可以赢得两轮:答案是$2$。

HINT

有n个数,两人玩n次游戏,第一个人选择一个次数k;第二个人选择一个初始位置x;执行k次操作后,最终结果是i则后者赢,i为第几次游戏。 两人均使用最优策略,请问后缀你能赢多少次? 操作:将x改为a[x]

加入题单

算法标签: