Universal Cup Judging System

Universal Cup

Time Limit: 2.0 s Memory Limit: 1024 MB Total points: 100 Hackable ✓
Statistics

給定正整數 $n, m > 0$,以及整數 $a_1, a_2, \dots, a_n \in \{-1, 0, 1, \dots, m - 1\}$。請構造一棵包含 $n$ 個頂點 $V = \{v_1, v_2, \dots, v_n\}$ 的樹 $T$,滿足以下條件:對於每個滿足 $a_i \ne -1$ 的頂點 $v_i$,其度數 $\deg_T(v_i)$ 除以 $m$ 的餘數必須等於 $a_i$。若 $a_i = -1$,則對 $v_i$ 的度數沒有任何限制。

輸入格式

輸入的第一行包含一個整數 $t$,表示測試資料的組數($1 \le t \le 5 \cdot 10^5$)。對於每組測試資料:

第一行包含兩個整數 $n$ 和 $m$,分別表示樹中的頂點數與模數($1 \le n \le 5 \cdot 10^5$;$1 \le m \le 2 \cdot 10^9$)。

下一行包含 $n$ 個整數 $a_1, a_2, \dots, a_n$,表示要求的餘數($-1 \le a_i < m$)。

所有測試資料中 $n$ 的總和不超過 $5 \cdot 10^5$。

輸出格式

對於每組測試資料,若存在這樣一棵包含 $n$ 個頂點的樹,請輸出「YES」(大小寫不限),隨後輸出 $n - 1$ 行,每行包含兩個整數,表示每條邊的兩個端點。否則,請輸出「NO」(大小寫不限)。

範例

輸入

6
1 5
0
2 3
1 1
5 4
0 1 1 1 1
6 2
1 0 0 0 0 1
3 2
1 1 1
4 10
3 3 1 1

輸出

YES
YES
1 2
YES
1 2
1 3
1 4
1 5
YES
1 2
2 3
3 4
4 5
5 6
NO
NO

說明

在第一組測試資料中,我們需要建造一棵有 $n = 1$ 個頂點、$m = 5$ 且度數餘數陣列為 $b = [0]$ 的樹。由於只有一個頂點,因此不能有邊,所以只有唯一一棵可能的樹:一個度數為 $0$ 的單一頂點。因為 $0 \bmod 5 = 0$,滿足要求,故答案為「YES」。

在第二組測試資料中,我們需要一棵有 $n = 2$ 個頂點、$m = 3$ 且餘數為 $b = [1, 1]$ 的樹,意味著兩個度數模 $3$ 都必須同餘於 $1$。在兩個頂點上,只有唯一一棵樹:單一條邊 $1-2$。其度數為 $(1, 1)$ 且確實滿足 $1 \bmod 3 = 1$,因此答案為「YES」,印出「1 2」是合法的。

在第三組測試資料中,我們需要 $n = 5$、$m = 4$,餘數為 $b = [0, 1, 1, 1, 1]$:第一個頂點的度數必須能被 $4$ 整除,而另外四個頂點的度數必須滿足 $\text{degree} \equiv 1 \pmod 4$。一個自然的構造是以頂點 $1$ 為中心的星形圖:將 $1$ 連接到其他所有頂點。此時 $\deg(1) = 4$ 且 $4 \bmod 4 = 0$,而 $2, 3, 4, 5$ 的度數皆為 $1$ 且 $1 \bmod 4 = 1$。因此,答案為「YES」,邊「1 2」、「1 3」、「1 4」、「1 5」是可行的。

在第四組測試資料中,我們需要一棵 $n = 6$、$m = 2$、餘數為 $b = [1, 0, 0, 0, 0, 1]$ 的樹,亦即兩端點的度數必須為奇數,而中間四個頂點的度數必須為偶數。在模 $2$ 之下,這純粹是奇偶性限制。簡單路徑 $1-2-3-4-5-6$ 完美契合:頂點 $1$ 和 $6$ 的度數為 $1$(奇數),而頂點 $2, 3, 4, 5$ 的度數為 $2$(偶數)。因此答案為「YES」,路徑上的邊是正確的。

在第五組測試資料中,我們被要求在 $n = 3$、$m = 2$ 且 $b = [1, 1, 1]$ 的情況下建造一個圖(因此特別地,一棵樹),所以每個頂點的度數都必須為奇數。這是不可能的:在任何無向圖中,奇數度頂點的數量總是偶數,因此對任何邊集都不可能出現三個奇數度數。故判定結果為「NO」。

在第六組測試資料中,我們有 $n = 4$、$m = 10$、$b = [3, 3, 1, 1]$。由於 $m > n - 1 = 3$,模 $10$ 的餘數等於度數本身,因此我們要求度數恰好為 $\deg = [3, 3, 1, 1]$。這迫使兩個頂點與所有其他頂點相鄰(在 $4$ 個頂點的圖中度數為 $3$),而當 $n > 2$ 時,任何這樣的一對頂點必然會與任意第三個頂點一起形成一個環。樹不能包含環,故判定結果亦為「NO」。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#1219EditorialOpen题解jianglyView

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.