Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
数字からなる文字列 S が与えられます。
S から 2 以外の文字を削除し、残った文字を順序を保って結合した文字列を求めてください。
制約
- S は数字からなる長さ 1 以上 100 以下の文字列
- S は
2を 1 つ以上含む
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
20250222
出力例 1
22222
20250222 から 0, 5, 0 を削除し、残った文字を順序を保って結合することで文字列 22222 が得られます。
入力例 2
2
出力例 2
2
入力例 3
22222000111222222
出力例 3
22222222222
Score : 100 points
Problem Statement
You are given a string S consisting of digits.
Remove all characters from S except for 2, and then concatenate the remaining characters in their original order to form a new string.
Constraints
- S is a string consisting of digits with length between 1 and 100, inclusive.
- S contains at least one
2.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
20250222
Sample Output 1
22222
By removing 0, 5, and 0 from 20250222 and then concatenating the remaining characters in their original order, the string 22222 is obtained.
Sample Input 2
2
Sample Output 2
2
Sample Input 3
22222000111222222
Sample Output 3
22222222222
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
長さ N の英小文字からなる文字列 S が与えられます。
S の i 文字目を S_i と記します。
S_1,S_1,S_2,S_2,\dots,S_N,S_N をこの順に連結した長さ 2N の文字列を出力してください。
例えば、 S が beginner のときは bbeeggiinnnneerr と出力してください。
制約
- N は 1 \le N \le 50 を満たす整数
- S は長さ N の英小文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
8 beginner
出力例 1
bbeeggiinnnneerr
問題文中の例と同じです。
入力例 2
3 aaa
出力例 2
aaaaaa
Score : 100 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters.
We denote the i-th character of S by S_i.
Print the string of length 2N obtained by concatenating S_1,S_1,S_2,S_2,\dots,S_N, and S_N in this order.
For example, if S is beginner, print bbeeggiinnnneerr.
Constraints
- N is an integer such that 1 \le N \le 50.
- S is a string of length N consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer.
Sample Input 1
8 beginner
Sample Output 1
bbeeggiinnnneerr
It is the same as the example described in the problem statement.
Sample Input 2
3 aaa
Sample Output 2
aaaaaa
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
H 行 W 列の行列 A が与えられます。
A の上から i 行目、左から j 列目の要素は A_{i,j} です。
ここで、W 行 H 列の行列 B を、上から i 行目、左から j 列目の要素が A_{j,i} と一致するような行列として定めます。
すなわち、B は A の転置行列です。
B を出力してください。
制約
- 1\leq H,W \leq 10^5
- H \times W \leq 10^5
- 1 \leq A_{i,j} \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}
出力
B を以下の形式で出力せよ。
B_{1,1} B_{1,2} \ldots B_{1,H}
B_{2,1} B_{2,2} \ldots B_{2,H}
\vdots
B_{W,1} B_{W,2} \ldots B_{W,H}
入力例 1
4 3 1 2 3 4 5 6 7 8 9 10 11 12
出力例 1
1 4 7 10 2 5 8 11 3 6 9 12
たとえば A_{2,1}=4 なので、転置行列 B の上から 1 行目、左から 2 列目の要素は 4 になります。
入力例 2
2 2 1000000000 1000000000 1000000000 1000000000
出力例 2
1000000000 1000000000 1000000000 1000000000
Score : 200 points
Problem Statement
You are given an H-by-W matrix A.
The element at the i-th row from the top and j-th column from the left of A is A_{i,j}.
Let B be a W-by-H matrix whose element at the i-th row from the top and j-th column from the left equals A_{j, i}.
That is, B is the transpose of A.
Print B.
Constraints
- 1\leq H,W \leq 10^5
- H \times W \leq 10^5
- 1 \leq A_{i,j} \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}
Output
Print B in the following format:
B_{1,1} B_{1,2} \ldots B_{1,H}
B_{2,1} B_{2,2} \ldots B_{2,H}
\vdots
B_{W,1} B_{W,2} \ldots B_{W,H}
Sample Input 1
4 3 1 2 3 4 5 6 7 8 9 10 11 12
Sample Output 1
1 4 7 10 2 5 8 11 3 6 9 12
For example, we have A_{2,1}=4, so the element at the 1-st row from the top and 2-nd column from the left of the transpose B is 4.
Sample Input 2
2 2 1000000000 1000000000 1000000000 1000000000
Sample Output 2
1000000000 1000000000 1000000000 1000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
英小文字からなる N 個の奇数長の文字列 S_1,S_2,\dots,S_N が与えられます。
S_1,S_2,\dots,S_N のうち最も長いものの長さを m とします。 以下の条件を満たす文字列 T_1,T_2,\dots,T_N を求めてください。
- 条件:T_i はある非負整数 k について k 個の
.、S_i、k 個の.をこの順に結合してできる、長さ m の文字列である。
制約
- N は 1 以上 100 以下の整数
- S_i は英小文字からなる長さ 1 以上 99 以下の奇数長の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
N 行出力せよ。i\ (1 \leq i \leq N) 行目には T_i を出力せよ。
入力例 1
4 apple blueberry coconut dragonfruit
出力例 1
...apple... .blueberry. ..coconut.. dragonfruit
m=11 であり、T_1,T_2,T_3,T_4 はそれぞれ k=3,1,2,0 について問題文中の条件を満たしています。
入力例 2
6 abc d efghi jkl mnopq r
出力例 2
.abc. ..d.. efghi .jkl. mnopq ..r..
Score : 200 points
Problem Statement
You are given N strings S_1,S_2,\dots,S_N of odd lengths consisting of lowercase English letters.
Let m be the length of the longest string among S_1,S_2,\dots,S_N. Find strings T_1,T_2,\dots,T_N satisfying the following condition.
- Condition: T_i is a string of length m formed by concatenating k copies of
., S_i, and k copies of.in this order, for some non-negative integer k.
Constraints
- N is an integer between 1 and 100, inclusive.
- S_i is a string of odd length between 1 and 99, inclusive, consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Output N lines. The i-th line (1 \leq i \leq N) should contain T_i.
Sample Input 1
4 apple blueberry coconut dragonfruit
Sample Output 1
...apple... .blueberry. ..coconut.. dragonfruit
m=11, and T_1,T_2,T_3,T_4 satisfy the condition in the problem statement for k=3,1,2,0, respectively.
Sample Input 2
6 abc d efghi jkl mnopq r
Sample Output 2
.abc. ..d.. efghi .jkl. mnopq ..r..
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 個の袋があります。
袋 i には L_i 個のボールが入っていて、袋 i の j(1\leq j\leq L_i) 番目のボールには正の整数 a_{i,j} が書かれています。
それぞれの袋から 1 つずつボールを取り出します。
取り出したボールに書かれた数の総積が X になるような取り出し方は何通りありますか?
ただし、書かれた数が同じであっても全てのボールは区別します。
制約
- N \geq 2
- L_i \geq 2
- 袋に入っているボールの個数の総積は 10^5 を超えない。すなわち、\displaystyle\prod_{i=1}^{N}L_i \leq 10^5
- 1 \leq a_{i,j} \leq 10^9
- 1 \leq X \leq 10^{18}
- 入力に含まれる値は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}
出力
答えを出力せよ。
入力例 1
2 40 3 1 8 4 2 10 5
出力例 1
2
袋 1 の 3 番目のボールと袋 2 の 1 番目のボールを選ぶと、a_{1,3} \times a_{2,1} = 4 \times 10 = 40 となります。
袋 1 の 2 番目のボールと袋 2 の 2 番目のボールを選ぶと、a_{1,2} \times a_{2,2} = 8 \times 5 = 40 となります。
これ以外に総積が 40 になる取り出し方は存在しないので、答えは 2 です。
入力例 2
3 200 3 10 10 10 3 10 10 10 5 2 2 2 2 2
出力例 2
45
書かれた数が同じであっても全てのボールは区別することに注意してください。
入力例 3
3 1000000000000000000 2 1000000000 1000000000 2 1000000000 1000000000 2 1000000000 1000000000
出力例 3
0
総積が X になる取り出し方が 1 つも存在しないこともあります。
Score : 300 points
Problem Statement
We have N bags.
Bag i contains L_i balls. The j-th ball (1\leq j\leq L_i) in Bag i has a positive integer a_{i,j} written on it.
We will pick out one ball from each bag.
How many ways are there to pick the balls so that the product of the numbers written on the picked balls is X?
Here, we distinguish all balls, even with the same numbers written on them.
Constraints
- N \geq 2
- L_i \geq 2
- The product of the numbers of balls in the bags is at most 10^5: \displaystyle\prod_{i=1}^{N}L_i \leq 10^5.
- 1 \leq a_{i,j} \leq 10^9
- 1 \leq X \leq 10^{18}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}
Output
Print the answer.
Sample Input 1
2 40 3 1 8 4 2 10 5
Sample Output 1
2
When choosing the 3-rd ball in Bag 1 and 1-st ball in Bag 2, we have a_{1,3} \times a_{2,1} = 4 \times 10 = 40.
When choosing the 2-nd ball in Bag 1 and 2-nd ball in Bag 2, we have a_{1,2} \times a_{2,2} = 8 \times 5 = 40.
There are no other ways to make the product 40, so the answer is 2.
Sample Input 2
3 200 3 10 10 10 3 10 10 10 5 2 2 2 2 2
Sample Output 2
45
Note that we distinguish all balls, even with the same numbers written on them.
Sample Input 3
3 1000000000000000000 2 1000000000 1000000000 2 1000000000 1000000000 2 1000000000 1000000000
Sample Output 3
0
There may be no way to make the product X.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
縦 H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と表します。
(i,j) には文字 G_{i,j} が書きこまれています。ここで G_{i,j} は U, D, L, R のいずれかです。
あなたは (1,1) にいます。あなたは移動することができなくなるまで次の操作を繰り返します。
あなたは現在 (i,j) にいるとする。
G_{i,j} がUであり、かつ i \neq 1 ならば (i-1,j) へ移動する。
G_{i,j} がDであり、かつ i \neq H ならば (i+1,j) へ移動する。
G_{i,j} がLであり、かつ j \neq 1 ならば (i,j-1) へ移動する。
G_{i,j} がRであり、かつ j \neq W ならば (i,j+1) へ移動する。
それ以外の場合、あなたは移動することができない。
操作を終了した時点であなたがいるマスを出力してください。
ただし、あなたが操作を終了することなく無限に移動し続ける場合は -1 を出力してください。
制約
- 1 \leq H, W \leq 500
- G_{i,j} は
U,D,L,Rのいずれかである。 - H, W は整数である。
入力
入力は以下の形式で標準入力から与えられる。
H W
G_{1,1}G_{1,2}\dots G_{1,W}
G_{2,1}G_{2,2}\dots G_{2,W}
\vdots
G_{H,1}G_{H,2}\dots G_{H,W}
出力
操作を終了した時点であなたが (i,j) にいる場合は次の形式で出力せよ。
i j
また、無限に動き続ける場合は -1 を出力せよ。
入力例 1
2 3 RDU LRU
出力例 1
1 3
あなたは (1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3) の順に動いたあとに操作を終了します。よって答えは (1, 3) です。
入力例 2
2 3 RRD ULL
出力例 2
-1
あなたは (1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots というように無限に動き続けます。この場合は -1 を出力します。
入力例 3
9 44 RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR
出力例 3
9 5
Score : 300 points
Problem Statement
We have a grid with H horizontal rows and W vertical columns. (i, j) denotes the square at the i-th row from the top and j-th column from the left.
(i,j) has a character G_{i,j} written on it. G_{i,j} is U, D, L, or R.
You are initially at (1,1). You repeat the following operation until you cannot make a move.
Let (i,j) be the square you are currently at.
If G_{i,j} isUand i \neq 1, move to (i-1,j).
If G_{i,j} isDand i \neq H, move to (i+1,j).
If G_{i,j} isLand j \neq 1, move to (i,j-1).
If G_{i,j} isRand j \neq W, move to (i,j+1).
Otherwise, you cannot make a move.
Print the square you end up at when you cannot make a move.
If you indefinitely repeat moving, print -1 instead.
Constraints
- 1 \leq H, W \leq 500
- G_{i,j} is
U,D,L, orR. - H and W are integers.
Input
Input is given from Standard Input in the following format:
H W
G_{1,1}G_{1,2}\dots G_{1,W}
G_{2,1}G_{2,2}\dots G_{2,W}
\vdots
G_{H,1}G_{H,2}\dots G_{H,W}
Output
If you end up at (i, j), print it in the following format:
i j
If you indefinitely repeat moving, print -1.
Sample Input 1
2 3 RDU LRU
Sample Output 1
1 3
You will move as (1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3), ending up here, so the answer is (1, 3).
Sample Input 2
2 3 RRD ULL
Sample Output 2
-1
You will indefinitely repeat moving as (1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots, so -1 should be printed in this case.
Sample Input 3
9 44 RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR
Sample Output 3
9 5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
2N 個のボールがあります。各ボールには 1 以上 N 以下の整数によって表される色が塗られており、各色で塗られたボールはちょうど 2 個ずつ存在します。
これらのボールが、底が地面と平行になるように置かれた M 本の筒に入れられています。はじめ、i \ (1 \leq i \leq M) 本目の筒には k_i 個のボールが入っており、上から j \ (1 \leq j \leq k_i) 番目のボールの色は a_{i, j} です。
あなたの目標は、以下の操作を繰り返すことで M 本の筒全てを空にすることです。
- 異なる 2 本の空でない筒を選び、それぞれの筒の一番上にあるボールを取り出して捨てる。ここで、取り出して捨てた 2 つのボールは同じ色で塗られている必要がある。
目標が達成可能かを判定してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 2 \leq M \leq 2 \times 10^5
- 1 \leq k_i\ (1 \leq i \leq M)
- 1 \leq a_{i,j} \leq N\ (1 \leq i \leq M,1 \leq j \leq k_i)
- \sum_{i=1}^{M} k_i = 2N
- 全ての x\ (1 \leq x \leq N) について、1 \leq i \leq M かつ 1 \leq j \leq k_i かつ a_{i,j}=x なる整数の組 (i,j) はちょうど 2 つ存在する
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M
k_1
a_{1,1} a_{1,2} \ldots a_{1,k_1}
k_2
a_{2,1} a_{2,2} \ldots a_{2,k_2}
\hspace{2.1cm}\vdots
k_M
a_{M,1} a_{M,2} \ldots a_{M,k_M}
出力
目標が達成可能なら Yes を、達成不可能なら No を出力せよ。
入力例 1
2 2 2 1 2 2 1 2
出力例 1
Yes
以下のように操作を行えばよいです。
- 1 つ目の筒と 2 つ目の筒を選び、それぞれの筒の一番上にあるボールを取り出して捨てる。捨てられるボールの色は共に 1 であり等しいので、この操作は有効である。
- 1 つ目の筒と 2 つ目の筒を選び、それぞれの筒の一番上にあるボールを取り出して捨てる。捨てられるボールの色は共に 2 であり等しいので、この操作は有効である。
入力例 2
2 2 2 1 2 2 2 1
出力例 2
No
そもそも一度も操作を行うことができないため、目標を達成する、すなわち M 本の筒全てを空にすることは不可能です。
Score : 400 points
Problem Statement
We have 2N balls. Each ball has a color represented by an integer between 1 and N (inclusive). For each of the N colors, there are exactly two balls of that color.
These balls are contained in M cylinders placed perpendicularly to the floor. Initially, the i-th cylinder (1 \leq i \leq M) contains k_i balls, the j-th of which from the top (1 \leq j \leq k_i) has the color a_{i, j}.
Your objective is to empty all M cylinders by repeating the following operation.
- Choose two different non-empty cylinders and remove the topmost ball from each of them. Here, the two balls removed must be of the same color.
Determine whether the objective is achievable.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 2 \leq M \leq 2 \times 10^5
- 1 \leq k_i\ (1 \leq i \leq M)
- 1 \leq a_{i,j} \leq N\ (1 \leq i \leq M,1 \leq j \leq k_i)
- \sum_{i=1}^{M} k_i = 2N
- For every x\ (1 \leq x \leq N), there exists exactly two pairs of integers (i,j) such that 1 \leq i \leq M, 1 \leq j \leq k_i, and a_{i,j}=x.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
k_1
a_{1,1} a_{1,2} \ldots a_{1,k_1}
k_2
a_{2,1} a_{2,2} \ldots a_{2,k_2}
\hspace{2.1cm}\vdots
k_M
a_{M,1} a_{M,2} \ldots a_{M,k_M}
Output
If the objective is achievable, print Yes; otherwise, print No.
Sample Input 1
2 2 2 1 2 2 1 2
Sample Output 1
Yes
The objective can be achieved as follows.
- Choose the first and second cylinders to remove the topmost ball from each of them, which is allowed since the removed balls have the same color: 1.
- Choose the first and second cylinders to remove the topmost ball from each of them, which is allowed since the removed balls have the same color: 2.
Sample Input 2
2 2 2 1 2 2 2 1
Sample Output 2
No
No operation can be done at all, which means it is impossible to achieve the objective of emptying the M cylinders.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 個の頂点と M 本の辺からなる、単純(自己ループおよび多重辺を含まない)かつ連結な無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ無向辺です。
各頂点を白または黒で塗る方法であって、下記の 2 つの条件をともに満たすものが存在するかを判定し、存在する場合はその一例を示してください。
- 1 個以上の頂点が黒で塗られている。
- すべての i = 1, 2, \ldots, K について、下記の条件が成り立つ。
- 頂点 p_i と「黒で塗られた頂点のうち頂点 p_i からの距離が最小であるもの」の距離がちょうど d_i である。
ここで、頂点 u と頂点 v の距離は、u と v を結ぶパスの辺の本数としてあり得る最小値として定義されます。
制約
- 1 \leq N \leq 2000
- N-1 \leq M \leq \min\lbrace N(N-1)/2, 2000 \rbrace
- 1 \leq u_i, v_i \leq N
- 0 \leq K \leq N
- 1 \leq p_1 \lt p_2 \lt \cdots \lt p_K \leq N
- 0 \leq d_i \leq N
- 与えられるグラフは単純かつ連結
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M K p_1 d_1 p_2 d_2 \vdots p_K d_K
出力
問題文中の条件を満たすように各頂点を白または黒で塗る方法が存在しない場合は、No を出力せよ。
存在する場合は、下記の形式の通り、1 行目に Yes と出力し、2 行目に各頂点を塗る方法を表す文字列 S を出力せよ。
ここで、S は長さ N の文字列であり、i = 1, 2, \ldots, N について S の i 文字目は、頂点 i を黒で塗るとき 1 、白で塗るとき 0 である。
Yes S
答えが複数ある場合、どれを出力しても正解とみなされる。
入力例 1
5 5 1 2 2 3 3 1 3 4 4 5 2 1 0 5 2
出力例 1
Yes 10100
例えば、頂点 1, 3 を黒に、頂点 2, 4, 5 を白に塗るという方法が、問題文中の条件を満たします。
実際、i = 1, 2, 3, 4, 5 について、頂点 i と「黒で塗られた頂点のうち頂点 i からの距離が最小であるもの」の距離を A_i で表すと、
(A_1, A_2, A_3, A_4, A_5) = (0, 1, 0, 1, 2) であり、特に、A_1 = 0, A_5 = 2 が成り立ちます。
入力例 2
5 5 1 2 2 3 3 1 3 4 4 5 5 1 1 2 1 3 1 4 1 5 1
出力例 2
No
問題文中の条件を満たすように各頂点を白または黒で塗る方法が存在しないため、No を出力します。
入力例 3
1 0 0
出力例 3
Yes 1
Score : 500 points
Problem Statement
You are given a simple connected undirected graph with N vertices and M edges (a simple graph contains no self-loop and no multi-edges).
For i = 1, 2, \ldots, M, the i-th edge connects vertex u_i and vertex v_i bidirectionally.
Determine whether there is a way to paint each vertex black or white to satisfy both of the following conditions, and show one such way if it exists.
- At least one vertex is painted black.
- For every i = 1, 2, \ldots, K, the following holds:
- the minimum distance between vertex p_i and a vertex painted black is exactly d_i.
Here, the distance between vertex u and vertex v is the minimum number of edges in a path connecting u and v.
Constraints
- 1 \leq N \leq 2000
- N-1 \leq M \leq \min\lbrace N(N-1)/2, 2000 \rbrace
- 1 \leq u_i, v_i \leq N
- 0 \leq K \leq N
- 1 \leq p_1 \lt p_2 \lt \cdots \lt p_K \leq N
- 0 \leq d_i \leq N
- The given graph is simple and connected.
- All values in the input are integers.
Input
The 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 K p_1 d_1 p_2 d_2 \vdots p_K d_K
Output
If there is no way to paint each vertex black or white to satisfy the conditions, print No.
Otherwise, print Yes in the first line, and a string S representing a coloring of the vertices in the second line, as shown below.
Here, S is a string of length N such that, for each i = 1, 2, \ldots, N, the i-th character of S is 1 if vertex i is painted black and 0 if white.
Yes S
If multiple solutions exist, you may print any of them.
Sample Input 1
5 5 1 2 2 3 3 1 3 4 4 5 2 1 0 5 2
Sample Output 1
Yes 10100
One way to satisfy the conditions is to paint vertices 1, 3 black and vertices 2, 4, 5 white.
Indeed, for each i = 1, 2, 3, 4, 5, let A_i denote the minimum distance between vertex i and a vertex painted black, and we have (A_1, A_2, A_3, A_4, A_5) = (0, 1, 0, 1, 2), where A_1 = 0, A_5 = 2.
Sample Input 2
5 5 1 2 2 3 3 1 3 4 4 5 5 1 1 2 1 3 1 4 1 5 1
Sample Output 2
No
There is no way to satisfy the conditions by painting each vertex black or white, so you should print No.
Sample Input 3
1 0 0
Sample Output 3
Yes 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
英大小文字と ( 、 ) からなる文字列 S=S_1 S_2 S_3 \dots S_{|S|} が与えられます。
文字列 S 中の括弧は、対応が取れています。
次の操作を、操作ができなくなるまで繰り返します。
- まず、以下の条件を全て満たす整数組 (l,r) をひとつ選択する。
- l < r
- S_l =
( - S_r =
) - S_{l+1},S_{l+2},\dots,S_{r-1} は全て英大文字または英小文字である
- T=\overline{S_{r-1}S_{r-2} \dots S_{l+1}} とする。
- 但し、 \overline{x} は x の大文字と小文字を反転させた文字列を指す。
- その後、 S の l 文字目から r 文字目までを削除し、削除した位置に T を挿入する。
詳細は入出力例を参照してください。
上記の操作を使って全ての ( と ) を除去することができ、最終的な文字列は操作の方法や順序によらないことが証明できます。
このとき、最終的な文字列を求めてください。
「 S 中の括弧の対応が取れている」とは?
まず、正しい括弧列を次の通り定義します。- 正しい括弧列とは、以下のいずれかの条件を満たす文字列です。
- 空文字列
- ある正しい括弧列 A が存在して、
(, A,)をこの順に連結した文字列 - ある空でない正しい括弧列 A,B が存在して、 A,B をこの順に連結した文字列
( と ) を順序を保って抜き出した時、それが正しい括弧列となることを指す。
制約
- 1 \le |S| \le 5 \times 10^5
- S は英大小文字と
(、)からなる - S 中の括弧は対応が取れている
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
((A)y)x
出力例 1
YAx
S= ((A)y)x に対して操作を行います。
- l=2,r=4 を選択します。このとき削除される文字列は
(A)で、代わりにaが挿入されます。- この操作の結果、 S=
(ay)xとなります。
- この操作の結果、 S=
- l=1,r=4 を選択します。このとき削除される文字列は
(ay)で、代わりにYAが挿入されます。- この操作の結果、 S=
YAxとなります。
- この操作の結果、 S=
括弧を除去した結果、文字列は YAx となったので、これを出力してください。
入力例 2
((XYZ)n(X(y)Z))
出力例 2
XYZNXYZ
S= ((XYZ)n(X(y)Z)) に対して操作を行います。
- l=10,r=12 を選択します。このとき削除される文字列は
(y)で、代わりにYが挿入されます。- この操作の結果、 S=
((XYZ)n(XYZ))となります。
- この操作の結果、 S=
- l=2,r=6 を選択します。このとき削除される文字列は
(XYZ)で、代わりにzyxが挿入されます。- この操作の結果、 S=
(zyxn(XYZ))となります。
- この操作の結果、 S=
- l=6,r=10 を選択します。このとき削除される文字列は
(XYZ)で、代わりにzyxが挿入されます。- この操作の結果、 S=
(zyxnzyx)となります。
- この操作の結果、 S=
- l=1,r=9 を選択します。このとき削除される文字列は
(zyxnzyx)で、代わりにXYZNXYZが挿入されます。- この操作の結果、 S=
XYZNXYZとなります。
- この操作の結果、 S=
括弧を除去した結果、文字列は XYZNXYZ となったので、これを出力してください。
入力例 3
(((()))(()))(())
出力例 3
操作結果が空文字列になる場合もあります。
入力例 4
dF(qT(plC())NnnfR(GsdccC))PO()KjsiI((ysA)eWW)ve
出力例 4
dFGsdccCrFNNnplCtQPOKjsiIwwEysAve
Score: 550 points
Problem Statement
You are given a string S = S_1 S_2 S_3 \dots S_{|S|} consisting of uppercase and lowercase English letters, (, and ).
The parentheses in the string S are properly matched.
Repeat the following operation until no more operations can be performed:
- First, select one pair of integers (l, r) that satisfies all of the following conditions:
- l < r
- S_l =
( - S_r =
) - Each of the characters S_{l+1}, S_{l+2}, \dots, S_{r-1} is an uppercase or lowercase English letter.
- Let T = \overline{S_{r-1} S_{r-2} \dots S_{l+1}}.
- Here, \overline{x} denotes the string obtained by toggling the case of each character in x (uppercase to lowercase and vice versa).
- Then, delete the l-th through r-th characters of S and insert T at the position where the deletion occurred.
Refer to the sample inputs and outputs for clarification.
It can be proved that it is possible to remove all (s and )s from the string using the above operations, and the final string is independent of how and in what order the operations are performed.
Determine the final string.
What does it mean that the parentheses in S are properly matched?
First, a correct parenthesis sequence is defined as follows:- A correct parenthesis sequence is a string that satisfies one of the following conditions:
- It is an empty string.
- There exists a correct parenthesis sequence A, and the string is formed by concatenating
(, A,)in this order. - There exist non-empty correct parenthesis sequences A and B, and the string is formed by concatenating A and B in this order.
(s and )s extracted from S without changing the order form a correct parenthesis sequence.
Constraints
- 1 \le |S| \le 5 \times 10^5
- S consists of uppercase and lowercase English letters,
(, and). - The parentheses in S are properly matched.
Input
The input is given from Standard Input in the following format:
S
Output
Print the final string.
Sample Input 1
((A)y)x
Sample Output 1
YAx
Let us perform the operations for S = ((A)y)x.
- Choose l=2 and r=4. The substring
(A)is removed and replaced witha.- After this operation, S =
(ay)x.
- After this operation, S =
- Choose l=1 and r=4. The substring
(ay)is removed and replaced withYA.- After this operation, S =
YAx.
- After this operation, S =
After removing the parentheses, the string becomes YAx, which should be printed.
Sample Input 2
((XYZ)n(X(y)Z))
Sample Output 2
XYZNXYZ
Let us perform the operations for S = ((XYZ)n(X(y)Z)).
- Choose l=10 and r=12. The substring
(y)is removed and replaced withY.- After this operation, S =
((XYZ)n(XYZ)).
- After this operation, S =
- Choose l=2 and r=6. The substring
(XYZ)is removed and replaced withzyx.- After this operation, S =
(zyxn(XYZ)).
- After this operation, S =
- Choose l=6 and r=10. The substring
(XYZ)is removed and replaced withzyx.- After this operation, S =
(zyxnzyx).
- After this operation, S =
- Choose l=1 and r=9. The substring
(zyxnzyx)is removed and replaced withXYZNXYZ.- After this operation, S =
XYZNXYZ.
- After this operation, S =
After removing the parentheses, the string becomes XYZNXYZ, which should be printed.
Sample Input 3
(((()))(()))(())
Sample Output 3
The final outcome may be an empty string.
Sample Input 4
dF(qT(plC())NnnfR(GsdccC))PO()KjsiI((ysA)eWW)ve
Sample Output 4
dFGsdccCrFNNnplCtQPOKjsiIwwEysAve