Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
長さ N の数列 A=(A_1,A_2,\ldots,A_N) および正整数 P,Q,R,S が与えられます。
ここで、P,Q,R,S は、1\leq P\leq Q<R\leq S \leq N および Q-P=S-R をみたしています。
数列 A の P 番目から Q 番目の項までと R 番目から S 番目の項までを入れ替えた数列を B=(B_1, B_2,\ldots, B_N) とします。
数列 B を出力してください。
制約
- 1\leq N \leq 100
- 1\leq A_i\leq 100
- 1\leq P\leq Q<R\leq S \leq N
- Q-P=S-R
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N P Q R S A_1 A_2 \ldots A_N
出力
B_1, B_2,\ldots, B_N を空白区切りで出力せよ。
入力例 1
8 1 3 5 7 1 2 3 4 5 6 7 8
出力例 1
5 6 7 4 1 2 3 8
数列 A=(1,2,3,4,5,6,7,8) の 1 番目から 3 番目の項 (1,2,3) と 5 番目から 7 番目までの項 (5,6,7) を 入れ替えると, B=(5,6,7,4,1,2,3,8) となります。 よってこれを空白区切りで出力します。
入力例 2
5 2 3 4 5 2 2 1 1 1
出力例 2
2 1 1 2 1
数列には同じ整数が複数回現れる事もあります。
入力例 3
2 1 1 2 2 50 100
出力例 3
100 50
入力例 4
10 2 4 7 9 22 75 26 45 72 81 47 29 97 2
出力例 4
22 47 29 97 72 81 75 26 45 2
Score : 100 points
Problem Statement
You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N and positive integers P,Q,R, and S.
Here, P,Q,R, and S satisfy 1\leq P\leq Q<R\leq S \leq N and Q-P=S-R.
Let B=(B_1, B_2,\ldots, B_N) be the sequence obtained by swapping the P-th through Q-th terms and the R-th through S-th terms of A.
Print the sequence B.
Constraints
- 1\leq N \leq 100
- 1\leq A_i\leq 100
- 1\leq P\leq Q<R\leq S \leq N
- Q-P=S-R
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N P Q R S A_1 A_2 \ldots A_N
Output
Print B_1, B_2,\ldots, B_N, with spaces in between.
Sample Input 1
8 1 3 5 7 1 2 3 4 5 6 7 8
Sample Output 1
5 6 7 4 1 2 3 8
Swapping the 1-st through 3-rd terms (1,2,3) and the 5-th through 7-th terms (5,6,7) of the sequence A=(1,2,3,4,5,6,7,8) results in B=(5,6,7,4,1,2,3,8), which should be printed with spaces in between.
Sample Input 2
5 2 3 4 5 2 2 1 1 1
Sample Output 2
2 1 1 2 1
The same integer may occur multiple times in the sequence.
Sample Input 3
2 1 1 2 2 50 100
Sample Output 3
100 50
Sample Input 4
10 2 4 7 9 22 75 26 45 72 81 47 29 97 2
Sample Output 4
22 47 29 97 72 81 75 26 45 2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
末尾が er
または ist
であるような文字列 S が与えられます。
S の末尾が er
である場合は er
を、 ist
である場合は ist
を出力してください。
制約
- 2 \le |S| \le 20
- S は英小文字のみからなる。
- S の末尾は
er
またはist
である。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
atcoder
出力例 1
er
S="atcoder
" の末尾は er
です。
入力例 2
tourist
出力例 2
ist
入力例 3
er
出力例 3
er
Score : 100 points
Problem Statement
You are given a string S ending with er
or ist
.
If S ends with er
, print er
; if it ends with ist
, print ist
.
Constraints
- 2 \le |S| \le 20
- S consists of lowercase English letters.
- S ends with
er
orist
.
Input
Input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
atcoder
Sample Output 1
er
S="atcoder
" ends with er
.
Sample Input 2
tourist
Sample Output 2
ist
Sample Input 3
er
Sample Output 3
er
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
縦 H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と表します。
各マスの状態は文字 C_{i,j} で表されます。C_{i,j} が .
ならば (i, j) には何も置かれておらず、 #
ならば箱が 1 個置かれています。
1 \leq j \leq W を満たす整数 j に対して、整数 X_j を次のように定義します。
- j 列目に置かれている箱の個数。言い換えると、C_{i,j} が
#
であるような整数 i の個数。
X_1, X_2, \dots, X_W をすべて求めてください。
制約
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- H, W は整数
- C_{i, j} は
.
または#
入力
入力は以下の形式で標準入力から与えられる。
H W C_{1,1}C_{1,2}\dots C_{1,W} C_{2,1}C_{2,2}\dots C_{2,W} \vdots C_{H,1}C_{H,2}\dots C_{H,W}
出力
X_1, X_2, \dots, X_W を以下の形式に従って出力せよ。
X_1 X_2 \dots X_W
入力例 1
3 4 #..# .#.# .#.#
出力例 1
1 2 0 3
1 列目の箱が置かれているマスは (1, 1) の 1 ヵ所です。よって X_1 = 1 です。
2 列目の箱が置かれているマスは (2, 2), (3, 2) の 2 ヵ所です。よって X_2 = 2 です。
3 列目の箱が置かれているマスは存在しません。よって X_3 = 0 です。
4 列目の箱が置かれているマスは (1, 4), (2, 4), (3, 4) の 3 ヵ所です。よって X_4 = 3 です。
よって (X_1, X_2, X_3, X_4) = (1, 2, 0, 3) が答えとなります。
入力例 2
3 7 ....... ....... .......
出力例 2
0 0 0 0 0 0 0
箱が置かれているマスが存在しない場合もあります。
入力例 3
8 3 .#. ### .#. .#. .## ..# ##. .##
出力例 3
2 7 4
入力例 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
出力例 4
0 5 1 2 2 0 0 5 3 3 3 3 0 0 1 1 3 1 1 0 0 5 3 3 3 3 0 0 5 1 1 1 5 0 0 3 2 2 2 2 0 0 5 3 3 3 3
Score : 200 points
Problem Statement
There is a grid with H rows from top to bottom and W columns from left to right. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
The squares are described by characters C_{i,j}. If C_{i,j} is .
, (i, j) is empty; if it is #
, (i, j) contains a box.
For integers j satisfying 1 \leq j \leq W, let the integer X_j defined as follows.
- X_j is the number of boxes in the j-th column. In other words, X_j is the number of integers i such that C_{i,j} is
#
.
Find all of X_1, X_2, \dots, X_W.
Constraints
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- H and W are integers.
- C_{i, j} is
.
or#
.
Input
The input is given from Standard Input in the following format:
H W C_{1,1}C_{1,2}\dots C_{1,W} C_{2,1}C_{2,2}\dots C_{2,W} \vdots C_{H,1}C_{H,2}\dots C_{H,W}
Output
Print X_1, X_2, \dots, X_W in the following format:
X_1 X_2 \dots X_W
Sample Input 1
3 4 #..# .#.# .#.#
Sample Output 1
1 2 0 3
In the 1-st column, one square, (1, 1), contains a box. Thus, X_1 = 1.
In the 2-nd column, two squares, (2, 2) and (3, 2), contain a box. Thus, X_2 = 2.
In the 3-rd column, no squares contain a box. Thus, X_3 = 0.
In the 4-th column, three squares, (1, 4), (2, 4), and (3, 4), contain a box. Thus, X_4 = 3.
Therefore, the answer is (X_1, X_2, X_3, X_4) = (1, 2, 0, 3).
Sample Input 2
3 7 ....... ....... .......
Sample Output 2
0 0 0 0 0 0 0
There may be no square that contains a box.
Sample Input 3
8 3 .#. ### .#. .#. .## ..# ##. .##
Sample Output 3
2 7 4
Sample Input 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
Sample Output 4
0 5 1 2 2 0 0 5 3 3 3 3 0 0 1 1 3 1 1 0 0 5 3 3 3 3 0 0 5 1 1 1 5 0 0 3 2 2 2 2 0 0 5 3 3 3 3
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
配点 : 300 点
問題文
N 人の巨人がいます。巨人にはそれぞれ 1, 2, \ldots, N の名前がついており、巨人 i が地面に立ったとき、肩の高さは A_i、頭の高さは B_i となります。
あなたは (1, 2, \ldots, N) を並べ替えて得られる数列 (P_1, P_2, \ldots, P_N) を選び、以下の規則に従って N 人の巨人を積み上げることができます。
-
まず地面に巨人 P_1 を立たせる。巨人 P_1 の肩は地面を基準として A_{P_1}、頭は地面を基準として B_{P_1} の高さとなる。
-
i = 1, 2, \ldots, N - 1 の順に巨人 P_i の肩の上に巨人 P_{i + 1} を立たせる。巨人 P_i の肩が地面を基準として高さ t のとき、巨人 P_{i + 1} の肩は地面を基準として t + A_{P_{i + 1}}、頭は地面を基準として t + B_{P_{i + 1}} の高さとなる。
一番上に立っている巨人、すなわち巨人 P_N の地面を基準とした頭の高さとして実現できる最大値を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq B_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
答えを出力せよ。
入力例 1
3 4 10 5 8 2 9
出力例 1
18
(P_1, P_2, P_3) = (2, 1, 3) とすると、地面を基準として巨人 2 は肩の高さが 5、頭の高さが 8、巨人 1 は肩の高さが 9、頭の高さが 15、巨人 3 は肩の高さが 11、頭の高さが 18 となります。
一番上に立っている巨人の頭の高さが地面を基準として 18 より大きくなることはないため 18 を出力します。
入力例 2
5 1 1 1 1 1 1 1 1 1 1
出力例 2
5
入力例 3
10 690830957 868532399 741145463 930111470 612846445 948344128 540375785 925723427 723092548 925021315 928915367 973970164 563314352 832796216 562681294 868338948 923012648 954764623 691107436 891127278
出力例 3
7362669937
Score: 300 points
Problem Statement
There are N giants, named 1 to N. When giant i stands on the ground, their shoulder height is A_i, and their head height is B_i.
You can choose a permutation (P_1, P_2, \ldots, P_N) of (1, 2, \ldots, N) and stack the N giants according to the following rules:
-
First, place giant P_1 on the ground. The giant P_1's shoulder will be at a height of A_{P_1} from the ground, and their head will be at a height of B_{P_1} from the ground.
-
For i = 1, 2, \ldots, N - 1 in order, place giant P_{i + 1} on the shoulders of giant P_i. If giant P_i's shoulders are at a height of t from the ground, then giant P_{i + 1}'s shoulders will be at a height of t + A_{P_{i + 1}} from the ground, and their head will be at a height of t + B_{P_{i + 1}} from the ground.
Find the maximum possible height of the head of the topmost giant P_N from the ground.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq B_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Print the answer.
Sample Input 1
3 4 10 5 8 2 9
Sample Output 1
18
If (P_1, P_2, P_3) = (2, 1, 3), then measuring from the ground, giant 2 has a shoulder height of 5 and a head height of 8, giant 1 has a shoulder height of 9 and a head height of 15, and giant 3 has a shoulder height of 11 and a head height of 18.
The head height of the topmost giant from the ground cannot be greater than 18, so print 18.
Sample Input 2
5 1 1 1 1 1 1 1 1 1 1
Sample Output 2
5
Sample Input 3
10 690830957 868532399 741145463 930111470 612846445 948344128 540375785 925723427 723092548 925021315 928915367 973970164 563314352 832796216 562681294 868338948 923012648 954764623 691107436 891127278
Sample Output 3
7362669937
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
キーエンス本社に勤務する人数が増えてきたので、本社に存在する部署を 2 つのグループに分け、昼休みの時間帯を分けることにしました。
キーエンス本社には N 個の部署が存在し、i 番目 (1\leq i\leq N) の部署に所属する人数は K_i 人です。
それぞれの部署をグループ A, B のいずれか一方に割り当て、グループごとに同時に昼休みをとり、
かつグループ A, B の昼休みの時間が重ならないようにしたとき、同時に昼休みをとる最大人数としてあり得る最小の値を求めてください。
すなわち、グループ A に割り当てられた部署に所属する人数の合計とグループ B に割り当てられた部署に所属する人数の合計
のうち大きい方の値としてあり得る最小の値を求めてください。
制約
- 2\leq N \leq 20
- 1\leq K_i \leq 10^8
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K_1 K_2 \ldots K_N
出力
同時に昼休みを取る最大人数としてあり得る最小の値を出力せよ。
入力例 1
5 2 3 5 10 12
出力例 1
17
1,2,5 番目の部署をグループ A に、3,4 番目の部署をグループ B に割り当てたとき、 グループ A に割り当てられた部署に所属する人数の合計は 2+3+12=17 、 グループ B に割り当てられた部署に所属する人数の合計は 5+10=15 となり、 このとき同時に昼休みを取る最大人数は 17 となります。
一方で、グループ A,B それぞれに割り当てられた部署に所属する人数の合計がいずれも 16 以下になるように 部署を割り当てることはできないため、17 を出力します。
入力例 2
2 1 1
出力例 2
1
同一人数の部署が複数存在する可能性もあります。
入力例 3
6 22 25 26 45 22 31
出力例 3
89
例えば、1,4,5 番目の部署をグループ A に、2,3,6 番目の部署をグループ B に割り当てたとき同時に昼休みを取る最大人数は 89 となります。
Score : 300 points
Problem Statement
As KEYENCE headquarters have more and more workers, they decided to divide the departments in the headquarters into two groups and stagger their lunch breaks.
KEYENCE headquarters have N departments, and the number of people in the i-th department (1\leq i\leq N) is K_i.
When assigning each department to Group A or Group B, having each group take lunch breaks at the same time, and ensuring that the lunch break times of Group A and Group B do not overlap, find the minimum possible value of the maximum number of people taking a lunch break at the same time.
In other words, find the minimum possible value of the larger of the following: the total number of people in departments assigned to Group A, and the total number of people in departments assigned to Group B.
Constraints
- 2 \leq N \leq 20
- 1 \leq K_i \leq 10^8
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K_1 K_2 \ldots K_N
Output
Print the minimum possible value of the maximum number of people taking a lunch break at the same time.
Sample Input 1
5 2 3 5 10 12
Sample Output 1
17
When assigning departments 1, 2, and 5 to Group A, and departments 3 and 4 to Group B, Group A has a total of 2+3+12=17 people, and Group B has a total of 5+10=15 people. Thus, the maximum number of people taking a lunch break at the same time is 17.
It is impossible to assign the departments so that both groups have 16 or fewer people, so print 17.
Sample Input 2
2 1 1
Sample Output 2
1
Multiple departments may have the same number of people.
Sample Input 3
6 22 25 26 45 22 31
Sample Output 3
89
For example, when assigning departments 1, 4, and 5 to Group A, and departments 2, 3, and 6 to Group B, the maximum number of people taking a lunch break at the same time is 89.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
20230322
は並べ替えると 02320232
となり、これは 0232
を 2 度繰り返しています。
このように、数字のみからなる文字列であって、適切に文字を並び替える (そのままでもよい) ことによって同じ列を 2 度繰り返すようにできるものを 嬉しい列 と呼びます。
数字のみからなる文字列 S が与えられるので、以下の条件を全て満たす整数の組 (l,r) はいくつあるか求めてください。
- 1 \le l \le r \le |S| ( |S| は S の長さ)
- S の l 文字目から r 文字目までの (連続する) 部分文字列は嬉しい列である。
制約
- S は数字のみからなる長さ 1 以上 5 \times 10^5 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
20230322
出力例 1
4
S= 20230322
です。
条件を満たす整数組 (l,r) は (1,6),(1,8),(2,7),(7,8) の 4 つです。
入力例 2
0112223333444445555556666666777777778888888889999999999
出力例 2
185
S の先頭が 0
である場合もあります。
入力例 3
3141592653589793238462643383279502884197169399375105820974944
出力例 3
9
Score : 400 points
Problem Statement
The string 20230322
can be rearranged into 02320232
, which is a repetition of 0232
twice.
Similarly, a string consisting of digits is said to be happy when it can be rearranged into (or already is) a repetition of some string twice.
You are given a string S consisting of digits. Find the number of pairs of integers (l,r) satisfying all of the following conditions.
- 1 \le l \le r \le |S|. (|S| is the length of S.)
- The (contiguous) substring formed of the l-th through r-th characters of S is happy.
Constraints
- S is a string consisting of digits whose length is between 1 and 5 \times 10^5, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print an integer representing the answer.
Sample Input 1
20230322
Sample Output 1
4
We have S= 20230322
.
Here are the four pairs of integers (l,r) that satisfy the condition: (1,6), (1,8), (2,7), and (7,8).
Sample Input 2
0112223333444445555556666666777777778888888889999999999
Sample Output 2
185
S may begin with 0
.
Sample Input 3
3141592653589793238462643383279502884197169399375105820974944
Sample Output 3
9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 個の頂点と M 本の辺からなる無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ無向辺であり、
a_i = 1 ならばはじめは通行可能、a_i = 0 ならばはじめは通行不能です。
また、頂点 s_1 、頂点 s_2 、\ldots 、頂点 s_K の K 個の頂点にはスイッチがあります。
高橋君は、はじめ頂点 1 におり、「下記の移動とスイッチを押すの 2 つの行動のどちらかを行うこと」を好きなだけ繰り返します。
- 移動 : いまいる頂点と通行可能な辺を介して隣接する頂点を 1 つ選び、その頂点に移動する。
- スイッチを押す : いまいる頂点にスイッチがあるならば、そのスイッチを押す。その結果、グラフ上のすべての辺の通行可能・通行不能の状態が反転する。すなわち、通行可能である辺は通行不能に、通行不能である辺は通行可能に変化する。
高橋君が頂点 N に到達することが可能かどうかを判定し、可能な場合は頂点 N に到達するまでに行う移動の回数としてあり得る最小値を出力してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- a_i \in \lbrace 0, 1\rbrace
- 1 \leq s_1 \lt s_2 \lt \cdots \lt s_K \leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M K u_1 v_1 a_1 u_2 v_2 a_2 \vdots u_M v_M a_M s_1 s_2 \ldots s_K
出力
高橋君が頂点 N に到達することが不可能な場合は -1 を、 可能な場合は頂点 N に到達するまでに行う移動の回数としてあり得る最小値を出力せよ。
入力例 1
5 5 2 1 3 0 2 3 1 5 4 1 2 1 1 1 4 0 3 4
出力例 1
5
高橋君は、下記の手順で行動することで頂点 N に到達できます。
- 頂点 1 から頂点 2 に移動する。
- 頂点 2 から頂点 3 に移動する。
- 頂点 3 にあるスイッチを押す。これによって、グラフ上のすべての辺の通行可能・通行不能の状態が反転する。
- 頂点 3 から頂点 1 に移動する。
- 頂点 1 から頂点 4 に移動する。
- 頂点 4 にあるスイッチを押す。これによって、グラフ上のすべての辺の通行可能・通行不能の状態が再び反転する。
- 頂点 4 から頂点 5 に移動する。
この手順における移動の回数は 5 回であり、これが考えられる最小の回数です。
入力例 2
4 4 2 4 3 0 1 2 1 1 2 0 2 1 1 2 4
出力例 2
-1
与えられるグラフは、連結でないことや、多重辺を含むことがあります。 この入力例では、高橋君はどのように行動しても頂点 N に到達することはできないので、-1 を出力します。
Score : 500 points
Problem Statement
You are given an undirected graph consisting of N vertices and M edges.
For i = 1, 2, \ldots, M, the i-th edge is an undirected edge connecting vertex u_i and v_i that is initially passable if a_i = 1 and initially impassable if a_i = 0.
Additionally, there are switches on K of the vertices: vertex s_1, vertex s_2, \ldots, vertex s_K.
Takahashi is initially on vertex 1, and will repeat performing one of the two actions below, Move or Hit Switch, which he may choose each time, as many times as he wants.
- Move: Choose a vertex adjacent to the vertex he is currently on via an edge, and move to that vertex.
- Hit Switch: If there is a switch on the vertex he is currently on, hit it. This will invert the passability of every edge in the graph. That is, a passable edge will become impassable, and vice versa.
Determine whether Takahashi can reach vertex N, and if he can, print the minimum possible number of times he performs Move before reaching vertex N.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- a_i \in \lbrace 0, 1\rbrace
- 1 \leq s_1 \lt s_2 \lt \cdots \lt s_K \leq N
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M K u_1 v_1 a_1 u_2 v_2 a_2 \vdots u_M v_M a_M s_1 s_2 \ldots s_K
Output
If Takahashi cannot reach vertex N, print -1; if he can, print the minimum possible number of times he performs Move before reaching vertex N.
Sample Input 1
5 5 2 1 3 0 2 3 1 5 4 1 2 1 1 1 4 0 3 4
Sample Output 1
5
Takahashi can reach vertex N as follows.
- Move from vertex 1 to vertex 2.
- Move from vertex 2 to vertex 3.
- Hit the switch on vertex 3. This inverts the passability of every edge in the graph.
- Move from vertex 3 to vertex 1.
- Move from vertex 1 to vertex 4.
- Hit the switch on vertex 4. This again inverts the passability of every edge in the graph.
- Move from vertex 4 to vertex 5.
Here, Move is performed five times, which is the minimum possible number.
Sample Input 2
4 4 2 4 3 0 1 2 1 1 2 0 2 1 1 2 4
Sample Output 2
-1
The given graph may be disconnected or contain multi-edges. In this sample input, there is no way for Takahashi to reach vertex N, so you should print -1.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
Atcoder 国には 1 から N の番号がついた N 個の街と 1 から M の番号がついた M 個の高速道路があります。
高速道路 i は街 A_i と街 B_i を双方向に結んでいます。
国王の高橋君は、新たに N-M-1 本の高速道路を建設し、次の 2 つの条件をともに満たそうとしています。
- すべての街同士は、高速道路をいくつか通って互いに行き来できる
- 各 i=1,\ldots,N について、街 i はちょうど D_i 本の高速道路と直接つながっている
条件を満たすような建設方法が存在するか判定し、存在するなら 1 つ出力してください。
制約
- 2 \leq N \leq 2\times 10^5
- 0 \leq M \lt N-1
- 1 \leq D_i \leq N-1
- 1\leq A_i \lt B_i \leq N
- i\neq j ならば、(A_i, B_i) \neq (A_j,B_j)
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M D_1 \ldots D_N A_1 B_1 \vdots A_M B_M
出力
条件を満たすような建設方法が存在しないとき -1
を出力せよ。
存在するとき、N-M-1 行出力せよ。i 行目には、i 番目に建設する高速道路が結ぶ 2 つの街の番号を空白区切りで出力せよ。
入力例 1
6 2 1 2 1 2 2 2 2 3 1 4
出力例 1
6 2 5 6 4 5
出力例のように、街 6 と2、街 5 と 6、街 4 と 5 をそれぞれ結ぶ高速道路を建設すると条件を満たすことができます。
この他にも、例えば 街 6 と4、街 5 と 6、街 2 と 5 を結ぶような高速道路を建設しても条件を満たすことができます。
入力例 2
5 1 1 1 1 1 4 2 3
出力例 2
-1
入力例 3
4 0 3 3 3 3
出力例 3
-1
Score : 500 points
Problem Statement
The Republic of Atcoder has N towns numbered 1 through N, and M highways numbered 1 through M.
Highway i connects Town A_i and Town B_i bidirectionally.
King Takahashi is going to construct (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,\ldots,N, exactly D_i highways are directly connected to Town i
Determine if there is such a way of construction. If it exists, print one.
Constraints
- 2 \leq N \leq 2\times 10^5
- 0 \leq M \lt N-1
- 1 \leq D_i \leq N-1
- 1\leq A_i \lt B_i \leq N
- If i\neq j, then (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:
N M D_1 \ldots D_N A_1 B_1 \vdots A_M B_M
Output
If there isn't a way of construction that satisfies the conditions, print -1
.
If there is, print (N-M-1) lines. The i-th line should contain the indices of the two towns connected by the i-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 6 and 2, Towns 5 and 6, and Towns 4 and 5, respectively.
Another example to satisfy the conditions is to construct highways connecting Towns 6 and 4, Towns 5 and 6, and Towns 2 and 5, 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