#abc239f. F - Construct Highway

F - Construct Highway

Score : 500500 points

问题陈述

在Atcoder共和国,有编号为 11NNNN 个城镇以及编号为 11MMMM 条高速公路。
高速公路 ii 双向连接城镇 AiA_i 和城镇 BiB_i

高桥国王计划新建 (NM1)(N-M-1) 条新高速公路,以满足以下两个条件:

  • 使用一些数量的高速公路可以从任意一对城镇之间通行
  • 对于每 i=1,,Ni=1,\ldots,N,恰好有 DiD_i 条高速公路直接与城镇 ii 相连

确定是否存在这样的建设方式。如果存在,输出一种方案。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

The Republic of Atcoder has NN towns numbered 11 through NN, and MM highways numbered 11 through MM.
Highway ii connects Town AiA_i and Town BiB_i bidirectionally.

King Takahashi is going to construct (NM1)(N-M-1) new highways so that the following two conditions are satisfied:

  • One can travel between every pair of towns using some number of highways
  • For each i=1,,Ni=1,\ldots,N, exactly DiD_i highways are directly connected to Town ii

Determine if there is such a way of construction. If it exists, print one.

Constraints

  • 2N2×1052 \leq N \leq 2\times 10^5
  • 0M<N10 \leq M \lt N-1
  • 1DiN11 \leq D_i \leq N-1
  • 1Ai<BiN1\leq A_i \lt B_i \leq N
  • If iji\neq j, then (Ai,Bi)(Aj,Bj)(A_i, B_i) \neq (A_j,B_j).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

D1D_1 \ldots DND_N

A1A_1 B1B_1

\vdots

AMA_M BMB_M

Output

If there isn't a way of construction that satisfies the conditions, print -1.
If there is, print (NM1)(N-M-1) lines. The ii-th line should contain the indices of the two towns connected by the ii-th highway to be constructed.

Sample Input 1

6 2
1 2 1 2 2 2
2 3
1 4

Sample Output 1

6 2
5 6
4 5

As in the Sample Output, the conditions can be satisfied by constructing highways connecting Towns 66 and 22, Towns 55 and 66, and Towns 44 and 55, respectively.

Another example to satisfy the conditions is to construct highways connecting Towns 66 and 44, Towns 55 and 66, and Towns 22 and 55, respectively.

Sample Input 2

5 1
1 1 1 1 4
2 3

Sample Output 2

-1

Sample Input 3

4 0
3 3 3 3

Sample Output 3

-1

update @ 2024/3/10 10:19:26