Secret Santa solution codeforces – Every December, VK traditionally holds an event for its employees named “Secret Santa”

Secret Santa solution codeforces

Every December, VK traditionally holds an event for its employees named “Secret Santa”. Here’s how it happens.

nn employees numbered from 11 to nn take part in the event. Each employee ii is assigned a different employee bibi, to which employee ii 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 bibi must be distinct integers between 11 and nn, and for any iibiibi≠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 ii has said that they wish to make a gift to employee aiai.

Find a valid assignment bb that maximizes the number of fulfilled wishes of the employees.

Input Secret Santa solution codeforces

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1051≤t≤105). Description of the test cases follows.

Each test case consists of two lines. The first line contains a single integer nn (2n21052≤n≤2⋅105) — the number of participants of the event.

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ain1≤ai≤naiiai≠i) — wishes of the employees in order from 11 to nn.

It is guaranteed that the sum of nn over all test cases does not exceed 21052⋅105.

Output Secret Santa solution codeforces

For each test case, print two lines.

In the first line, print a single integer kk (0kn0≤k≤n) — the number of fulfilled wishes in your assignment.

In the second line, print nn distinct integers b1,b2,,bnb1,b2,…,bn (1bin1≤bi≤nbiibi≠i) — the numbers of employees assigned to employees 1,2,,n1,2,…,n.

kk must be equal to the number of values of ii such that ai=biai=bi, and must be as large as possible. If there are multiple answers, print any.

Example Secret Santa solution codeforces


2 1 2
6 4 6 2 4 5 6


3 1 2
6 4 7 2 3 5 1

In the first test case, two valid assignments exist: [3,1,2][3,1,2] and [2,3,1][2,3,1]. The former assignment fulfills two wishes, while the latter assignment fulfills only one. Therefore, k=2k=2, and the only correct answer is [3,1,2][3,1,2].


Secret Santa solution codeforcesPrimes and Queries solution kickstart

Also readWafa Na Raas Aayi Lyrics

Leave a Comment