#P4089. [USACO17DEC] The Bovine Shuffle S

[USACO17DEC] The Bovine Shuffle S

题目描述

Convinced that happy cows generate more milk, Farmer John has installed a giant disco ball in his barn and plans to teach his cows to dance!

Looking up popular cow dances, Farmer John decides to teach his cows the "Bovine Shuffle". The Bovine Shuffle consists of his NN cows (1N100,0001 \leq N \leq 100,000) lining up in a row in some order, then performing successive "shuffles", each of which potentially re-orders the cows. To make it easier for his cows to locate themselves, Farmer John marks the locations for his line of cows with positions 1N1 \ldots N, so the first cow in the lineup will be in position 1, the next in position 2, and so on, up to position NN.

A shuffle is described with NN numbers, a1aNa_1 \ldots a_N, where a cow in position ii moves to position aia_i during the shuffle (and so, each aia_i is in the range 1N1 \ldots N). Every cow moves to its new location during the shuffle. Unfortunately, all the aia_i's are not necessarily distinct, so multiple cows might try to move to the same position during a shuffle, after which they will move together for all remaining shuffles.

Farmer John notices that some positions in his lineup contain cows in them no matter how many shuffles take place. Please help him count the number of such positions.

输入格式

The first line of input contains NN, the number of cows. The next line contains the NN integers a1aNa_1 \ldots a_N.

输出格式

Please output the number of positions that will always contain cows, no matter how many shuffles take place.

题目大意

题目描述

Farmer John 坚信快乐的奶牛能产更多的奶,因此他在谷仓里安装了一个巨大的迪斯科球,并计划教他的奶牛跳舞!

在查阅了流行的奶牛舞蹈后,Farmer John 决定教他的奶牛“Bovine Shuffle”。Bovine Shuffle 包括他的 NN 头奶牛(1N100,0001 \leq N \leq 100,000)以某种顺序排成一行,然后进行连续的“洗牌”,每次洗牌可能会重新排列奶牛的顺序。为了让奶牛更容易找到自己的位置,Farmer John 为他的奶牛队伍标记了位置 1N1 \ldots N,因此队伍中的第一头奶牛位于位置 1,第二头位于位置 2,依此类推,直到位置 NN

一次洗牌由 NN 个数字 a1aNa_1 \ldots a_N 描述,其中位于位置 ii 的奶牛在洗牌期间移动到位置 aia_i(因此,每个 aia_i 都在 1N1 \ldots N 范围内)。每头奶牛在洗牌期间都会移动到它的新位置。不幸的是,所有的 aia_i 不一定互不相同,因此多只奶牛可能会在洗牌期间尝试移动到同一位置,之后它们将在所有剩余的洗牌中一起移动。

Farmer John 注意到,无论进行多少次洗牌,他的队伍中某些位置始终会有奶牛。请帮助他计算这样的位置数量。

输入格式

输入的第一行包含 NN,表示奶牛的数量。第二行包含 NN 个整数 a1aNa_1 \ldots a_N

输出格式

请输出无论进行多少次洗牌,始终会有奶牛的位置数量。

4
3 2 1 3
3