Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
A の中に同じ要素が 3 つ以上連続する箇所が存在するか判定してください。
より厳密には、 1 以上 N-2 以下の整数 i であって A_i=A_{i+1}=A_{i+2} を満たすものが存在するか判定してください。
制約
- 3\le N\le 100
- 1\le A_i\le 100
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
A の中に同じ要素が 3 つ以上連続する箇所が存在するならば Yes を、存在しないならば No を出力せよ。
入力例 1
5 1 4 4 4 2
出力例 1
Yes
A=(1,4,4,4,2) です。 4 が 3 つ連続している箇所が存在するので、 Yes を出力してください。
入力例 2
6 2 4 4 2 2 4
出力例 2
No
A=(2,4,4,2,2,4) です。同じ要素が 3 つ以上連続している箇所は存在しないので、 No を出力してください。
入力例 3
8 1 4 2 5 7 7 7 2
出力例 3
Yes
入力例 4
10 1 2 3 4 5 6 7 8 9 10
出力例 4
No
入力例 5
13 1 1 1 1 1 1 1 1 1 1 1 1 1
出力例 5
Yes
Score : 100 points
Problem Statement
You are given an integer sequence of length N: A = (A_1,A_2,\ldots,A_N).
Determine whether there is a place in A where the same element appears three or more times in a row.
More formally, determine whether there exists an integer i with 1 \le i \le N-2 such that A_i = A_{i+1} = A_{i+2}.
Constraints
- 3 \le N \le 100
- 1 \le A_i \le 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
If there is a place in A where the same element appears three or more times in a row, print Yes. Otherwise, print No.
Sample Input 1
5 1 4 4 4 2
Sample Output 1
Yes
We have A=(1,4,4,4,2). There is a place where 4 appears three times in a row, so print Yes.
Sample Input 2
6 2 4 4 2 2 4
Sample Output 2
No
We have A=(2,4,4,2,2,4). There is no place where the same element appears three or more times in a row, so print No.
Sample Input 3
8 1 4 2 5 7 7 7 2
Sample Output 3
Yes
Sample Input 4
10 1 2 3 4 5 6 7 8 9 10
Sample Output 4
No
Sample Input 5
13 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 5
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋くんは、N 個の品物と 1 つのカバンを持っています。
i 番目 (1\le i\le N) の品物の大きさは A _ i で、カバンの大きさは M です。
カバンに入れようとしている品物の大きさの合計が M 以下のとき、かつそのときに限り、それらの品物をすべて同時にカバンに入れることができます。
高橋くんが N 個の品物すべてを同時にカバンに入れることができるなら Yes 、そうでなければ No と出力してください。
制約
- 1\le N\le100
- 1\le M\le10000
- 1\le A _ i\le100\ (1\le i\le N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A _ 1 A _ 2 \ldots A _ N
出力
高橋くんがすべての品物を同時にカバンに入れられるなら Yes を、そうでなければ No を出力せよ。
入力例 1
5 15 3 1 4 1 5
出力例 1
Yes
5 つの品物の大きさの合計は 3+1+4+1+5=14 です。
これは、カバンの大きさ 15 以下なので、高橋くんはすべての品物を同時にカバンに入れることができます。
なので、Yes を出力してください。
入力例 2
5 5 3 1 4 1 5
出力例 2
No
5 つの品物の大きさの合計は 14 で、カバンの大きさ 5 より大きいため、高橋くんはすべての品物を同時にカバンに入れることができません。
なので、No を出力してください。
入力例 3
1 10000 100
出力例 3
Yes
Score : 100 points
Problem Statement
Takahashi has N items and one bag.
The size of the i-th (1\le i\le N) item is A_i, and the size of the bag is M.
If and only if the total size of the items he is trying to put in the bag is at most M, he can put all those items in the bag simultaneously.
If he can put all N items in the bag simultaneously, print Yes; otherwise, print No.
Constraints
- 1\le N\le100
- 1\le M\le10000
- 1\le A_i\le100\ (1\le i\le N)
- All input values are integers.
Input
The input is given from standard input in the following format:
N M A_1 A_2 \ldots A_N
Output
If Takahashi can put all items in the bag simultaneously, print Yes; otherwise, print No.
Sample Input 1
5 15 3 1 4 1 5
Sample Output 1
Yes
The total size of the 5 items is 3+1+4+1+5=14.
Since this is not greater than the bag size 15, Takahashi can put all items in the bag simultaneously.
Thus, print Yes.
Sample Input 2
5 5 3 1 4 1 5
Sample Output 2
No
The total size of the 5 items is 14, which is greater than the bag size 5, so he cannot put all items in the bag simultaneously.
Thus, print No.
Sample Input 3
1 10000 100
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数 N が与えられるので、 2^k \le N となる最大の整数 k を求めてください。
制約
- N は 1 \le N \le 10^{18} を満たす整数である
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを整数として出力せよ。
入力例 1
6
出力例 1
2
- k=2 は 2^2=4 \le 6 を満たします。
- k \ge 3 である全ての整数 k について 2^k > 6 となります。
以上より、答えは k=2 となります。
入力例 2
1
出力例 2
0
2^0=1 であることに注意してください。
入力例 3
1000000000000000000
出力例 3
59
入力が 32 bit 整数に収まらない場合があります。
Score : 200 points
Problem Statement
Given a positive integer N, find the maximum integer k such that 2^k \le N.
Constraints
- N is an integer satisfying 1 \le N \le 10^{18}.
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer as an integer.
Sample Input 1
6
Sample Output 1
2
- k=2 satisfies 2^2=4 \le 6.
- For every integer k such that k \ge 3, 2^k > 6 holds.
Therefore, the answer is k=2.
Sample Input 2
1
Sample Output 2
0
Note that 2^0=1.
Sample Input 3
1000000000000000000
Sample Output 3
59
The input value may not fit into a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
1\leq l\leq r\leq N をみたす整数の組 (l,r) であって、
次の条件をみたすものの個数を求めてください。
l\leq i\leq r をみたす任意の整数 i について、A_i は A_l+A_{l+1}+\cdots+A_r の約数でない。
制約
- 1 \leq N \leq 50
- 1 \leq A_i \leq 1000
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
5 8 6 10 5 7
出力例 1
6
A=(8,6,10,5,7) です。
例えば、(l,r)=(1,2) は、A_l+A_{l+1}+\cdots+A_r=A_1+A_2=14 であり、A_1=8, A_2=6 はどちらも 14 の約数でないため、条件をみたします。
一方で、(l,r)=(1,3) は、A_l+A_{l+1}+\cdots+A_r=A_1+A_2+A_3=24 であり、A_1=8 が 24 の約数であるため、条件をみたしません。
条件をみたす組は (l,r)=(1,2), (1,4), (2,3), (2,4), (3,5), (4,5) の 6 つであるため、6 を出力します。
入力例 2
3 1 1 1
出力例 2
0
Score : 200 points
Problem Statement
You are given a sequence of positive integers A=(A_1,A_2,\ldots,A_N) of length N.
Find the number of pairs of integers (l,r) satisfying 1\leq l\leq r\leq N that satisfy the following condition:
For every integer i satisfying l\leq i\leq r, A_i is not a divisor of A_l+A_{l+1}+\cdots+A_r.
Constraints
- 1 \leq N \leq 50
- 1 \leq A_i \leq 1000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Output the answer.
Sample Input 1
5 8 6 10 5 7
Sample Output 1
6
We have A=(8,6,10,5,7).
For example, (l,r)=(1,2) satisfies the condition because A_l+A_{l+1}+\cdots+A_r=A_1+A_2=14, and neither A_1=8 nor A_2=6 is a divisor of 14.
On the other hand, (l,r)=(1,3) does not satisfy the condition because A_l+A_{l+1}+\cdots+A_r=A_1+A_2+A_3=24, and A_1=8 is a divisor of 24.
The pairs that satisfy the condition are (l,r)=(1,2), (1,4), (2,3), (2,4), (3,5), (4,5), which is six pairs, so output 6.
Sample Input 2
3 1 1 1
Sample Output 2
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
ある OS のバージョンは N 個あり、古い順に 1,2,\dots,N の番号がついています。
PC が N 台あり、初めは i 番目の PC の OS のバージョンは i です。
Q 回の操作を順に行ってください。そのうち i 回目は次の通りです。
- 現時点での OS のバージョンが X_i かそれ以前の PC 全てを、バージョンを Y_i(>X_i) にアップグレードする。その後、この操作でアップグレードを行った PC の台数を出力する。
i<Q について、 i 回目の操作でのアップグレードが行われた状態で i+1 回目の操作に進むことに注意してください。
制約
- 入力は全て整数
- 2 \le N \le 10^6
- 1 \le Q \le 2 \times 10^5
- 1 \le X_i < Y_i \le N
入力
入力は以下の形式で標準入力から与えられる。
N Q X_1 Y_1 X_2 Y_2 \vdots X_Q Y_Q
出力
Q 行出力せよ。
そのうち i 行目には、 i 回目の操作でアップグレードを行った PC の台数を出力せよ。
入力例 1
8 5 2 6 3 5 1 7 5 7 7 8
出力例 1
2 1 0 3 7
この入力には 5 回の操作が含まれます。
- はじめ、 8 台の PC のバージョンは 1,2,3,4,5,6,7,8 です。
- 1 回目の操作で、バージョンが 2 かそれ以前のバージョンの PC をバージョン 6 にアップグレードします。
- この操作によって 2 台の PC がアップグレードされ、各 PC のバージョンは 6,6,3,4,5,6,7,8 となります。
- 2 回目の操作で、バージョンが 3 かそれ以前のバージョンの PC をバージョン 5 にアップグレードします。
- この操作によって 1 台の PC がアップグレードされ、各 PC のバージョンは 6,6,5,4,5,6,7,8 となります。
- 3 回目の操作で、バージョンが 1 かそれ以前のバージョンの PC をバージョン 7 にアップグレードします。
- この操作によって 0 台の PC がアップグレードされ、各 PC のバージョンは 6,6,5,4,5,6,7,8 となります。
- 4 回目の操作で、バージョンが 5 かそれ以前のバージョンの PC をバージョン 7 にアップグレードします。
- この操作によって 3 台の PC がアップグレードされ、各 PC のバージョンは 6,6,7,7,7,6,7,8 となります。
- 5 回目の操作で、バージョンが 7 かそれ以前のバージョンの PC をバージョン 8 にアップグレードします。
- この操作によって 7 台の PC がアップグレードされ、各 PC のバージョンは 8,8,8,8,8,8,8,8 となります。
Score : 300 points
Problem Statement
There are N versions of a certain OS, numbered 1,2,\dots,N in chronological order.
There are N PCs, and initially the OS version of the i-th PC is i.
Perform Q operations in order. The i-th operation is as follows:
- Upgrade all PCs whose current OS version is X_i or earlier to version Y_i(>X_i). Then, print the number of PCs upgraded in this operation.
Note that for i<Q, the upgrades from the i-th operation are performed before proceeding to the (i+1)-th operation.
Constraints
- All input values are integers.
- 2 \le N \le 10^6
- 1 \le Q \le 2 \times 10^5
- 1 \le X_i < Y_i \le N
Input
The input is given from Standard Input in the following format:
N Q X_1 Y_1 X_2 Y_2 \vdots X_Q Y_Q
Output
Output Q lines.
The i-th line should contain the number of PCs upgraded in the i-th operation.
Sample Input 1
8 5 2 6 3 5 1 7 5 7 7 8
Sample Output 1
2 1 0 3 7
This input contains five operations.
- Initially, the versions of the eight PCs are 1,2,3,4,5,6,7,8.
- In the first operation, PCs with version 2 or earlier are upgraded to version 6.
- This operation upgrades two PCs, and the versions of the PCs become 6,6,3,4,5,6,7,8.
- In the second operation, PCs with version 3 or earlier are upgraded to version 5.
- This operation upgrades one PC, and the versions of the PCs become 6,6,5,4,5,6,7,8.
- In the third operation, PCs with version 1 or earlier are upgraded to version 7.
- This operation upgrades zero PCs, and the versions of the PCs become 6,6,5,4,5,6,7,8.
- In the fourth operation, PCs with version 5 or earlier are upgraded to version 7.
- This operation upgrades three PCs, and the versions of the PCs become 6,6,7,7,7,6,7,8.
- In the fifth operation, PCs with version 7 or earlier are upgraded to version 8.
- This operation upgrades seven PCs, and the versions of the PCs become 8,8,8,8,8,8,8,8.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
この問題における 11/22 文字列の定義は A 問題および E 問題と同じです。
文字列 T が以下の条件を全て満たすとき、T を 11/22 文字列 と呼びます。
- |T| は奇数である。ここで、|T| は T の長さを表す。
- 1 文字目から \frac{|T|+1}{2} - 1 文字目までが
1である。 - \frac{|T|+1}{2} 文字目が
/である。 - \frac{|T|+1}{2} + 1 文字目から |T| 文字目までが
2である。
例えば 11/22, 111/222, / は 11/22 文字列ですが、1122, 1/22, 11/2222, 22/11, //2/2/211 はそうではありません。
1, 2, / からなる長さ N の文字列 S が与えられます。S は / を 1 個以上含みます。
11/22 文字列であるような S の(連続な)部分文字列の長さの最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- S は
1,2,/からなる長さ N の文字列 - S は
/を 1 個以上含む
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
11/22 文字列であるような S の(連続な)部分文字列の長さの最大値を出力せよ。
入力例 1
8 211/2212
出力例 1
5
S の 2 文字目から 6 文字目からなる部分文字列は 11/22 で、これは 11/22 文字列です。S の部分文字列のうち 11/22 文字列であるものはこれが最長です。よって 5 が答えです。
入力例 2
5 22/11
出力例 2
1
入力例 3
22 /1211/2///2111/2222/11
出力例 3
7
Score : 300 points
Problem Statement
The definition of an 11/22 string in this problem is the same as in Problems A and E.
A string T is called an 11/22 string when it satisfies all of the following conditions:
- |T| is odd. Here, |T| denotes the length of T.
- The 1-st through (\frac{|T|+1}{2} - 1)-th characters are all
1. - The (\frac{|T|+1}{2})-th character is
/. - The (\frac{|T|+1}{2} + 1)-th through |T|-th characters are all
2.
For example, 11/22, 111/222, and / are 11/22 strings, but 1122, 1/22, 11/2222, 22/11, and //2/2/211 are not.
You are given a string S of length N consisting of 1, 2, and /, where S contains at least one /.
Find the maximum length of a (contiguous) substring of S that is an 11/22 string.
Constraints
- 1 \leq N \leq 2 \times 10^5
- S is a string of length N consisting of
1,2, and/. - S contains at least one
/.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the maximum length of a (contiguous) substring of S that is an 11/22 string.
Sample Input 1
8 211/2212
Sample Output 1
5
The substring from the 2-nd to 6-th character of S is 11/22, which is an 11/22 string. Among all substrings of S that are 11/22 strings, this is the longest. Therefore, the answer is 5.
Sample Input 2
5 22/11
Sample Output 2
1
Sample Input 3
22 /1211/2///2111/2222/11
Sample Output 3
7
Time Limit: 2.5 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
正整数 x,y に対して f(x,y) を以下で定義します。
- 十進表記の x,y をそれぞれ文字列として解釈しこの順に連結して得られる文字列を z とする。z を十進表記の整数として解釈したときの値を f(x,y) とする。
たとえば f(3,14)=314,\ f(100,3)=1003 です。
正の整数 C, D が与えられます。以下を満たす整数 x の個数を求めてください。
- 1 \leq x \leq D
- f(C, C+x) は平方数である
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \leq T \leq 3 \times 10^5
- 1 \leq C \leq 2 \times 10^8
- 1 \leq D \leq 5 \times 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\textrm{case}_1
\textrm{case}_2
\vdots
\textrm{case}_T
\textrm{case}_i は i 番目のテストケースを表す。各テストケースは以下の形式で与えられる。
C D
出力
T 行出力せよ。i 行目 (1 \leq i \leq T) には i 番目のテストケースに対する答えを出力せよ。
入力例 1
4 4 80 183 5000 18 10 824 5000000000
出力例 1
3 2 0 1421
1 番目のテストケースにおいて、条件を満たす x は x = 5, 37, 80 の 3 通りです。
- x=5 のとき f(C, C+5) = f(4,9) = 49 = 7^2
- x=37 のとき f(C, C+37) = f(4,41) = 441 = 21^2
- x=80 のとき f(C, C+80) = f(4,84) = 484 = 22^2
2 番目のテストケースにおいて、条件を満たす x は x = 1, 3133 の 2 通りです。
- x=1 のとき f(C, C+1) = f(183,184) = 183184 = 428^2
- x=3133 のとき f(C, C+3133) = f(183,3316) = 1833316 = 1354^2
3 番目のテストケースにおいて、条件を満たす x は 0 通りです。
4 番目のテストケースにおいて、条件を満たす x は 1421 通りです。
Score : 400 points
Problem Statement
For positive integers x and y, define f(x,y) as follows:
- Let z be the string obtained by interpreting x,y in decimal notation as strings and concatenating them in this order. Let f(x,y) be the value when z is interpreted as an integer in decimal notation.
For example, f(3,14)=314,\ f(100,3)=1003.
You are given positive integers C and D. Find the number of integers x that satisfy the following conditions:
- 1 \leq x \leq D
- f(C, C+x) is a perfect square.
You are given T test cases, find the answer for each of them.
Constraints
- 1 \leq T \leq 3 \times 10^5
- 1 \leq C \leq 2 \times 10^8
- 1 \leq D \leq 5 \times 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\textrm{case}_1
\textrm{case}_2
\vdots
\textrm{case}_T
\textrm{case}_i represents the i-th test case. Each test case is given in the following format:
C D
Output
Output T lines. The i-th line (1 \leq i \leq T) should contain the answer to the i-th test case.
Sample Input 1
4 4 80 183 5000 18 10 824 5000000000
Sample Output 1
3 2 0 1421
For the first test case, there are three values of x that satisfy the conditions: x = 5, 37, 80.
- When x=5, f(C, C+5) = f(4,9) = 49 = 7^2
- When x=37, f(C, C+37) = f(4,41) = 441 = 21^2
- When x=80, f(C, C+80) = f(4,84) = 484 = 22^2
For the second test case, there are two values of x that satisfy the conditions: x = 1, 3133.
- When x=1, f(C, C+1) = f(183,184) = 183184 = 428^2
- When x=3133, f(C, C+3133) = f(183,3316) = 1833316 = 1354^2
For the third test case, there are zero values of x that satisfy the conditions.
For the fourth test case, there are 1421 values of x that satisfy the conditions.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
英小文字からなる N 個の文字列 S_1, S_2, \ldots, S_N 、および、英小文字からなる文字列 T が与えられます。
1 以上 N 以下の 2 つの整数からなる組 (i, j) は N^2 個ありますが、そのうち下記の条件を満たすものの個数を出力してください。
- S_i と S_j をこの順に連結して得られる文字列は、T を(連続とは限らない)部分列として含む。
制約
- N は整数
- 1 \leq N \leq 5 \times 10^5
- S_i および T は英小文字からなる長さ 1 以上 5 \times 10^5 以下の文字列
- S_1, S_2, \ldots, S_N の長さの総和は 5 \times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
N T S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
3 bac abba bcb aaca
出力例 1
3
問題文中の条件を満たす組 (i, j) は、下記に示す 3 個の組 (1, 2), (1, 3), (2, 3) です。
- (i, j) = (1, 2) について、S_1 と S_2 をこの順に連結して得られる文字列
abbabcbはbacを部分列として含みます。 - (i, j) = (1, 3) について、S_1 と S_3 をこの順に連結して得られる文字列
abbaaacaはbacを部分列として含みます。 - (i, j) = (2, 3) について、S_2 と S_3 をこの順に連結して得られる文字列
bcbaacaはbacを部分列として含みます。
入力例 2
5 xx x x x x x
出力例 2
25
入力例 3
1 y x
出力例 3
0
入力例 4
10 ms mkgn m hlms vmsle mxsm nnzdhi umsavxlb ffnsybomr yvmm naouel
出力例 4
68
Score : 500 points
Problem Statement
You are given N strings S_1, S_2, \ldots, S_N consisting of lowercase English letters, and a string T consisting of lowercase English letters.
There are N^2 pairs (i, j) of integers between 1 and N, inclusive. Print the number of pairs among them that satisfy the following condition.
- The concatenation of S_i and S_j in this order contains T as a (not necessarily contiguous) subsequence.
Constraints
- N is an integer.
- 1 \leq N \leq 5 \times 10^5
- S_i and T are strings of length 1 to 5 \times 10^5, inclusive, consisting of lowercase English letters.
- The total length of S_1, S_2, \ldots, S_N is at most 5 \times 10^5.
Input
The input is given from Standard Input in the following format:
N T S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
3 bac abba bcb aaca
Sample Output 1
3
The pairs (i, j) that satisfy the condition in the problem statement are (1, 2), (1, 3), (2, 3), as seen below.
- For (i, j) = (1, 2), the concatenation
abbabcbof S_1 and S_2 in this order containsbacas a subsequence. - For (i, j) = (1, 3), the concatenation
abbaaacaof S_1 and S_3 in this order containsbacas a subsequence. - For (i, j) = (2, 3), the concatenation
bcbaacaof S_2 and S_3 in this order containsbacas a subsequence.
Sample Input 2
5 xx x x x x x
Sample Output 2
25
Sample Input 3
1 y x
Sample Output 3
0
Sample Input 4
10 ms mkgn m hlms vmsle mxsm nnzdhi umsavxlb ffnsybomr yvmm naouel
Sample Output 4
68
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の無向グラフ G が与えられます。 G は単純(自己ループおよび多重辺を持たない)かつ連結です。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ無向辺 \lbrace u_i, v_i \rbrace です。
下記の 2 つの条件をともに満たすような G の 2 つの全域木 T_1,T_2 を 1 組構成してください。( T_1 と T_2 は異なる全域木である必要はありません。)
-
T_1 は下記を満たす。
T_1 を頂点 1 を根とする根付き木とみなしたとき、G の辺のうち T_1 に含まれないすべての辺 \lbrace u, v \rbrace について、u と v は T_1 において祖先と子孫の関係にある。
-
T_2 は下記を満たす。
T_2 を頂点 1 を根とする根付き木とみなしたとき、G の辺のうち T_2 に含まれない辺 \lbrace u, v \rbrace であって、u と v が T_2 において祖先と子孫の関係にあるようなものは存在しない。
ここで、「根付き木 T において頂点 u と頂点 v が祖先と子孫の関係にある」とは、「 T において u が v の祖先である」と「 T において v が u の祖先である」のうちどちらかが成り立つことをいいます。
本問題の制約下において上記の条件を満たす T_1 と T_2 は必ず存在することが示せます。
制約
- 2 \leq N \leq 2 \times 10^5
- N-1 \leq M \leq \min\lbrace 2 \times 10^5, N(N-1)/2 \rbrace
- 1 \leq u_i, v_i \leq N
- 入力はすべて整数
- 与えられるグラフは単純かつ連結
入力
入力は以下の形式で標準入力から与えられます。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
T_1 と T_2 を下記の形式にしたがって、2N-2 行にわたって出力してください。すなわち、
- 1 行目から N-1 行目には、T_1 に含まれる N-1 本の無向辺 \lbrace x_1, y_1\rbrace, \lbrace x_2, y_2\rbrace, \ldots, \lbrace x_{N-1}, y_{N-1}\rbrace を、各行に 1 本ずつ出力してください。
- N 行目から 2N-2 行目には、T_2 に含まれる N-1 本の無向辺 \lbrace z_1, w_1\rbrace, \lbrace z_2, w_2\rbrace, \ldots, \lbrace z_{N-1}, w_{N-1}\rbrace を、各行に 1 本ずつ出力してください。
各全域木を構成する辺をどのような順番で出力するかや、各辺の出力においてどちらの端点を先に出力するかは任意です。
x_1 y_1
x_2 y_2
\vdots
x_{N-1} y_{N-1}
z_1 w_1
z_2 w_2
\vdots
z_{N-1} w_{N-1}
入力例 1
6 8 5 1 4 3 1 4 3 5 1 2 2 6 1 6 4 2
出力例 1
1 4 4 3 5 3 4 2 6 2 1 5 5 3 1 4 2 1 1 6
上記の出力例において、T_1 は 5 本の辺 \lbrace 1, 4 \rbrace, \lbrace 4, 3 \rbrace, \lbrace 5, 3 \rbrace, \lbrace 4, 2 \rbrace, \lbrace 6, 2 \rbrace を持つ G の全域木です。この T_1 は問題文中の条件を満たします。実際、G の辺のうち T_1 に含まれない各辺に関して、
- 辺 \lbrace 5, 1 \rbrace について、頂点 1 は頂点 5 の祖先であり、
- 辺 \lbrace 1, 2 \rbrace について、頂点 1 は頂点 2 の祖先であり、
- 辺 \lbrace 1, 6 \rbrace について、頂点 1 は頂点 6 の祖先です。
また、T_2 は 5 本の辺 \lbrace 1, 5 \rbrace, \lbrace 5, 3 \rbrace, \lbrace 1, 4 \rbrace, \lbrace 2, 1 \rbrace, \lbrace 1, 6 \rbrace を持つ G の全域木です。この T_2 は問題文中の条件を満たします。実際、G の辺のうち T_2 に含まれない各辺に関して、
- 辺 \lbrace 4, 3 \rbrace について、頂点 4 と頂点 3 は祖先と子孫の関係になく、
- 辺 \lbrace 2, 6 \rbrace について、頂点 2 と頂点 6 は祖先と子孫の関係になく、
- 辺 \lbrace 4, 2 \rbrace について、頂点 4 と頂点 2 は祖先と子孫の関係にありません。
入力例 2
4 3 3 1 1 2 1 4
出力例 2
1 2 1 3 1 4 1 4 1 3 1 2
3 本の辺 \lbrace 1, 2\rbrace, \lbrace 1, 3 \rbrace, \lbrace 1, 4 \rbrace を持つ木 T が G の唯一の全域木です。 G の辺のうちこの木 T に含まれない辺は存在しないので、明らかに、T は T_1 の条件と T_2 の条件をともに満たします。
Score : 500 points
Problem Statement
You are given an undirected graph G with N vertices and M edges. G is simple (it has no self-loops and multiple edges) and connected.
For i = 1, 2, \ldots, M, the i-th edge is an undirected edge \lbrace u_i, v_i \rbrace connecting Vertices u_i and v_i.
Construct two spanning trees T_1 and T_2 of G that satisfy both of the two conditions below. (T_1 and T_2 do not necessarily have to be different spanning trees.)
-
T_1 satisfies the following.
If we regard T_1 as a rooted tree rooted at Vertex 1, for any edge \lbrace u, v \rbrace of G not contained in T_1, one of u and v is an ancestor of the other in T_1.
-
T_2 satisfies the following.
If we regard T_2 as a rooted tree rooted at Vertex 1, there is no edge \lbrace u, v \rbrace of G not contained in T_2 such that one of u and v is an ancestor of the other in T_2.
We can show that there always exists T_1 and T_2 that satisfy the conditions above.
Constraints
- 2 \leq N \leq 2 \times 10^5
- N-1 \leq M \leq \min\lbrace 2 \times 10^5, N(N-1)/2 \rbrace
- 1 \leq u_i, v_i \leq N
- All values in input are integers.
- The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
Output
Print (2N-2) lines to output T_1 and T_2 in the following format. Specifically,
- The 1-st through (N-1)-th lines should contain the (N-1) undirected edges \lbrace x_1, y_1\rbrace, \lbrace x_2, y_2\rbrace, \ldots, \lbrace x_{N-1}, y_{N-1}\rbrace contained in T_1, one edge in each line.
- The N-th through (2N-2)-th lines should contain the (N-1) undirected edges \lbrace z_1, w_1\rbrace, \lbrace z_2, w_2\rbrace, \ldots, \lbrace z_{N-1}, w_{N-1}\rbrace contained in T_2, one edge in each line.
You may print edges in each spanning tree in any order. Also, you may print the endpoints of each edge in any order.
x_1 y_1
x_2 y_2
\vdots
x_{N-1} y_{N-1}
z_1 w_1
z_2 w_2
\vdots
z_{N-1} w_{N-1}
Sample Input 1
6 8 5 1 4 3 1 4 3 5 1 2 2 6 1 6 4 2
Sample Output 1
1 4 4 3 5 3 4 2 6 2 1 5 5 3 1 4 2 1 1 6
In the Sample Output above, T_1 is a spanning tree of G containing 5 edges \lbrace 1, 4 \rbrace, \lbrace 4, 3 \rbrace, \lbrace 5, 3 \rbrace, \lbrace 4, 2 \rbrace, \lbrace 6, 2 \rbrace. This T_1 satisfies the condition in the Problem Statement. Indeed, for each edge of G not contained in T_1:
- for edge \lbrace 5, 1 \rbrace, Vertex 1 is an ancestor of 5;
- for edge \lbrace 1, 2 \rbrace, Vertex 1 is an ancestor of 2;
- for edge \lbrace 1, 6 \rbrace, Vertex 1 is an ancestor of 6.
T_2 is another spanning tree of G containing 5 edges \lbrace 1, 5 \rbrace, \lbrace 5, 3 \rbrace, \lbrace 1, 4 \rbrace, \lbrace 2, 1 \rbrace, \lbrace 1, 6 \rbrace. This T_2 satisfies the condition in the Problem Statement. Indeed, for each edge of G not contained in T_2:
- for edge \lbrace 4, 3 \rbrace, Vertex 4 is not an ancestor of Vertex 3, and vice versa;
- for edge \lbrace 2, 6 \rbrace, Vertex 2 is not an ancestor of Vertex 6, and vice versa;
- for edge \lbrace 4, 2 \rbrace, Vertex 4 is not an ancestor of Vertex 2, and vice versa.
Sample Input 2
4 3 3 1 1 2 1 4
Sample Output 2
1 2 1 3 1 4 1 4 1 3 1 2
Tree T, containing 3 edges \lbrace 1, 2\rbrace, \lbrace 1, 3 \rbrace, \lbrace 1, 4 \rbrace, is the only spanning tree of G. Since there are no edges of G that are not contained in T, obviously this T satisfies the conditions for both T_1 and T_2.