#abc228b. B - Takahashi's Secret

B - Takahashi's Secret

Score : 200200 points

问题描述

Takahashi 有 NN 个朋友。他们的昵称分别是:朋友 11、朋友 22\ldots、朋友 NN

有一天,Takahashi 不小心让他的一个朋友——朋友 XX 知道了他的一个羞耻的秘密。
对于每个 i=1,2,,Ni = 1, 2, \ldots, N,当朋友 ii 知道了这个秘密后,若朋友 AiA_i 还未知道这个秘密,他/她将会把这个秘密分享给朋友 AiA_i

最终会有多少个 Takahashi 的朋友知道这个秘密?

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

Takahashi has NN friends. They have nicknames: Friend 11, Friend 22, \ldots, Friend NN.

One day, Takahashi accidentally let one of his friends, Friend XX, learn his shameful secret.
For each i=1,2,,Ni = 1, 2, \ldots, N, when Friend ii learns the secret, he/she will share it with Friend AiA_i, if Friend AiA_i has not already learned it.

How many of Takahashi's friends will learn the secret in the end?

Constraints

  • 2N1052 \leq N \leq 10^5
  • 1XN1 \leq X \leq N
  • 1AiN1 \leq A_i \leq N
  • AiiA_i \neq i
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN XX

A1A_1 A2A_2 \cdots ANA_N

Output

Print the answer.

Sample Input 1

4 2
3 1 1 2

Sample Output 1

3

Takahashi's secret will be learned by Friend 11, Friend 22, and Friend 33, as follows.

  • One day, Takahashi let Friend 22 learn the secret.
  • Friend 22 shares it with Friend 11.
  • Friend 11 shares it with Friend 33.

In the end, three of his friends learn the secret, so we print 33.

Sample Input 2

20 12
7 11 10 1 7 20 14 2 17 3 2 5 19 20 8 14 18 2 10 10

Sample Output 2

7

update @ 2024/3/10 09:58:16