CF1530D.Secret Santa
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Every December, VK traditionally holds an event for its employees named "Secret Santa". Here's how it happens.
n employees numbered from 1 to n take part in the event. Each employee i is assigned a different employee bi , to which employee i has to make a new year gift. Each employee is assigned to exactly one other employee, and nobody is assigned to themselves (but two employees may be assigned to each other). Formally, all bi must be distinct integers between 1 and n , and for any i , bi=i must hold.
The assignment is usually generated randomly. This year, as an experiment, all event participants have been asked who they wish to make a gift to. Each employee i has said that they wish to make a gift to employee ai .
Find a valid assignment b that maximizes the number of fulfilled wishes of the employees.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t ( 1≤t≤105 ). Description of the test cases follows.
Each test case consists of two lines. The first line contains a single integer n ( 2≤n≤2⋅105 ) — the number of participants of the event.
The second line contains n integers a1,a2,…,an ( 1≤ai≤n ; ai=i ) — wishes of the employees in order from 1 to n .
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .
输出格式
For each test case, print two lines.
In the first line, print a single integer k ( 0≤k≤n ) — the number of fulfilled wishes in your assignment.
In the second line, print n distinct integers b1,b2,…,bn ( 1≤bi≤n ; bi=i ) — the numbers of employees assigned to employees 1,2,…,n .
k must be equal to the number of values of i such that ai=bi , and must be as large as possible. If there are multiple answers, print any.
输入输出样例
输入#1
2 3 2 1 2 7 6 4 6 2 4 5 6
输出#1
2 3 1 2 4 6 4 7 2 3 5 1
说明/提示
In the first test case, two valid assignments exist: [3,1,2] and [2,3,1] . The former assignment fulfills two wishes, while the latter assignment fulfills only one. Therefore, k=2 , and the only correct answer is [3,1,2] .