#P10678. 『STA - R6』月

    ID: 9879 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>洛谷原创Special JudgeO2优化树论构造洛谷月赛

『STA - R6』月

Description

For a tree TT with nn nodes, its diameter diam(T)\operatorname{diam}(T) is defined as the maximum distance between any two nodes.

Given a positive integer nn and the degree did_i of each node ii, construct a tree TT^\prime that minimizes diam(T)\operatorname{diam}(T^\prime). It is guaranteed that at least one valid tree exists. If multiple valid answers exist, output any one of them.

Input Format

This problem contains multiple test cases within a single test point.

The first line contains a positive integer TT, representing the number of test cases.

For each test case:

  • The first line contains a positive integer nn.
  • The second line contains nn positive integers, where the ii-th integer denotes the degree did_i of node ii.

Output Format

For each test case, output n1n - 1 lines. Each line contains two positive integers uiu_i and viv_i, representing the edges of the constructed tree.

4
2
1 1
3
1 1 2
5
1 1 2 2 2
7
1 3 2 3 1 1 1
2 1
1 3
3 2
5 4
4 2
3 1
3 5
4 2
3 2
1 2
5 4
6 4
7 3

Hint

Sample Explanation

For the last test case in the sample input, the constructed tree is shown below:

Its diameter equals the distance between nodes 55 and 77 or nodes 66 and 77, which is 44. It can be proven that no tree satisfying the conditions has a diameter smaller than 44.

Data Constraints

This problem uses subtask scoring.

For 100%100\% of the data:

  • 2n2×1052 \le n \le 2 \times 10^5;
  • 1T1051 \le T \le 10^5;
  • n2×105\sum n \le 2 \times 10^5;
  • 1di<n1 \le d_i < n;
  • It is guaranteed that at least one valid solution exists.

Detailed subtask allocations are as follows:

Subtask ID Data Range Points
1 n5n \le 5 17
2 di2d_i \le 2 23
3 dd contains only two distinct values 26
4 No additional constraints 34

Translation by DeepSeek R1