CF1530D.Secret Santa

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 bib_i, 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 bib_i must be distinct integers between 11 and nn, and for any ii, bi≠ib_i \ne 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 aia_i.

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

每年 12 月,VK 公司都会为其员工举办一项名为“秘密圣诞老人”的传统活动。活动规则如下:

共有 nn 名员工,编号从 11 到 nn。每名员工 ii 被分配给另一名不同的员工 bib_i,即员工 ii 需要为员工 bib_i 准备新年礼物。每位员工恰好被分配给另一位员工,且任何人都不能被分配给自己(但两名员工可以互相分配)。形式化地说,所有 bib_i 必须是 11 到 nn 之间互不相同的整数,且对任意 ii,必须满足 bi≠ib_i \ne i。

该分配通常随机生成。而今年,作为一项实验,所有活动参与者都被询问了他们希望将礼物送给谁。每名员工 ii 表示希望将礼物送给员工 aia_i。

请找出一个合法的分配方案 bb,使得员工愿望被满足的数量最大化。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1051 \le t \le 10^5). Description of the test cases follows.

Each test case consists of two lines. The first line contains a single integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — the number of participants of the event.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n; ai≠ia_i \ne 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 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1051 \le t \le 10^5)。随后是各测试用例的描述。

每个测试用例由两行组成。第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 活动参与者的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n;ai≠ia_i \ne i)—— 员工的愿望,按编号 11 到 nn 的顺序给出。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print two lines.

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

In the second line, print nn distinct integers b1,b2,…,bnb_1, b_2, \ldots, b_n (1≤bi≤n1 \le b_i \le n; bi≠ib_i \ne i) — the numbers of employees assigned to employees 1,2,…,n1, 2, \ldots, n.

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

对于每个测试用例,输出两行。

第一行,输出一个整数 kk(0≤k≤n0 \le k \le n)—— 表示你所构造的分配方案中满足的愿望数量。

第二行,输出 nn 个互不相同的整数 b1,b2,…,bnb_1, b_2, \ldots, b_n(1≤bi≤n1 \le b_i \le n;且 bi≠ib_i \ne i)—— 表示分配给员工 1,2,…,n1, 2, \ldots, n 的员工编号。

其中 kk 必须等于满足 ai=bia_i = b_i 的下标 ii 的个数,且应尽可能大。若存在多个合法答案,输出任意一个即可。

输入输出样例

  • 输入#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][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].

在第一个测试用例中,存在两个有效的分配方案:[3,1,2][3, 1, 2] 和 [2,3,1][2, 3, 1]。前一个分配方案满足了两个愿望,而后一个分配方案仅满足了一个愿望。因此,k=2k = 2,唯一正确的答案是 [3,1,2][3, 1, 2]。

输入解题思路,AI测评打分。不知道怎么写?

首页