Flower-like Pseudotree
Contest: Codeforces Round 896 (Div. 1)
Difficulty rating: 3000
Tags: constructive algorithms, graphs, greedy, implementation, trees

A pseudotree is a connected graph which has exactly one cycle and no self-loops. Note that a pseudotree may contain multiple-edges. It can be shown that a pseudotree with $$$n$$$ vertices always contains $$$n$$$ edges.

After deleting all edges on the cycle in the pseudotree, a forest$$$^{\dagger}$$$ will be formed. It can be shown that each tree in the forest will contain exactly one vertex which is on cycle before removing the edges. If all trees in the forest have the same depth$$$^{\ddagger}$$$ when picking the vertex on cycle as root, we call the original pseudotree flower-like.

Our friend sszcdjr, had a flower-like pseudotree with $$$n$$$ vertices and $$$n$$$ edges. However, he forgot all the edges in the pseudotree. Fortunately, he still remembers the degrees of vertices. Specifically, the degree of the $$$i$$$-th vertex is $$$d_i$$$.

You have to help sszcdjr construct a possible flower-like pseudotree with $$$n$$$ vertices, where the degree of the $$$i$$$-th vertex is exactly $$$d_i$$$, or tell him that it is impossible.

$$$^{\dagger}$$$ A forest is a graph in which all connectivity components are trees. A connected graph without cycles and self-loops is called a tree.

$$$^{\ddagger}$$$ The depth of a tree with a root is the maximum distance from the root to the vertex of this tree.

Input format:
The first line of the input contains a single integer $$$t$$$ ($$$1\leq t\leq 10^5$$$) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$2\leq n\leq 10^6$$$) — the number of vertices.

The second line of each test case contains $$$n$$$ integers $$$d_1,d_2,\ldots,d_n$$$ ($$$1\leq d_i\leq n$$$) — the degree of each vertex.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.

Output format:
For each test case, if there exist a possible flower-like pseudotree:

- Print "Yes" (without quotes) in the first line.
- Then, output $$$n$$$ lines, in each line print two integers $$$u_i$$$ and $$$v_i$$$ — the two vertices that the $$$i$$$-th edge connects.

If there are multiple answers, you may output any of them.

Otherwise, print "No" (without quotes) in the only line of output.

You can output the first line of each test case in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Constraints:
Time limit: 3.0 s
Memory limit: 512.0 MB

Note:
In the first test case, the only possible flower-like psuedotree is:

After deleting all edges on the cycle in the pseudotree, each tree has depth $$$0$$$.

In the second test case, it can be proven that there's no such flower-like psuedotree.

In the third test case, one of the possible flower-like psuedotrees is:

Example input:
6
3
2 2 2
4
1 2 3 4
7
4 3 3 1 1 1 1
6
1 1 2 2 3 3
10
1 1 5 2 1 1 1 1 1 6
9
1 1 3 1 1 4 1 1 5

Example output:
Yes
1 2
2 3
3 1
No
Yes
1 2
2 3
3 1
1 4
1 5
2 6
3 7
Yes
5 6
6 5
1 3
2 4
3 5
4 6
No
Yes
3 6
6 9
9 3
1 3
2 6
4 6
5 9
7 9
8 9

Editorial:
If $$$\sum_{i=1}^n d_i\neq 2n$$$, it's obviously impossible to construct a flower-like psuedotree. So let's just consider the situation when $$$\sum_{i=1}^n d_i=2n$$$.
Sort $$$d_i$$$ from largest to smallest. If $$$d_1=d_2=\dots=d_n=2$$$, we can simply construct a cycle. Otherwise $$$d_1>2$$$, in which case there must be at least two $$$d_i>2$$$ and the vertices on the cycle must satisfy $$$d_i>2$$$.
Ignore all vertices with $$$d_i=1$$$ as we can just hang them under all the other vertices. The following pictures don't show vertices with $$$d_i=1$$$ and the edges with weight $$$2$$$ are two multi-edges.
Let $$$cnt$$$ be the number of vertices with $$$d_i>1$$$.
If $$$cnt$$$ is even, we can make a cycle using $$$d_1$$$ and $$$d_2$$$, with two chains of equal length hanging from $$$d_1$$$ and $$$d_2$$$.
If $$$cnt$$$ is odd and $$$d_1>3$$$, it's easy to see that we can hang two vertices below $$$d_1$$$ to transform it to the situation where $$$cnt$$$ is even. However, we need at least $$$5$$$ vertices to make the psuedotree flower-like, which means that $$$cnt\geq5$$$ must be satisfied. (In the picture, at least vertices $$$1\sim5$$$ are needed.)
If $$$cnt$$$ is odd, $$$d_1>3$$$ and $$$cnt=3$$$, it's impossible for us to simply build a cycle length $$$2$$$. Thus, we can try to put $$$d_1,d_2,d_3$$$ in one cycle and the condition is $$$d_3>2$$$.
If $$$cnt$$$ is odd and $$$d_1=3$$$, it's impossible for us to do the operation above, because we can't hang two vertices under $$$d_1$$$. It's easy to see that we can't balance the two trees if $$$d_3=2$$$. For $$$d_3=3$$$, we can hang $$$d_3,d_4$$$ under $$$d_1,d_2$$$, $$$d_5,d_6$$$ under $$$d_3$$$ and $$$d_7$$$ under $$$d_4$$$ to make the psuedotree flower-like. However, we need at least $$$7$$$ vertices to make it balanced, which means $$$cnt\geq7$$$ must be satisfied.
If $$$cnt$$$ is odd, $$$d_1=3$$$ and $$$cnt=5$$$, there are three cases:
• $$$d_1=d_2=d_3=3,d_4=d_5=2$$$: It can be proven that there isn't such a psuedotree.
• $$$d_1=d_2=d_3=d_4=3,d_5=2$$$: It can be proven that there isn't such a psuedotree.
• $$$d_1=d_2=d_3=d_4=d_5=3$$$: We can simply build a cycle of length $$$5$$$.
If $$$cnt$$$ is odd, $$$d_1=3$$$ and $$$cnt=3$$$, there's only one case:
• $$$d_1=d_2=d_3=3$$$: We can simply build a cycle of length $$$3$$$.
The total complexity depends on the sorting part, which is $$$\mathcal O(n\log n)$$$ if you use std::sort or std::stable_sort, and $$$\mathcal O(n)$$$ if you use bucket sorting.