No.3217 Shiki no Shiki
問題文最終更新日: 2025-07-28 22:50:09
English (AI translation)
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence.
Back to the original
yukicoder contest 476
(順位表)
の他の問題:
Story
"Today I'm fresh from possessing someone, piping hot!"
Problem Statement
There are $N$ yokai. When $P_i\neq 0$, yokai $i(1\leq i\leq N)$ uses yokai $P_i$ as a shikigami; when $P_i=0$, it has no shikigami.
Find how many yokai are a shikigami of a shikigami.
Here, yokai $i$ is called a shikigami of a shikigami if the following condition is satisfied.
- There exists a pair $(p, q)$ satisfying all of the following:
- $1\leq p, q\leq N$ and $p\neq q$
- Yokai $p$ uses yokai $i$ as a shikigami
- Yokai $q$ uses yokai $p$ as a shikigami
- There is no yokai that uses yokai $q$ as a shikigami
Input
$N$ $P_1\ P_2 \cdots\ P_N$
- $1\leq N\leq 10^5$
- $0\leq P_i \leq N$
- $P_i\neq i$
- All inputs are integers
Output
Let $n$ be the number of yokai satisfying the condition, and output it on $1$ line in the following format.
$n$
Print a newline at the end.
Sample
Sample 1
Input
3 2 3 0
Output
1
Only the yokai numbered $3$ satisfies the condition.
Sample 2
Input
2 0 1
Output
0
No yokai satisfies the condition.
Sample 3
Input
6 2 3 0 5 6 0
Output
2
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
nauclhlt