Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
文字列 S が与えられるので、この文字列が Hello,World! と完全に一致するなら AC 、そうでないなら WA と出力してください。
「完全に一致する」とは?
文字列 A と B が完全に一致するとは、文字列 A と B の長さが等しく、かつ全ての 1 \le i \le |A| を満たす整数 i について A の先頭から i 文字目と B の先頭から i 文字目とが(英大文字か小文字かも含めて)一致することを指します。制約
- 1 \le |S| \le 15
- S は英大小文字,
,,!のみからなる
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
Hello,World!
出力例 1
AC
文字列 S は Hello,World! と完全に一致します。
入力例 2
Hello,world!
出力例 2
WA
先頭から 7 文字目の W が、 Hello,World! では大文字ですが S では小文字です。よって S は Hello,World! と一致しません。
入力例 3
Hello!World!
出力例 3
WA
Score : 100 points
Problem Statement
Given a string S, print AC if it perfectly matches Hello,World!; otherwise, print WA.
What is a perfect match?
Strings A is said to perfectly match B when the length of A is equal to that of B, and the i-th character of A is the same as the i-th character of B for every integer i such that 1 \le i \le |A|.Constraints
- 1 \le |S| \le 15
- S consists of English lowercase letters, English uppercase letters,
,, and!.
Input
Input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
Hello,World!
Sample Output 1
AC
The string S perfectly matches Hello,World!.
Sample Input 2
Hello,world!
Sample Output 2
WA
The seventh character from the beginning should be an uppercase W in Hello,World!, but S has a lowercase w in that position. Thus, S does not match Hello,World!.
Sample Input 3
Hello!World!
Sample Output 3
WA
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
相異なる二つの文字列 S, T が与えられます。
S が T よりも辞書順で小さい場合は Yes を、大きい場合は No を出力してください。
辞書順とは?
辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、相異なる文字列 S と文字列 T の大小を判定するアルゴリズムを以下に説明します。
以下では「 S の i 文字目の文字」を S_i のように表します。また、 S が T より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。
- S と T のうち長さが短い方の文字列の長さを L とします。i=1,2,\dots,L に対して S_i と T_i が一致するか調べます。
- S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_j と T_j を比較して、 S_j がアルファベット順で T_j より小さい場合は S \lt T 、大きい場合は S \gt T と決定して、アルゴリズムを終了します。
- S_i \neq T_i である i が存在しない場合、 S と T の長さを比較して、S が T より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。
なお、主要なプログラミング言語の多くでは、文字列の辞書順による比較は標準ライブラリに含まれる関数や演算子として実装されています。詳しくは各言語のリファレンスをご参照ください。
制約
- S, T は英小文字からなる長さ 1 以上 10 以下の相異なる文字列である。
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
S が T より辞書順で小さい場合は Yes を、大きい場合は No を出力せよ。
入力例 1
abc atcoder
出力例 1
Yes
abc と atcoder は 1 文字目が同じで 2 文字目が異なります。 アルファベットの b は t よりもアルファベット順で先に来るので、 abc の方が atcoder よりも辞書順で小さいことがわかります。
入力例 2
arc agc
出力例 2
No
入力例 3
a aa
出力例 3
Yes
Score : 100 points
Problem Statement
You are given two different strings S and T.
If S is lexicographically smaller than T, print Yes; otherwise, print No.
What is the lexicographical order?
Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings S and T.
Below, let S_i denote the i-th character of S. Also, if S is lexicographically smaller than T, we will denote that fact as S \lt T; if S is lexicographically larger than T, we will denote that fact as S \gt T.
- Let L be the smaller of the lengths of S and T. For each i=1,2,\dots,L, we check whether S_i and T_i are the same.
- If there is an i such that S_i \neq T_i, let j be the smallest such i. Then, we compare S_j and T_j. If S_j comes earlier than T_j in alphabetical order, we determine that S \lt T and quit; if S_j comes later than T_j, we determine that S \gt T and quit.
- If there is no i such that S_i \neq T_i, we compare the lengths of S and T. If S is shorter than T, we determine that S \lt T and quit; if S is longer than T, we determine that S \gt T and quit.
Note that many major programming languages implement lexicographical comparison of strings as operators or functions in standard libraries. For more detail, see your language's reference.
Constraints
- S and T are different strings, each of which consists of lowercase English letters and has a length of between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
S T
Output
If S is lexicographically smaller than T, print Yes; otherwise, print No.
Sample Input 1
abc atcoder
Sample Output 1
Yes
abc and atcoder begin with the same character, but their second characters are different. Since b comes earlier than t in alphabetical order, we can see that abc is lexicographically smaller than atcoder.
Sample Input 2
arc agc
Sample Output 2
No
Sample Input 3
a aa
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正の整数 N が与えられます。
N=2^x3^y を満たす整数 x,y が存在するなら Yes 、そうでなければ No と出力してください。
制約
- 1\leq N\leq10^{18}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
条件を満たす整数 x,y が存在するなら Yes 、そうでなければ No と 1 行に出力せよ。
入力例 1
324
出力例 1
Yes
x=2,y=4 とすると、2^x3^y=2^23^4=4\times81=324 となるため条件を満たします。
よって、Yes と出力してください。
入力例 2
5
出力例 2
No
どのような整数 x,y をとっても 2^x3^y=5 とすることはできません。
よって、No と出力してください。
入力例 3
32
出力例 3
Yes
x=5,y=0 とすると、2^x3^y=32\times1=32 となるため、Yes と出力してください。
入力例 4
37748736
出力例 4
Yes
Score : 200 points
Problem Statement
You are given a positive integer N.
If there are integers x and y such that N=2^x3^y, print Yes; otherwise, print No.
Constraints
- 1\leq N\leq10^{18}
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print a single line containing Yes if there are integers x and y that satisfy the condition, and No otherwise.
Sample Input 1
324
Sample Output 1
Yes
For x=2,y=4, we have 2^x3^y=2^23^4=4\times81=324, so the condition is satisfied.
Thus, you should print Yes.
Sample Input 2
5
Sample Output 2
No
There are no integers x and y such that 2^x3^y=5.
Thus, you should print No.
Sample Input 3
32
Sample Output 3
Yes
For x=5,y=0, we have 2^x3^y=32\times1=32, so you should print Yes.
Sample Input 4
37748736
Sample Output 4
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1 から N までの番号が付いた N 人のプレイヤーが総当たり戦をしました。この総当たり戦で行われた試合全てについて、二人の一方が勝ち、もう一方が負けました。
総当たり戦の結果は N 個の長さ N の文字列 S_1,S_2,\ldots,S_N によって以下の形式で与えられます。
-
i\neq j のとき、S_i の j 文字目は
o,xのいずれかであり、oのときプレイヤー i がプレイヤー j に勝ったことを、xのときプレイヤー i がプレイヤー j に負けたことを意味する。 -
i=j のとき、S_i の j 文字目は
-である。
総当たり戦で勝った試合数が多いほうが順位が上であり、勝った試合数が同じ場合は、プレイヤーの番号が小さいほうが順位が上となります。 N 人のプレイヤーの番号を順位が高い順に答えてください。
制約
- 2\leq N\leq 100
- N は整数
- S_i は
o,x,-からなる長さ N の文字列 - S_1,\ldots,S_N は問題文中の形式を満たす
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
N 人のプレイヤーの番号を、順位が高い順に空白区切りで出力せよ。
入力例 1
3 -xx o-x oo-
出力例 1
3 2 1
プレイヤー 1 は 0 勝、プレイヤー 2 は 1 勝、プレイヤー 3 は 2 勝なので、プレイヤーの番号は順位が高い順に 3,2,1 です。
入力例 2
7 -oxoxox x-xxxox oo-xoox xoo-ooo ooxx-ox xxxxx-x oooxoo-
出力例 2
4 7 3 1 5 2 6
プレイヤー 4 とプレイヤー 7 はどちらも 5 勝ですが、プレイヤー番号が小さいプレイヤー 4 のほうが順位が上になります。
Score : 200 points
Problem Statement
There are N players numbered 1 to N, who have played a round-robin tournament. For every match in this tournament, one player won and the other lost.
The results of the matches are given as N strings S_1,S_2,\ldots,S_N of length N each, in the following format:
-
If i\neq j, the j-th character of S_i is
oorx.omeans that player i won against player j, andxmeans that player i lost to player j. -
If i=j, the j-th character of S_i is
-.
The player with more wins ranks higher. If two players have the same number of wins, the player with the smaller player number ranks higher. Report the player numbers of the N players in descending order of rank.
Constraints
- 2\leq N\leq 100
- N is an integer.
- S_i is a string of length N consisting of
o,x, and-. - S_1,\ldots,S_N conform to the format described in the problem statement.
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the player numbers of the N players in descending order of rank.
Sample Input 1
3 -xx o-x oo-
Sample Output 1
3 2 1
Player 1 has 0 wins, player 2 has 1 win, and player 3 has 2 wins. Thus, the player numbers in descending order of rank are 3,2,1.
Sample Input 2
7 -oxoxox x-xxxox oo-xoox xoo-ooo ooxx-ox xxxxx-x oooxoo-
Sample Output 2
4 7 3 1 5 2 6
Both players 4 and 7 have 5 wins, but player 4 ranks higher because their player number is smaller.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 頂点の木 T があり、 i (1\leq i\leq N-1) 番目の辺は頂点 U_i と頂点 V_i を結んでいます。
T 上の相異なる 2 頂点 X,Y が与えられるので、 頂点 X から頂点 Y への単純パス上の頂点(端点含む)を順に列挙してください。
ただし、木上の任意の相異なる 2 頂点 a,b について、a から b への単純パスがただ一つ存在することが証明できます。
単純パスとは?
グラフ G 上の頂点 X,Y に対して、頂点列 v_1,v_2, \ldots, v_k であって、 v_1=X, v_k=Y かつ、1\leq i\leq k-1 に対して v_i と v_{i+1} が辺で結ばれているようなものを頂点 X から頂点 Y への パス と呼びます。 さらに、v_1,v_2, \ldots, v_k がすべて異なるようなものを頂点 X から頂点 Y への 単純パス と呼びます。制約
- 1\leq N\leq 2\times 10^5
- 1\leq X,Y\leq N
- X\neq Y
- 1\leq U_i,V_i\leq N
- 入力はすべて整数
- 与えられるグラフは木
入力
入力は以下の形式で標準入力から与えられる。
N X Y
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
出力
頂点 X から頂点 Y への単純パス上の頂点番号を順に空白区切りで出力せよ。
入力例 1
5 2 5 1 2 1 3 3 4 3 5
出力例 1
2 1 3 5
木 T は以下のような形であり、頂点 2 から頂点 5への単純パスは
頂点 2 \to 頂点 1 \to 頂点 3 \to 頂点 5 となります。
よって、2,1,3,5 をこの順に空白区切りで出力します。

入力例 2
6 1 2 3 1 2 5 1 2 4 1 2 6
出力例 2
1 2
木 T は以下のような形です。

Score : 300 points
Problem Statement
There is a tree T with N vertices. The i-th edge (1\leq i\leq N-1) connects vertex U_i and vertex V_i.
You are given two different vertices X and Y in T. List all vertices along the simple path from vertex X to vertex Y in order, including endpoints.
It can be proved that, for any two different vertices a and b in a tree, there is a unique simple path from a to b.
What is a simple path?
For vertices X and Y in a graph G, a path from vertex X to vertex Y is a sequence of vertices v_1,v_2, \ldots, v_k such that v_1=X, v_k=Y, and v_i and v_{i+1} are connected by an edge for each 1\leq i\leq k-1. Additionally, if all of v_1,v_2, \ldots, v_k are distinct, the path is said to be a simple path from vertex X to vertex Y.Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq X,Y\leq N
- X\neq Y
- 1\leq U_i,V_i\leq N
- All values in the input are integers.
- The given graph is a tree.
Input
The input is given from Standard Input in the following format:
N X Y
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Output
Print the indices of all vertices along the simple path from vertex X to vertex Y in order, with spaces in between.
Sample Input 1
5 2 5 1 2 1 3 3 4 3 5
Sample Output 1
2 1 3 5
The tree T is shown below. The simple path from vertex 2 to vertex 5 is 2 \to 1 \to 3 \to 5.
Thus, 2,1,3,5 should be printed in this order, with spaces in between.

Sample Input 2
6 1 2 3 1 2 5 1 2 4 1 2 6
Sample Output 2
1 2
The tree T is shown below.

Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
空の箱があります。
髙橋君は以下の 2 種類の魔法を好きな順番で好きな回数使えます。
- 魔法 A :箱の中にボールを 1 つ増やす
- 魔法 B :箱の中のボールの数を 2 倍にする
合計 \mathbf{120} 回以内の魔法で、箱の中のボールの数をちょうど N 個にする方法を 1 つ教えてください。
なお、与えられた制約のもとで条件を満たす方法が必ず存在することが示せます。
魔法以外の方法でボールの数を変化させることはできません。
制約
- 1 \leq N \leq 10^{18}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
A , B のみからなる文字列 S を出力せよ。
S の i 文字目が A ならば、髙橋君が i 回目に使う魔法が魔法 A であることを表し、B ならば魔法 B であることを表す。
S の長さは \mathbf{120} 以下でなければならない。
入力例 1
5
出力例 1
AABA
ボールの数は、0 \xrightarrow{A} 1\xrightarrow{A} 2 \xrightarrow{B}4\xrightarrow{A} 5 と変化します。
AAAAA などの答えも正解になります。
入力例 2
14
出力例 2
BBABBAAAB
ボールの数は、0 \xrightarrow{B} 0 \xrightarrow{B} 0 \xrightarrow{A}1 \xrightarrow{B} 2 \xrightarrow{B} 4 \xrightarrow{A}5 \xrightarrow{A}6 \xrightarrow{A} 7 \xrightarrow{B}14 と変化します。
S の長さを最小化する必要はありません。
Score : 300 points
Problem Statement
We have an empty box.
Takahashi can cast the following two spells any number of times in any order.
- Spell A: puts one new ball into the box.
- Spell B: doubles the number of balls in the box.
Tell us a way to have exactly N balls in the box with at most \mathbf{120} casts of spells.
It can be proved that there always exists such a way under the Constraints given.
There is no way other than spells to alter the number of balls in the box.
Constraints
- 1 \leq N \leq 10^{18}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
Output
Print a string S consisting of A and B.
The i-th character of S should represent the spell for the i-th cast.
S must have at most \mathbf{120} characters.
Sample Input 1
5
Sample Output 1
AABA
This changes the number of balls as follows: 0 \xrightarrow{A} 1\xrightarrow{A} 2 \xrightarrow{B}4\xrightarrow{A} 5.
There are also other acceptable outputs, such as AAAAA.
Sample Input 2
14
Sample Output 2
BBABBAAAB
This changes the number of balls as follows: 0 \xrightarrow{B} 0 \xrightarrow{B} 0 \xrightarrow{A}1 \xrightarrow{B} 2 \xrightarrow{B} 4 \xrightarrow{A}5 \xrightarrow{A}6 \xrightarrow{A} 7 \xrightarrow{B}14.
It is not required to minimize the length of S.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
空の数列 A があります。
クエリが Q 個与えられるので、与えられた順番に処理してください。
クエリは次の 3 種類のいずれかです。
-
1 x: A に x を追加する。 -
2 x k: A の x 以下の要素のうち、大きい方から k 番目の値を出力する。(k は \bf{5} 以下)
ただし、A に x 以下の要素が k 個以上存在しないときは-1と出力する。 -
3 x k: A の x 以上の要素のうち、小さい方から k 番目の値を出力する。(k は \bf{5} 以下)
ただし、A に x 以上の要素が k 個以上存在しないときは-1と出力する。
制約
- 1\leq Q \leq 2\times 10^5
- 1\leq x\leq 10^{18}
- 1\leq k\leq 5
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
i 番目のクエリ \text{query}_i では、まずクエリの種類 c_i (1,2,3 のいずれか) が与えられる。
c_i=1 の場合は x が追加で与えられ、c_i=2,3 の場合は x,k が追加で与えられる。
すなわち、各クエリは以下に示す 3 つの形式のいずれかである。
1 x
2 x k
3 x k
出力
c_i=2,3 を満たすクエリの個数を q として q 行出力せよ。
j(1\leq j\leq q) 行目では j 番目のそのようなクエリに対する答えを出力せよ。
入力例 1
11 1 20 1 10 1 30 1 20 3 15 1 3 15 2 3 15 3 3 15 4 2 100 5 1 1 2 100 5
出力例 1
20 20 30 -1 -1 1
\text{query}_{1,2,3,4} が終了した段階で、A=(20,10,30,20) となっています。
\text{query}_{5,6,7} について、A の 15 以上の要素は (20,30,20) です。
このうち小さい方から 1 番目の値は 20 、2 番目の値は 20 、3 番目の値は 30 です。
Score : 400 points
Problem Statement
We have an empty sequence A.
Given Q queries, process them in order.
Each query is of one of the following three types.
-
1 x: Insert x to A. -
2 x k: Among the elements of A that are less than or equal to x, print the k-th largest value. (k is no more than \bf{5})
If there are less than k elements of A that are less than or equal to x, then print-1. -
3 x k: Among the elements of A that are greater than or equal to x, print the k-th smallest value. (k is no more than \bf{5})
If there are less than k elements of A that are greater than or equal to x, then print-1.
Constraints
- 1\leq Q \leq 2\times 10^5
- 1\leq x\leq 10^{18}
- 1\leq k\leq 5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
In the i-th query \text{query}_i, the type of query c_i (which is either 1, 2, or 3) is given first.
If c_i=1, then x is additionally given; if c_i=2, 3, then x and k are additionally given.
In other words, each query is given in one of the following three formats:
1 x
2 x k
3 x k
Output
Print q lines, where q is the number of queries such that c_i=2,3.
The j-th line (1\leq j\leq q) should contain the answer for the j-th such query.
Sample Input 1
11 1 20 1 10 1 30 1 20 3 15 1 3 15 2 3 15 3 3 15 4 2 100 5 1 1 2 100 5
Sample Output 1
20 20 30 -1 -1 1
After \text{query}_{1,2,3,4} have been processed, we have A=(20,10,30,20).
For \text{query}_{5,6,7}, the elements of A greater than or equal to 15 are (20,30,20).
The 1-st smallest value of them is 20; the 2-nd is 20; the 3-rd is 30.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
頂点に 1 から N までの、辺に 1 から M までの番号がついた N 頂点 M 辺の単純無向グラフがあります。 辺 i は頂点 u_i と頂点 v_i を結んでいます。
また、全ての頂点は赤か青のいずれか一方で塗られています。頂点 i の色は C_i で表されて、C_i が 0 ならば頂点 i は赤く、1 ならば頂点 i は青く塗られています。
今、高橋君が頂点 1 に、青木君が頂点 N にいます。
2 人は次の行動を 0 回以上好きな回数繰り返します。
- 2 人が同時に、今いる頂点に隣接している頂点のいずれか 1 個に移動する。
ただし、高橋君の移動先の頂点の色と、青木君の移動先の頂点の色は異なる必要がある。
上記の行動を繰り返すことで、高橋君が頂点 N に、青木君が頂点 1 にいる状態にできますか?
可能である場合は必要な行動回数の最小値を答えてください。不可能である場合は -1 を出力してください。
入力のはじめに T が与えられるので、T 個のテストケースについて問題を解いてください。
制約
- 1 \leq T \leq 1000
- 2 \leq N \leq 2000
- 1 \leq M \leq \min(\frac{N(N-1)}{2}, 2000)
- C_i \in \lbrace 0, 1 \rbrace
- 1 \leq u_i, v_i \leq N
- 入力で与えられるグラフは単純
- 入力される値は全て整数
- 全てのテストケースに対する N の総和は 2000 を超えない。
- 全てのテストケースに対する M の総和は 2000 を超えない。
入力
入力は以下の形式で標準入力から与えられる。ここで \text{test}_i は i 番目のテストケースを意味する。
T
\text{test}_1
\text{test}_2
\vdots
\text{test}_T
各テストケースは以下の形式で与えられる。
N M C_1 C_2 \dots C_N u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
T 行出力せよ。i 行目には i 番目のテストケースに対する答えを出力せよ。
各テストケースでは、高橋君が頂点 N に、青木君が頂点 1 にいる状態にできる場合は必要な行動回数の最小値を、できない場合は -1 を出力せよ。
入力例 1
3 4 4 0 1 0 1 1 2 2 3 1 3 2 4 3 3 0 1 0 1 2 2 3 1 3 6 6 0 0 1 1 0 1 1 2 2 6 3 6 4 6 4 5 2 4
出力例 1
3 -1 3
1 番目のテストケースでは、高橋君と青木君は以下のように行動することで、 3 回の行動で目的の状態を達成することができて、これが最小です。
- 高橋君が頂点 3 に、青木君が頂点 2 に移動する。
- 高橋君が頂点 2 に、青木君が頂点 3 に移動する。
- 高橋君が頂点 4 に、青木君が頂点 1 に移動する。
ここで、1 回目の移動で高橋君と青木君がともに頂点 2 に移動することはできないのに注意してください。(なぜならば、高橋君の移動先の頂点の色と青木君の移動先の頂点の色は異なる必要があるからです。)
2 番目のテストケースでは、2 人はどのように行動しても目的の状態を達成することはできません。
Score : 500 points
Problem Statement
There is a simple undirected graph with N vertices numbered 1 through N and M edges numbered 1 through M. Edge i connects vertex u_i and vertex v_i.
Every vertex is painted either red or blue. The color of vertex i is represented by C_i; vertex i is painted red if C_i is 0 and blue if C_i is 1.
Now, Takahashi is on vertex 1 and Aoki is on vertex N.
They may repeat the following move zero or more times.
- Each of the two simultaneously moves to a vertex adjacent to the current vertex.
Here, the vertices that Takahashi and Aoki move to must have different colors.
By repeating the move above, can Takahashi and Aoki simultaneously end up on vertices N and 1, respectively?
If it is possible, find the minimum number of moves required. If it is impossible, print -1.
You are given T at the beginning of the input. Solve the problem for T test cases.
Constraints
- 1 \leq T \leq 1000
- 2 \leq N \leq 2000
- 1 \leq M \leq \min(\frac{N(N-1)}{2}, 2000)
- C_i \in \lbrace 0, 1 \rbrace
- 1 \leq u_i, v_i \leq N
- The graph given in the input is simple.
- All values in the input are integers.
- The sum of N over all test cases does not exceed 2000.
- The sum of M over all test cases does not exceed 2000.
Input
The input is given from Standard Input in the following format, where \text{test}_i denotes the i-th test case:
T
\text{test}_1
\text{test}_2
\vdots
\text{test}_T
Each test case is given in the following format:
N M C_1 C_2 \dots C_N u_1 v_1 u_2 v_2 \vdots u_M v_M
Output
Print T lines. The i-th line should contain the answer to the i-th test case.
For each test case, print the minimum number of moves required for Takahashi and Aoki to simultaneously end up in vertices N and 1, respectively, if it is possible, and -1 otherwise.
Sample Input 1
3 4 4 0 1 0 1 1 2 2 3 1 3 2 4 3 3 0 1 0 1 2 2 3 1 3 6 6 0 0 1 1 0 1 1 2 2 6 3 6 4 6 4 5 2 4
Sample Output 1
3 -1 3
For the 1-st test case, Takahashi and Aoki can achieve the objective by making the following 3 moves, which is the minimum number:
- Takahashi moves to vertex 3, and Aoki moves to vertex 2.
- Takahashi moves to vertex 2, and Aoki moves to vertex 3.
- Takahashi moves to vertex 4, and Aoki moves to vertex 1.
Note that in the 1-st move, it is disallowed that both Takahashi and Aoki move to vertex 2 (because the colors of vertices that Takahashi and Aoki move to must be different.)
For the 2-nd case, no matter how they move, they cannot achieve the objective.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の有向グラフが与えられます。頂点には 1 から N の番号、辺には 1 から M の番号がついています。辺 i\,(1 \leq i \leq M) は頂点 s_i から頂点 t_i に向かう長さ 1 の辺です。
各 i\,(1 \leq i \leq M) について、辺 i のみ通れないときの頂点 1 から頂点 N までの最短距離を求めてください。ただし、頂点 1 から頂点 N にたどり着けない場合は -1 を出力してください。
制約
- 2 \leq N \leq 400
- 1 \leq M \leq N(N-1)
- 1 \leq s_i,t_i \leq N
- s_i \neq t_i
- (s_i,t_i) \neq (s_j,t_j) (i \neq j)
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M s_1 t_1 s_2 t_2 \vdots s_M t_M
出力
M 行出力せよ。
i 行目には、辺 i のみ通れないときの頂点 1 から頂点 N までの最短距離を出力せよ。ただし、頂点 1 から頂点 N にたどり着けない場合は -1 を出力せよ。
入力例 1
3 3 1 2 1 3 2 3
出力例 1
1 2 1
入力例 2
4 4 1 2 2 3 2 4 3 4
出力例 2
-1 2 3 2
辺 1 のみ通れないとき、頂点 1 から頂点 N にたどり着けないので -1 を出力します。
入力例 3
5 10 1 2 1 4 1 5 2 1 2 3 3 1 3 2 3 5 4 2 4 3
出力例 3
1 1 3 1 1 1 1 1 1 1
入力例 4
4 1 1 2
出力例 4
-1
Score : 500 points
Problem Statement
You are given a directed graph with N vertices and M edges. The vertices are numbered 1 through N, and the edges are numbered 1 through M. Edge i (1 \leq i \leq M) goes from Vertex s_i to Vertex t_i and has a length of 1.
For each i (1 \leq i \leq M), find the shortest distance from Vertex 1 to Vertex N when all edges except Edge i are passable, or print -1 if Vertex N is unreachable from Vertex 1.
Constraints
- 2 \leq N \leq 400
- 1 \leq M \leq N(N-1)
- 1 \leq s_i,t_i \leq N
- s_i \neq t_i
- (s_i,t_i) \neq (s_j,t_j) (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M s_1 t_1 s_2 t_2 \vdots s_M t_M
Output
Print M lines.
The i-th line should contain the shortest distance from Vertex 1 to Vertex N when all edges except Edge i are passable, or -1 if Vertex N is unreachable from Vertex 1.
Sample Input 1
3 3 1 2 1 3 2 3
Sample Output 1
1 2 1
Sample Input 2
4 4 1 2 2 3 2 4 3 4
Sample Output 2
-1 2 3 2
Vertex N is unreachable from Vertex 1 when all edges except Edge 1 are passable, so the corresponding line contains -1.
Sample Input 3
5 10 1 2 1 4 1 5 2 1 2 3 3 1 3 2 3 5 4 2 4 3
Sample Output 3
1 1 3 1 1 1 1 1 1 1
Sample Input 4
4 1 1 2
Sample Output 4
-1