CF1969A Two Friends

题目描述

Monocarp 想要举办一个聚会。他有 $n$ 个朋友,并且他想要让他们之中的至少 $2$ 人来参加聚会。 第 $i$ 个朋友的最好的朋友是 $p_i$。每一个 $p_i$ 都是不一样的,并且对于所有的 $i \in [1, n]$,$p_i \neq i$。 Monocarp 可以给朋友们发送邀请。如果第 $i$ 个朋友和第 $p_i$ 个朋友都收到了邀请(注意第 $p_i$ 个朋友不一定真的要去参加聚会),那么第 $i$ 个朋友会去参加聚会。每份邀请都会发送给其中一位朋友。 举个例子,如果 $p = [3,1,2,5,4]$,并且 Monocarp 给朋友 $[1, 2, 4, 5]$ 发邀请,那么朋友 $[2, 4,5]$ 会去参加聚会。朋友 $1$ 不会去参加聚会因为他最好的朋友没有收到邀请;朋友 $3$ 不会去参加聚会因为他没有受到邀请。 求 Monocarp 最少需要发出的邀请数以让至少 $2$ 个朋友来参加聚会。

输入格式

第一行包含一个整数 $t$($1 \le t \le 5000$),表示数据组数。 每一组数据包含两行: - 第一行包含一个整数 $n$($2\le n \le 50$),表示朋友数; - 第二行包含 $n$ 个整数 $p_1, p_2, \dotsc, p_n$($1 \le p_i \le n$;$p_i \neq i$;所有的 $p_i$ 都不一样)。

输出格式

输出一个整数,为 Monocarp 最少需要发出的邀请数。 ### 样例解释 在第一组数据中,Monocarp 可以给朋友 $4$ 和 $5$ 发邀请。他们两人都会来参加聚会因为他们是对方最好的朋友,并且他们都收到了邀请。 在第二组数据中,例如,Monocarp 可以给朋友 $1,2$ 和 $3$ 发邀请。然后朋友 $1$ 和 $2$ 会出席:朋友 $1$ 和他最好的朋友 $2$ 都收到了邀请,朋友 $2$ 和他最好的朋友 $3$ 都收到了邀请。朋友 $3$ 不会出席因为他最好的朋友 $4$ 没有收到邀请。只给少于 $3$ 个朋友发邀请函还至少有 $2$ 个朋友来参加聚会是不可能的。 在第三组数据中,Monocarp 可以给朋友 $1$ 和 $2$ 都发邀请,然后他们两个都会出席。

说明/提示

In the first testcase, Monocarp can send invitations to friends $ 4 $ and $ 5 $ . Both of them will come to the party since they are each other's best friends, and both of them have invitations. In the second testcase, Monocarp can send invitations to friends $ 1, 2 $ and $ 3 $ , for example. Then friends $ 1 $ and $ 2 $ will attend: friend $ 1 $ and his best friend $ 2 $ have invitations, friend $ 2 $ and his best friend $ 3 $ have invitations. Friend $ 3 $ won't attend since his friend $ 4 $ doesn't have an invitation. It's impossible to send invitations to fewer than $ 3 $ friends in such a way that at least $ 2 $ come. In the third testcase, Monocarp can send invitations to both friends $ 1 $ and $ 2 $ , and both of them will attend.