A - Make M

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N を正の奇数とします. 長さ N の整数列 S = (S_1, S_2, \dots, S_N)M 型であるとは,「各偶数 i = 2, 4, \dots, N - 1 について S_{i-1} < S_i かつ S_i > S_{i+1} が成り立つ」ことを言います.

長さ N の正整数列 A = (A_1, A_2, \dots, A_N) が与えられます. A を M 型になるように並べ替えることが可能かどうかを判定してください.

制約

  • 1 \leq N \leq 2 \times 10^5
  • N奇数である.
  • 1 \leq A_i \leq 10^9 \ \ (1 \leq i \leq N)

入力

入力は以下の形式で標準入力から与えられる.

N
A_1 \ A_2 \ \dots \ A_N

出力

与えられた整数列 A を M 型になるように並べ替えることが可能なら Yes を,不可能なら No を出力せよ.


入力例 1

5
1 2 3 4 5

出力例 1

Yes

与えられた数列は A = (1, 2, 3, 4, 5) です. これを並べ替えて,たとえば B = (4, 5, 1, 3, 2) とすると,

  • i = 2 について B_1 = 4 < 5 = B_2 かつ B_2 = 5 > 1 = B_3 が成り立ち,
  • i = 4 について B_3 = 1 < 3 = B_4 かつ B_4 = 3 > 2 = B_5 が成り立ちます.

したがって,この数列 B は M 型であり,答えは Yes です.


入力例 2

5
1 6 1 6 1

出力例 2

Yes

与えられた数列 A 自身が M 型です.


入力例 3

5
1 6 6 6 1

出力例 3

No

M 型になるように並べ替えることは不可能です.

Score : 300 points

Problem Statement

Let N be a positive odd number. A sequence of integers of length N, S = (S_1, S_2, \dots, S_N), is said to be M-type if "for each even number i = 2, 4, \dots, N - 1, it holds that S_{i-1} < S_i and S_i > S_{i+1}".

You are given a sequence of positive integers of length N, A = (A_1, A_2, \dots, A_N). Determine if it is possible to rearrange A to be M-type.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • N is an odd number.
  • 1 \leq A_i \leq 10^9 \ \ (1 \leq i \leq N)

Input

The input is given from Standard Input in the following format:

N
A_1 \ A_2 \ \dots \ A_N

Output

If it is possible to rearrange the given integer sequence A to be M-type, print Yes; otherwise, print No.


Sample Input 1

5
1 2 3 4 5

Sample Output 1

Yes

The given sequence is A = (1, 2, 3, 4, 5). After rearranging it, for example, to B = (4, 5, 1, 3, 2),

  • for i = 2, it holds that B_1 = 4 < 5 = B_2 and B_2 = 5 > 1 = B_3;
  • for i = 4, it holds that B_3 = 1 < 3 = B_4 and B_4 = 3 > 2 = B_5.

Therefore, this sequence B is M-type, and the answer is Yes.


Sample Input 2

5
1 6 1 6 1

Sample Output 2

Yes

The given sequence A itself is M-type.


Sample Input 3

5
1 6 6 6 1

Sample Output 3

No

It is impossible to rearrange it to be M-type.

B - Exactly Three Bits

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

正整数 X に対し,「 X2 進法表記したときに現れる 1 の個数」を f(X) とします. たとえば,6 = 110_{(2)}, \ 11 = 1011_{(2)}, \ 16 = 10000_{(2)} なので,f(6) = 2, \ f(11) = 3, \ f(16) = 1 です.

正整数 N が与えられます. N 以下の正整数 X であって,f(X) = 3 を満たすものが存在するかどうかを判定し,存在するならその最大値を求めてください.

T 個のテストケースが与えられるので,それぞれについて答えてください.

制約

  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^{18}

入力

入力は以下の形式で標準入力から与えられる. N_ii 番目のテストケースにおける N の値を表す.

T
N_1
N_2
\vdots
N_T

出力

T 行出力せよ. i 行目には,N_i 以下の正整数 X であって,f(X) = 3 を満たすものの最大値を出力せよ. ただし,そのような正整数 X が存在しない場合には -1 を出力せよ.


入力例 1

4
16
161
4
1000000000000000000

出力例 1

14
161
-1
936748722493063168
  • 1 つ目のテストケースについて,f(14) = 3, \ f(15) = 4, \ f(16) = 1 なので,X \leq 16 かつ f(X) = 3 を満たす最大の正整数 X14 です.
  • 2 つ目のテストケースについて,f(161) = 3 なので,X \leq 161 かつ f(X) = 3 を満たす最大の正整数 X161 です.
  • 3 つ目のテストケースについて,f(1) = 1, \ f(2) = 1, \ f(3) = 2, \ f(4) = 1 なので,X \leq 4 かつ f(X) = 3 を満たす正整数 X は存在しません.
  • 4 つ目のテストケースのように,入出力値が 32bit 整数型に収まらないこともあります.

Score : 400 points

Problem Statement

For a positive integer X, let f(X) be the number of 1s that appear when X is represented in binary notation. For example, 6 = 110_{(2)}, \ 11 = 1011_{(2)}, \ 16 = 10000_{(2)}, so f(6) = 2, \ f(11) = 3, \ f(16) = 1.

You are given a positive integer N. Determine whether there is a positive integer X less than or equal to N such that f(X) = 3, and if so, find the maximum such X.

There are T test cases to solve.

Constraints

  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^{18}

Input

The input is given from Standard Input in the following format, where N_i represents the value of N in the i-th test case:

T
N_1
N_2
\vdots
N_T

Output

Print T lines. The i-th line should contain the maximum positive integer X less than or equal to N_i such that f(X) = 3. If there is no such positive integer X, print -1.


Sample Input 1

4
16
161
4
1000000000000000000

Sample Output 1

14
161
-1
936748722493063168
  • For the first test case, f(14) = 3, \ f(15) = 4, \ f(16) = 1, so the maximum positive integer X satisfying X \leq 16 and f(X) = 3 is 14.
  • For the second test case, f(161) = 3, so the maximum positive integer X satisfying X \leq 161 and f(X) = 3 is 161.
  • For the third test case, f(1) = 1, \ f(2) = 1, \ f(3) = 2, \ f(4) = 1, so there is no positive integer X satisfying X \leq 4 and f(X) = 3.
  • As seen in the fourth test case, the input and output values may not fit in a 32-bit integer type.
C - Dyed by Majority (Odd Tree)

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点の木が与えられます. 頂点には 1 から N までの番号が付いており,i 番目の辺は頂点 A_i と頂点 B_i を結んでいます. また,すべての頂点について,接続する辺の本数は奇数です.

与えられた木の各頂点を黒 ( B ) か白 ( W ) のいずれかの色で塗ります. このとき,「各頂点の色( B または W )を頂点の番号順に並べて得られる文字列」を色の列と呼びます.

色の列 S が与えられます. すべての頂点に色が塗られた状態で以下の操作を 1 回行った結果,色の列が S となることがあり得るかどうかを判定し,あり得るなら操作を行う前の色の列として適切なものを 1 つ求めてください.

操作: 各頂点 k = 1, 2, \dots, N に対して,辺で結ばれた頂点の色のうち過半数を占めるものを C_k とする. すべての頂点について同時に,頂点 k の色を C_k に塗り替える.

T 個のテストケースが与えられるので,それぞれについて答えてください.

制約

  • T \geq 1
  • N \geq 2
  • 1 つの入力に含まれるテストケースについて,N の総和は 2 \times 10^5 以下である.
  • 1 \leq A_i < B_i \leq N \ \ (1 \leq i \leq N - 1)
  • 与えられる辺 (A_i, B_i) \ (1 \leq i \leq N - 1) は木をなす.
  • 各頂点 k \ (1 \leq k \leq N)A_i, B_i \ (1 \leq i \leq N - 1) として合計奇数回現れる.
  • SB, W からなる長さ N の文字列である.

入力

入力は以下の形式で標準入力から与えられる.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケース \mathrm{case}_i \ (1 \leq i \leq T) は以下の形式である.

N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}
S

出力

T 行出力せよ. i 行目には,i 番目のテストケースについて,操作の結果として色の列が S となることがあり得るなら操作を行う前の色の列として適切なものを,あり得ないなら -1 を出力せよ. 操作を行う前の色の列として適切なものが複数存在する場合,そのような色の列のうちどれを出力しても正答と見なされる.


入力例 1

2
4
1 2
1 3
1 4
BWWW
4
1 2
1 3
1 4
BBWW

出力例 1

WBBW
-1

1 つ目のテストケースについて,操作を行う前の色の列が WBBW であったとします. このとき,

  • 頂点 1 について,辺で結ばれた頂点 2, 3, 4 の色はそれぞれ B, B, W であり,過半数を占めるのは C_1 = {}B
  • 頂点 2 について,辺で結ばれた頂点 1 の色は W であり,過半数を占めるのは C_2 = {}W
  • 頂点 3 について,辺で結ばれた頂点 1 の色は W であり,過半数を占めるのは C_3 = {}W
  • 頂点 4 について,辺で結ばれた頂点 1 の色は W であり,過半数を占めるのは C_4 = {}W となります.

したがって,操作後の色の列は BWWW となり,条件を満たします. 同様に,操作前の色の列が WBBB, WBWB, WWBB であった場合にも,操作後の色の列は BWWW となり,これらのうちどれを出力しても正答と見なされます。

2 つ目のテストケースについて,入力された木において操作を行った結果,色の列が BBWW となることはあり得ません.

Score : 500 points

Problem Statement

You are given a tree with N vertices. The vertices are labeled from 1 to N, and the i-th edge connects vertex A_i and vertex B_i. Moreover, for every vertex, the number of incident edges is odd.

You will color each vertex of the given tree either black ( B ) or white ( W ). Here, the string obtained by arranging the colors ( B or W ) of the vertices in the order of vertex labels is called a color sequence.

You are given a color sequence S. Determine whether the color sequence S can result from performing the following operation once when all vertices are colored, and if it can, find one possible color sequence before the operation.

Operation: For each vertex k = 1, 2, \dots, N, let C_k be the color that occupies the majority (more than half) of the colors of the vertices connected to k by an edge. For every vertex k, change its color to C_k simultaneously.

There are T test cases to solve.

Constraints

  • T \geq 1
  • N \geq 2
  • The sum of N over the test cases in each input file is at most 2 \times 10^5.
  • 1 \leq A_i < B_i \leq N \ \ (1 \leq i \leq N - 1)
  • The given edges (A_i, B_i) \ (1 \leq i \leq N - 1) form a tree.
  • Each vertex k \ (1 \leq k \leq N) appears an odd number of times in total as A_i, B_i \ (1 \leq i \leq N - 1).
  • S is a string of length N consisting of B and W.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case \mathrm{case}_i \ (1 \leq i \leq T) is in the following format:

N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}
S

Output

Print T lines. For the i-th line, if the color sequence S can result from performing the operation in the i-th test case, print one possible color sequence before the operation; otherwise, print -1. If there are multiple possible color sequences before the operation, any of them will be considered correct.


Sample Input 1

2
4
1 2
1 3
1 4
BWWW
4
1 2
1 3
1 4
BBWW

Sample Output 1

WBBW
-1

For the first test case, suppose the color sequence before the operation was WBBW. In this case,

  • for vertex 1, the colors of the connected vertices 2, 3, 4 are B, B, W, respectively, with C_1 = {}B occupying the majority;
  • for vertex 2, the color of the connected vertex 1 is W, with C_2 = {}W occupying the majority;
  • for vertex 3, the color of the connected vertex 1 is W, with C_3 = {}W occupying the majority;
  • for vertex 4, the color of the connected vertex 1 is W, with C_4 = {}W occupying the majority.

Therefore, the color sequence after the operation is BWWW, satisfying the condition. Similarly, if the color sequence before the operation was WBBB, WBWB, or WWBB, the color sequence after the operation would be BWWW, and any of these are considered correct.

For the second test case, the color sequence BBWW cannot result from the operation on the given tree.

D - Everywhere is Sparser than Whole (Construction)

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

頂点集合が空でない単純無向グラフの密度\displaystyle\frac{(辺数)}{(頂点数)} と定義します.

正整数 N, D が与えられます. N 頂点 DN 辺の単純無向グラフ G であって,以下の条件を満たすものが存在するかどうかを判定し,存在するならそのようなグラフを 1 つ求めてください.

条件: G の頂点集合を V とする. V の任意の空でない部分集合 X に対して,X による G の誘導部分グラフの密度は D 未満である.

誘導部分グラフとは

グラフ G の頂点部分集合 X に対して,X による G誘導部分グラフとは,「頂点集合が X であり,辺集合が『 G の辺であって X 内の 2 頂点を結ぶもの全体』であるグラフ」を指します. 上記の条件では,頂点部分集合として空集合でも全体でもないもののみを考えていることに注意してください.

制約

  • N \geq 1
  • D \geq 1
  • DN \leq 5 \times 10^4

入力

入力は以下の形式で標準入力から与えられる.

N D

出力

条件を満たす単純無向グラフが存在するなら Yes を,存在しないなら No を出力せよ. さらに,Yes の場合には,続く DN 行に条件を満たすグラフを以下の形式で出力せよ. 条件を満たすグラフが複数存在する場合,そのようなグラフのうちどれを出力しても正答と見なされる.

A_1 B_1
A_2 B_2
\vdots
A_{DN} B_{DN}
  • 1 \leq A_i, B_i \leq N \ \ (1 \leq i \leq DN)
  • A_i \neq B_i \ \ (1 \leq i \leq DN)
  • \{A_i, B_i\} \neq \{A_j, B_j\} \ \ (1 \leq i < j \leq DN)
  • グラフの頂点には 1 から N までの番号が付いているものとする.
  • i 行目の出力は,i 番目の辺が頂点 A_i と頂点 B_i を結んでいることを表す.
  • 辺の順番(どの辺を何番目に出力するか)や頂点の順番(各辺についてどちらの端点を先に出力するか)は自由である.

入力例 1

3 1

出力例 1

Yes
1 2
1 3
2 3

出力されたグラフの頂点集合は \{1, 2, 3\},辺集合は \{(1, 2), (1, 3), (2, 3)\} であり,単純です. 頂点集合の空でない真部分集合 X としては \{1\}, \{2\}, \{3\}, \{1, 2\}, \{1, 3\}, \{2, 3\}6 通りが考えられ,

  • X = \{1\}, \{2\}, \{3\} のとき,X による誘導部分グラフの辺集合は空集合であり,その密度は \displaystyle\frac{0}{1} = 0
  • X = \{1, 2\}, \{1, 3\}, \{2, 3\} のとき,X による誘導部分グラフの辺集合はそれぞれ \{(1, 2)\}, \{(1, 3)\}, \{(2, 3)\} であり,いずれも密度は \displaystyle\frac{1}{2} です.

全ての場合に対して誘導部分グラフの密度は D = 1 未満であり,このグラフは条件を満たします.


入力例 2

4 2

出力例 2

No

4 頂点 8 辺の単純無向グラフは存在しません.

Score : 500 points

Problem Statement

We define the density of a non-empty simple undirected graph as \displaystyle\frac{(\text{number\ of\ edges})}{(\text{number\ of\ vertices})}.

You are given positive integers N and D. Determine whether there is a simple undirected graph G with N vertices and DN edges that satisfies the following condition. If it exists, find one such graph.

Condition: Let V be the vertex set of G. For any non-empty proper subset X of V, the density of the induced subgraph of G by X is strictly less than D.

What is an induced subgraph?

For a vertex subset X of graph G, the induced subgraph of G by X is the graph with vertex set X and edge set containing all edges of G that connect two vertices in X. In the above condition, note that we only consider vertex subsets that are neither empty nor the entire set.

Constraints

  • N \geq 1
  • D \geq 1
  • DN \leq 5 \times 10^4

Input

The input is given from Standard Input in the following format:

N D

Output

If there is a simple undirected graph that satisfies the condition, print Yes; otherwise, print No. Furthermore, in the case of Yes, print a graph that satisfies the condition in the following format in the next DN lines. If multiple graphs satisfy the condition, any of them will be considered correct.

A_1 B_1
A_2 B_2
\vdots
A_{DN} B_{DN}
  • 1 \leq A_i, B_i \leq N \ \ (1 \leq i \leq DN)
  • A_i \neq B_i \ \ (1 \leq i \leq DN)
  • \{A_i, B_i\} \neq \{A_j, B_j\} \ \ (1 \leq i < j \leq DN)
  • The vertices of the graph are numbered from 1 to N.
  • The i-th line of the output represents that the i-th edge connects vertex A_i and vertex B_i.
  • The order of the edges (which edge is printed first) and the order of the vertices (which endpoint of each edge is printed first) may be arbitrary.

Sample Input 1

3 1

Sample Output 1

Yes
1 2
1 3
2 3

The printed graph has vertex set \{1, 2, 3\} and edge set \{(1, 2), (1, 3), (2, 3)\}, and is simple. The vertex set X has 6 non-empty proper subsets: \{1\}, \{2\}, \{3\}, \{1, 2\}, \{1, 3\}, \{2, 3\}.

  • For X = \{1\}, \{2\}, \{3\}, the edge sets of the induced subgraphs by X are empty, and the densities are \displaystyle\frac{0}{1} = 0.
  • For X = \{1, 2\}, \{1, 3\}, \{2, 3\}, the edge sets of the induced subgraphs by X are \{(1, 2)\}, \{(1, 3)\}, \{(2, 3)\}, respectively, and the densities are all \displaystyle\frac{1}{2}.

In all cases, the density of the induced subgraph is strictly less than D = 1, so this graph satisfies the condition.


Sample Input 2

4 2

Sample Output 2

No

A simple undirected graph with 4 vertices and 8 edges does not exist.

E - Not Dyed by Majority (Cubic Graph)

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 800

問題文

N を正の偶数として,N 頂点 \displaystyle\frac{3}{2}N 辺の連結な単純無向グラフが与えられます. 頂点には 1 から N までの番号が付いており,i 番目の辺は頂点 A_i と頂点 B_i を結んでいます. また,すべての頂点について,接続する辺の本数はちょうど 3 です.

与えられたグラフの各頂点を黒 ( B ) か白 ( W ) のいずれかの色で塗ります. このとき,「各頂点の色( B または W )を頂点の番号順に並べて得られる文字列」を色の列と呼びます.

すべての頂点に色が塗られた状態で以下の操作を 1 回行った結果としてあり得ない色の列が存在するかどうかを判定し,存在するならそのような色の列を 1 つ求めてください.

操作: 各頂点 k = 1, 2, \dots, N に対して,辺で結ばれた頂点の色のうち過半数を占めるものを C_k とする. すべての頂点について同時に,頂点 k の色を C_k に塗り替える.

T 個のテストケースが与えられるので,それぞれについて答えてください.

制約

  • T \geq 1
  • N \geq 4
  • 1 つの入力に含まれるテストケースについて,N の総和は 5 \times 10^4 以下である.
  • N偶数である.
  • 1 \leq A_i < B_i \leq N \ \ \left(1 \leq i \leq \displaystyle\frac{3}{2}N\right)
  • (A_i, B_i) \neq (A_j, B_j) \ \ \left(1 \leq i < j \leq \displaystyle\frac{3}{2}N\right)
  • 与えられるグラフは連結である.
  • 各頂点 k \ (1 \leq k \leq N)A_i, B_i \ \left(1 \leq i \leq \displaystyle\frac{3}{2}N\right) として合計 3現れる.

入力

入力は以下の形式で標準入力から与えられる.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケース \mathrm{case}_i \ (1 \leq i \leq T) は以下の形式である.

N
A_1 B_1
A_2 B_2
\vdots
A_{\frac{3}{2}N} B_{\frac{3}{2}N}

出力

T 行出力せよ. i 行目には,i 番目のテストケースについて,操作を行った結果としてあり得ない色の列が存在するならそのような色の列を,存在しないなら -1 を出力せよ. 操作を行った結果としてあり得ない色の列が複数存在する場合,そのような色の列のうちどれを出力しても正答と見なされる.


入力例 1

2
4
1 2
1 3
1 4
2 3
2 4
3 4
10
1 2
1 3
1 4
2 3
2 4
3 5
4 5
5 6
6 7
6 8
7 9
7 10
8 9
8 10
9 10

出力例 1

BWWW
BWWWBWWWBB

1 つ目のテストケースについて考えます. 頂点 1 の色が B となるためには,操作を行う前に頂点 2, 3, 4 のうち 2 つ以上の色が B である必要があります. このとき,頂点 2, 3, 4 のうち少なくとも 1 つに関して,辺で結ばれた頂点のうち 2 つ以上の色が B であるため,操作を行った後の色は B となります. したがって,BWWW という色の列は操作を行った結果としてあり得ません.

Score : 800 points

Problem Statement

You are given a connected simple undirected graph with N vertices and \displaystyle\frac{3}{2}N edges, where N is a positive even number. The vertices are labeled from 1 to N, and the i-th edge connects vertex A_i and vertex B_i. Moreover, for every vertex, the number of incident edges is exactly 3.

You will color each vertex of the given graph either black ( B ) or white ( W ). Here, the string obtained by arranging the colors ( B or W ) of the vertices in the order of vertex labels is called a color sequence.

Determine whether there is a color sequence that cannot result from performing the following operation once when all vertices are colored, and if there is such a color sequence, find one.

Operation: For each vertex k = 1, 2, \dots, N, let C_k be the color that occupies the majority (more than half) of the colors of the vertices connected to k by an edge. For every vertex k, change its color to C_k simultaneously.

There are T test cases to solve.

Constraints

  • T \geq 1
  • N \geq 4
  • The sum of N over the test cases in each input file is at most 5 \times 10^4.
  • N is an even number.
  • 1 \leq A_i < B_i \leq N \ \ \left(1 \leq i \leq \displaystyle\frac{3}{2}N\right)
  • (A_i, B_i) \neq (A_j, B_j) \ \ \left(1 \leq i < j \leq \displaystyle\frac{3}{2}N\right)
  • The given graph is connected.
  • Each vertex k \ (1 \leq k \leq N) appears exactly 3 times as A_i, B_i \ \left(1 \leq i \leq \displaystyle\frac{3}{2}N\right).

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case \mathrm{case}_i \ (1 \leq i \leq T) is in the following format:

N
A_1 B_1
A_2 B_2
\vdots
A_{\frac{3}{2}N} B_{\frac{3}{2}N}

Output

Print T lines. For the i-th line, if there is a color sequence that cannot result from performing the operation for the i-th test case, print such a color sequence; otherwise, print -1. If there are multiple color sequences that cannot result from performing the operation, any of them will be considered correct.


Sample Input 1

2
4
1 2
1 3
1 4
2 3
2 4
3 4
10
1 2
1 3
1 4
2 3
2 4
3 5
4 5
5 6
6 7
6 8
7 9
7 10
8 9
8 10
9 10

Sample Output 1

BWWW
BWWWBWWWBB

Let us consider the first test case. For the color of vertex 1 to be B, at least two of the colors of vertices 2, 3, 4 must be B before the operation. Then, for at least one of the vertices 2, 3, 4, at least two of the colors of the vertices connected to that vertex by an edge are B, so the color of that vertex after the operation will be B. Therefore, the color sequence BWWW cannot result from performing the operation.

F - Everywhere is Sparser than Whole (Judge)

Time Limit: 8 sec / Memory Limit: 1024 MB

配点 : 900

問題文

頂点集合が空でない単純無向グラフの密度\displaystyle\frac{(辺数)}{(頂点数)} と定義します.

正整数 N, D と,N 頂点 DN 辺の単純無向グラフ G が与えられます. G の頂点には 1 から N までの番号が付いており,i 番目の辺は頂点 A_i と頂点 B_i を結んでいます. G が以下の条件を満たしているかどうかを判定してください.

条件: G の頂点集合を V とする. V の任意の空でない部分集合 X に対して,X による G の誘導部分グラフの密度は D 未満である.

T 個のテストケースが与えられるので,それぞれについて答えてください.

誘導部分グラフとは

グラフ G の頂点部分集合 X に対して,X による G誘導部分グラフとは,「頂点集合が X であり,辺集合が『 G の辺であって X 内の 2 頂点を結ぶもの全体』であるグラフ」を指します. 上記の条件では,頂点部分集合として空集合でも全体でもないもののみを考えていることに注意してください.

制約

  • T \geq 1
  • N \geq 1
  • D \geq 1
  • 1 つの入力に含まれるテストケースについて,DN の総和は 5 \times 10^4 以下である.
  • 1 \leq A_i < B_i \leq N \ \ (1 \leq i \leq DN)
  • (A_i, B_i) \neq (A_j, B_j) \ \ (1 \leq i < j \leq DN)

入力

入力は以下の形式で標準入力から与えられる.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケース \mathrm{case}_i \ (1 \leq i \leq T) は以下の形式である.

N D
A_1 B_1
A_2 B_2
\vdots
A_{DN} B_{DN}

出力

T 行出力せよ. i 行目には,i 番目のテストケースについて,与えられたグラフ G が条件を満たすなら Yes を,満たさないなら No を出力せよ.


入力例 1

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

出力例 1

Yes
No
  • 1 つ目のテストケースは問題 D の出力例 1 と同じであり,条件を満たします.
  • 2 つ目のテストケースについて,頂点集合 \{1, 2, 3, 4\} の空でない真部分集合 \{1, 2, 3\} による誘導部分グラフの辺集合は \{(1, 2), (1, 3), (2, 3)\} であり,その密度は \displaystyle\frac{3}{3} = 1 = D です. したがって,このグラフは条件を満たしません.

Score : 900 points

Problem Statement

We define the density of a non-empty simple undirected graph as \displaystyle\frac{(\text{number\ of\ edges})}{(\text{number\ of\ vertices})}.

You are given positive integers N, D, and a simple undirected graph G with N vertices and DN edges. The vertices of G are numbered from 1 to N, and the i-th edge connects vertex A_i and vertex B_i. Determine whether G satisfies the following condition.

Condition: Let V be the vertex set of G. For any non-empty proper subset X of V, the density of the induced subgraph of G by X is strictly less than D.

There are T test cases to solve.

What is an induced subgraph?

For a vertex subset X of graph G, the induced subgraph of G by X is the graph with vertex set X and edge set containing all edges of G that connect two vertices in X. In the above condition, note that we only consider vertex subsets that are neither empty nor the entire set.

Constraints

  • T \geq 1
  • N \geq 1
  • D \geq 1
  • The sum of DN over the test cases in each input file is at most 5 \times 10^4.
  • 1 \leq A_i < B_i \leq N \ \ (1 \leq i \leq DN)
  • (A_i, B_i) \neq (A_j, B_j) \ \ (1 \leq i < j \leq DN)

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case \mathrm{case}_i \ (1 \leq i \leq T) is in the following format:

N D
A_1 B_1
A_2 B_2
\vdots
A_{DN} B_{DN}

Output

Print T lines. The i-th line should contain Yes if the given graph G for the i-th test case satisfies the condition, and No otherwise.


Sample Input 1

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

Sample Output 1

Yes
No
  • The first test case is the same as Sample Input 1 in Problem D, and it satisfies the condition.
  • For the second test case, the edge set of the induced subgraph by the non-empty proper subset \{1, 2, 3\} of the vertex set \{1, 2, 3, 4\} is \{(1, 2), (1, 3), (2, 3)\}, and its density is \displaystyle\frac{3}{3} = 1 = D. Therefore, this graph does not satisfy the condition.