题目收集
题目列表题单
返回列表

MEX Tree

CodeforcesP0053· 1000ms· 256 MB· ZJU Summer 2026 Contest 5原题链接
构造贪心

创建于 2026/07/18 · 更新于 2026/07/18

题目描述

Given positive integer nnn and non-negative integer sequence a1…na_{1 \dots n}a1…n​.

You have to construct a rooted tree TTT with nnn nodes, labeld 1…n1 \dots n1…n.

Let did_idi​ be the number of edges on the unique simple path between the root and node iii on tree TTT.

You need to maximize m=MEX⁡ {ai+di∣i=1…n}m = \operatorname{MEX}\ \{ a_i+d_i \mid i=1 \dots n\}m=MEX {ai​+di​∣i=1…n}, where MEX⁡S\operatorname{MEX} SMEXS is defined as the smallest non-negative integer that is not in set SSS.

You need to solve CCC testcases for each test data.

输入格式

The first line of input contains a single integer CCC(1≤C≤2×1041 \le C \le 2 \times 10^41≤C≤2×104). Then CCC testcases follows.

The first line of each testcase contains a single integer nnn(1≤n≤2×1051 \le n \le 2 \times 10^51≤n≤2×105).

The second line of each testcase contains nnn integers a1,a2,…,ana_1,a_2,\dots,a_na1​,a2​,…,an​(0≤an≤n0 \le a_n \le n0≤an​≤n).

It is guaranteed that the sum of nnn over all testcases in a single testdata does not exceed 2×1052 \times 10^52×105.

输出格式

For each testcase, you should output two lines.

The first line of each testcase contains one integer mmax⁡m_{\max}mmax​, the maximum mmm.

The second line of each testcase contains nnn non-negative integers f1,f2,…,fnf_1,f_2,\dots,f_nf1​,f2​,…,fn​. fi=0f_i=0fi​=0 means that node iii is the root of TTT, otherwise fif_ifi​ is the label of the parent of node iii on TTT.

If there are several possible TTTs, you may output any of them.

样例输入 #1

3
3
0 2 0
7
1 0 4 1 0 2 6
1
1

样例输出 #1

2
0 1 1
6
2 0 2 1 2 1 2
0
0