欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1969A.Two Friends
Two Friends
Monocarp wants to throw a party. He has friends, and he wants to have at least of them at his party.
The -th friend's best friend is . All are distinct, and for every , .
Monocarp can send invitations to friends. The -th friend comes to the party if both the -th friend and the -th friend receive an invitation (note that the -th friend doesn't have to actually come to the party). Each invitation is sent to exactly one of the friends.
For example, if , and Monocarp sends invitations to the friends , then the friends will come to the party. The friend won't come since his best friend didn't receive an invitation; the friend won't come since he didn't receive an invitation.
Calculate the minimum number of invitations Monocarp has to send so that at least friends come to the party.
Input
The first line contains one integer () — the number of test cases.
Each test case consists of two lines:
- the first line contains one integer () — the number of friends;
- the second line contains integers (; ; all are distinct).
Output
Print one integer — the minimum number of invitations Monocarp has to send.
Note
In the first testcase, Monocarp can send invitations to friends and . 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 and , for example. Then friends and will attend: friend and his best friend have invitations, friend and his best friend have invitations. Friend won't attend since his friend doesn't have an invitation. It's impossible to send invitations to fewer than friends in such a way that at least come.
In the third testcase, Monocarp can send invitations to both friends and , and both of them will attend.
Samples
3
5
3 1 2 5 4
4
2 3 4 1
2
2 1
2
3
2
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |