Secret Santa solution codeforces
Every December, VK traditionally holds an event for its employees named “Secret Santa”. Here’s how it happens.
employees numbered from to take part in the event. Each employee is assigned a different employee , to which employee 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 must be distinct integers between and , and for any , 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 employeehas said that they wish to make a gift to employee .
Find a valid assignmentthat maximizes the number of fulfilled wishes of the employees.
Each test contains multiple test cases. The first line contains the number of test cases( ). Description of the test cases follows.
Each test case consists of two lines. The first line contains a single integer( ) — the number of participants of the event.
The second line containsintegers ( ; ) — wishes of the employees in order from to .
It is guaranteed that the sum ofover all test cases does not exceed .
For each test case, print two lines.
In the first line, print a single integer( ) — the number of fulfilled wishes in your assignment.
In the second line, printdistinct integers ( ; ) — the numbers of employees assigned to employees .
must be equal to the number of values of such that , and must be as large as possible. If there are multiple answers, print any.
2 3 2 1 2 7 6 4 6 2 4 5 6
2 3 1 2 4 6 4 7 2 3 5 1
In the first test case, two valid assignments exist:and . The former assignment fulfills two wishes, while the latter assignment fulfills only one. Therefore, , and the only correct answer is .