Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
縦 H マス, 横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i, j) と呼びます。
はじめ、グリッド上には、ある 縦横 2 マス以上 の部分長方形の内部にあるマスにクッキーが 1 枚ずつ置かれていて、それ以外のマスにはクッキーが置かれていません。
形式的に説明すると、以下の条件を全て満たす 4 つの整数の組 (a,b,c,d) がただ 1 つ存在します。
- 1 \leq a \lt b \leq H
- 1 \leq c \lt d \leq W
- グリッド上のマスのうち、a \leq i \leq b, c \leq j \leq d を満たす全てのマス (i, j) にはクッキーが 1 枚ずつ置かれていて、それ以外のマスにはクッキーが置かれていない。
ところが、すぬけ君がグリッド上のクッキーのどれか 1 枚を取って食べてしまいました。
すぬけ君がクッキーを取ったマスは、クッキーが置かれていない状態に変わります。
すぬけ君がクッキーを食べた後のグリッドの状態が入力として与えられます。
マス (i, j) の状態は文字 S_{i,j} として与えられて、# はクッキーが置かれているマスを, . はクッキーが置かれていないマスを意味します。
すぬけ君が食べたクッキーが元々置かれていたマスを答えてください。(答えは一意に定まります。)
制約
- 2 \leq H, W \leq 500
- S_{i,j} は
#または.
入力
入力は以下の形式で標準入力から与えられる。
H W
S_{1,1}S_{1,2}\dotsS_{1,W}
S_{2,1}S_{2,2}\dotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\dotsS_{H,W}
出力
すぬけ君が食べたクッキーが元々置かれていたマスを (i, j) とする。i, j をこの順に空白区切りで出力せよ。
入力例 1
5 6 ...... ..#.#. ..###. ..###. ......
出力例 1
2 4
はじめ、クッキーは (2, 3) を左上、(4, 5) を右下とする部分長方形の内部にあるマスに置かれていて、すぬけ君は (2, 4) にあるクッキーを食べたことがわかります。よって (2, 4) を出力します。
入力例 2
3 2 #. ## ##
出力例 2
1 2
はじめ、クッキーは (1, 1) を左上、(3, 2) を右下とする部分長方形の内部にあるマスに置かれていて、すぬけ君は (1, 2) にあるクッキーを食べたことがわかります。
入力例 3
6 6 ..#### ..##.# ..#### ..#### ..#### ......
出力例 3
2 5
Score : 300 points
Problem Statement
There is a grid with H rows and W columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left.
Initially, there was one cookie on each square inside a rectangle whose height and width were at least 2 squares long, and no cookie on the other squares.
Formally, there was exactly one quadruple of integers (a,b,c,d) that satisfied all of the following conditions.
- 1 \leq a \lt b \leq H
- 1 \leq c \lt d \leq W
- There was one cookie on each square (i, j) such that a \leq i \leq b, c \leq j \leq d, and no cookie on the other squares.
However, Snuke took and ate one of the cookies on the grid.
The square that contained that cookie is now empty.
As the input, you are given the state of the grid after Snuke ate the cookie.
The state of the square (i, j) is given as the character S_{i,j}, where # means a square with a cookie, and . means a square without one.
Find the square that contained the cookie eaten by Snuke. (The answer is uniquely determined.)
Constraints
- 2 \leq H, W \leq 500
- S_{i,j} is
#or..
Input
The input is given from Standard Input in the following format:
H W
S_{1,1}S_{1,2}\dotsS_{1,W}
S_{2,1}S_{2,2}\dotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\dotsS_{H,W}
Output
Let (i, j) the square contained the cookie eaten by Snuke. Print i and j in this order, separated by a space.
Sample Input 1
5 6 ...... ..#.#. ..###. ..###. ......
Sample Output 1
2 4
Initially, cookies were on the squares inside the rectangle with (2, 3) as the top-left corner and (4, 5) as the bottom-right corner, and Snuke ate the cookie on (2, 4). Thus, you should print (2, 4).
Sample Input 2
3 2 #. ## ##
Sample Output 2
1 2
Initially, cookies were placed on the squares inside the rectangle with (1, 1) as the top-left corner and (3, 2) as the bottom-right corner, and Snuke ate the cookie at (1, 2).
Sample Input 3
6 6 ..#### ..##.# ..#### ..#### ..#### ......
Sample Output 3
2 5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君が運営する SNS「Twidai」にはユーザー 1 からユーザー N までの N 人のユーザーがいます。 Twidai では、ユーザーは別のユーザーをフォローすることや、フォローを解除することができます。
Twidai がサービスを開始してから、Q 回の操作が行われました。 i 回目 (1\leq i\leq Q) の操作は 3 つの整数 T _ i, A _ i, B _ i で表され、それぞれ次のような操作を表します。
- T _ i=1 のとき:ユーザー A _ i がユーザー B _ i をフォローしたことを表す。この操作の時点でユーザー A _ i がユーザー B _ i をフォローしている場合、ユーザーのフォロー状況に変化はない。
- T _ i=2 のとき:ユーザー A _ i がユーザー B _ i のフォローを解除したことを表す。この操作の時点でユーザー A _ i がユーザー B _ i をフォローしていない場合、ユーザーのフォロー状況に変化はない。
- T _ i=3 のとき:ユーザー A _ i とユーザー B _ i が互いにフォローしているかをチェックすることを表す。この操作の時点でユーザー A _ i がユーザー B _ i をフォローしており、かつユーザー B _ i がユーザー A _ i をフォローしているとき、このチェックに対して
Yesと答え、そうでないときこのチェックに対してNoと答える必要がある。
サービス開始時には、どのユーザーも他のユーザーをフォローしていません。
すべての T _ i=3 であるような操作に対して、i が小さいほうから順番に正しい答えを出力してください。
制約
- 2 \leq N \leq 10 ^ 9
- 1 \leq Q \leq 2\times10 ^ 5
- T _ i=1,2,3\ (1\leq i\leq Q)
- 1 \leq A _ i \leq N\ (1\leq i\leq Q)
- 1 \leq B _ i \leq N\ (1\leq i\leq Q)
- A _ i\neq B _ i\ (1\leq i\leq Q)
- T _ i=3 となる i\ (1\leq i\leq Q) が存在する
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q T _ 1 A _ 1 B _ 1 T _ 2 A _ 2 B _ 2 \vdots T _ Q A _ Q B _ Q
出力
T _ i=3 であるような i\ (1\leq i\leq Q) の個数を X として、X 行出力せよ。 j\ (1\leq j\leq X) 行目には j 番目の T _ i=3 であるような操作に対する答えを出力せよ。
入力例 1
3 9 1 1 2 3 1 2 1 2 1 3 1 2 1 2 3 1 3 2 3 1 3 2 1 2 3 1 2
出力例 1
No Yes No No
Twidai には 3 人のユーザーがいます。 9 回の操作はそれぞれ次のようになっています。
- ユーザー 1 がユーザー 2 をフォローします。そのほかにフォローしている/されているユーザーはいません。
- ユーザー 1 とユーザー 2 が互いにフォローしているかチェックします。ユーザー 1 はユーザー 2 をフォローしていますが、ユーザー 2 はユーザー 1 をフォローしていません。この操作への正しい答えは
Noです。 - ユーザー 2 がユーザー 1 をフォローします。
- ユーザー 1 とユーザー 2 が互いにフォローしているかチェックします。ユーザー 1 はユーザー 2 をフォローしており、ユーザー 2 はユーザー 1 をフォローしています。この操作への正しい答えは
Yesです。 - ユーザー 2 がユーザー 3 をフォローします。
- ユーザー 3 がユーザー 2 をフォローします。
- ユーザー 1 とユーザー 3 が互いにフォローしているかチェックします。ユーザー 1 はユーザー 3 をフォローしておらず、ユーザー 3 もユーザー 1 をフォローしていません。この操作への正しい答えは
Noです。 - ユーザー 1 がユーザー 2 のフォローを解除します。
- ユーザー 1 とユーザー 2 が互いにフォローしているかチェックします。ユーザー 2 はユーザー 1 をフォローしていますが、ユーザー 1 はユーザー 2 をフォローしていません。この操作への正しい答えは
Noです。
入力例 2
2 8 1 1 2 1 2 1 3 1 2 1 1 2 1 1 2 1 1 2 2 1 2 3 1 2
出力例 2
Yes No
同じユーザーに対して何度もフォロー操作をする場合があります。
入力例 3
10 30 3 1 6 3 5 4 1 6 1 3 1 7 3 8 4 1 1 6 2 4 3 1 6 5 1 5 6 1 1 8 1 8 1 2 3 10 1 7 6 3 5 6 1 6 7 3 6 7 1 9 5 3 8 6 3 3 8 2 6 9 1 7 1 3 10 8 2 9 2 1 10 9 2 6 10 2 6 8 3 1 6 3 1 8 2 8 5 1 9 10
出力例 3
No No No No Yes Yes No No No Yes Yes
Score : 300 points
Problem Statement
Takahashi runs an SNS "Twidai," which has N users from user 1 through user N. In Twidai, users can follow or unfollow other users.
Q operations have been performed since Twidai was launched. The i-th (1\leq i\leq Q) operation is represented by three integers T_i, A_i, and B_i, whose meanings are as follows:
- If T_i = 1: it means that user A_i follows user B_i. If user A_i is already following user B_i at the time of this operation, it does not make any change.
- If T_i = 2: it means that user A_i unfollows user B_i. If user A_i is not following user B_i at the time of this operation, it does not make any change.
- If T_i = 3: it means that you are asked to determine if users A_i and B_i are following each other. You need to print
Yesif user A_i is following user B_i and user B_i is following user A_i, andNootherwise.
When the service was launched, no users were following any users.
Print the correct answers for all operations such that T_i = 3 in ascending order of i.
Constraints
- 2 \leq N \leq 10 ^ 9
- 1 \leq Q \leq 2\times10 ^ 5
- T _ i=1,2,3\ (1\leq i\leq Q)
- 1 \leq A _ i \leq N\ (1\leq i\leq Q)
- 1 \leq B _ i \leq N\ (1\leq i\leq Q)
- A _ i\neq B _ i\ (1\leq i\leq Q)
- There exists i\ (1\leq i\leq Q) such that T _ i=3.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N Q T _ 1 A _ 1 B _ 1 T _ 2 A _ 2 B _ 2 \vdots T _ Q A _ Q B _ Q
Output
Print X lines, where X is the number of i's (1\leq i\leq Q) such that T _ i=3. The j-th (1\leq j\leq X) line should contain the answer to the j-th operation such that T _ i=3.
Sample Input 1
3 9 1 1 2 3 1 2 1 2 1 3 1 2 1 2 3 1 3 2 3 1 3 2 1 2 3 1 2
Sample Output 1
No Yes No No
Twidai has three users. The nine operations are as follows.
- User 1 follows user 2. No other users are following or followed by any users.
- Determine if users 1 and 2 are following each other. User 1 is following user 2, but user 2 is not following user 1, so
Nois the correct answer to this operation. - User 2 follows user 1.
- Determine if users 1 and 2 are following each other. User 1 is following user 2, and user 2 is following user 1, so
Yesis the correct answer to this operation. - User 2 follows user 3.
- User 3 follows user 2.
- Determine if users 1 and 3 are following each other. User 1 is not following user 3, and user 3 is not following user 1, so
Nois the correct answer to this operation. - User 1 unfollows user 2.
- Determine if users 1 and 2 are following each other. User 2 is following user 1, but user 1 is not following user 2, so
Nois the correct answer to this operation.
Sample Input 2
2 8 1 1 2 1 2 1 3 1 2 1 1 2 1 1 2 1 1 2 2 1 2 3 1 2
Sample Output 2
Yes No
A user may follow the same user multiple times.
Sample Input 3
10 30 3 1 6 3 5 4 1 6 1 3 1 7 3 8 4 1 1 6 2 4 3 1 6 5 1 5 6 1 1 8 1 8 1 2 3 10 1 7 6 3 5 6 1 6 7 3 6 7 1 9 5 3 8 6 3 3 8 2 6 9 1 7 1 3 10 8 2 9 2 1 10 9 2 6 10 2 6 8 3 1 6 3 1 8 2 8 5 1 9 10
Sample Output 3
No No No No Yes Yes No No No Yes Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は N 匹のモンスターに順に出会います。i 匹目 (1\leq i\leq N) のモンスターの強さは A_i です。
高橋君はそれぞれのモンスターについて逃がすか倒すか選ぶことができます。
高橋君はそれぞれの行動によって次のように経験値を得ます。
- モンスターを逃がした場合、得られる経験値は 0 である。
- 強さが X のモンスターを倒したとき、経験値を X 得る。
ただし、モンスターを倒すのが偶数回目(2, 4, \ldots 回目)であるとき、さらに追加で経験値を X 得る。
N 匹から高橋君が得た経験値の合計としてあり得る最大の値を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq A_i\leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
N 匹から高橋君が得た経験値の合計としてあり得る最大の値を整数で出力せよ。
入力例 1
5 1 5 3 2 7
出力例 1
28
1,2,3,5 匹目のモンスターを倒して、4 匹目のモンスターを逃したとき、高橋君は次のように経験値を得ます。
- 強さが A_1=1 のモンスターを倒す。高橋君は 1 の経験値を得る。
- 強さが A_2=5 のモンスターを倒す。高橋君は 5 の経験値を得る。モンスターを倒すのは 2 回目であるため、追加で経験値 5 を得る。
- 強さが A_3=3 のモンスターを倒す。高橋君は 3 の経験値を得る。
- 4 匹目のモンスターを逃がす。高橋君は経験値を得ない。
- 強さが A_5=7 のモンスターを倒す。高橋君は 7 の経験値を得る。モンスターを倒すのは 4 回目であるため、追加で経験値 7 を得る。
よって、このとき 1+(5+5)+3+0+(7+7)=28 の経験値を得ます。
出会ったモンスターであっても、逃がした場合は倒した回数には数えられないことに注意してください。
高橋君がどのように行動しても得られる経験値は 28 以下であるため、28 を出力します。
なお、この場合においてすべてのモンスターを倒した時に得られる経験値は 1+(5+5)+3+(2+2)+7=25 となります。
入力例 2
2 1000000000 1000000000
出力例 2
3000000000
答えが 32bit 整数型に収まらない可能性があることに注意してください。
Score : 400 points
Problem Statement
Takahashi will encounter N monsters in order. The i-th monster (1\leq i\leq N) has a strength of A_i.
For each monster, he can choose to either let it go or defeat it.
Each action awards him experience points as follows:
- If he lets a monster go, he gains 0 experience points.
- If he defeats a monster with strength X, he gains X experience points.
If it is an even-numbered defeated monster (2nd, 4th, ...), he gains an additional X experience points.
Find the maximum total experience points he can gain from the N monsters.
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq A_i\leq 10^9
- 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
Print the maximum total experience points he can gain from the N monsters as an integer.
Sample Input 1
5 1 5 3 2 7
Sample Output 1
28
If Takahashi defeats the 1st, 2nd, 3rd, and 5th monsters, and lets the 4th monster go, he gains experience points as follows:
- Defeats a monster with strength A_1=1. He gains 1 experience point.
- Defeats a monster with strength A_2=5. He gains 5 experience points. As it is the 2nd defeated monster, he gains an additional 5 points.
- Defeats a monster with strength A_3=3. He gains 3 experience points.
- Lets the 4th monster go. Takahashi gains no experience points.
- Defeats a monster with strength A_5=7. He gains 7 experience points. As it is the 4th defeated monster, he gains an additional 7 points.
Therefore, in this case, he gains 1+(5+5)+3+0+(7+7)=28 experience points.
Note that even if he encounters a monster, if he lets it go, it does not count as defeated.
He can gain at most 28 experience points no matter how he acts, so print 28.
As a side note, if he defeats all monsters in this case, he would gain 1+(5+5)+3+(2+2)+7=25 experience points.
Sample Input 2
2 1000000000 1000000000
Sample Output 2
3000000000
Beware that the answer may not fit in a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ N の非負整数列 A および整数 K が与えられます。ここで二項係数 \dbinom{N}{K} は 10^6 以下であることが保証されます。
A から異なる K 項を選ぶとき、選んだ K 個の数の総 XOR としてあり得る最大値を求めてください。
すなわち \underset{1\leq i_1\lt i_2\lt \ldots\lt i_K\leq N}{\max} A_{i_1}\oplus A_{i_2}\oplus \ldots \oplus A_{i_K} を求めてください。
XOR とは
非負整数 A,B の XOR A \oplus B は、以下のように定義されます。- A \oplus B を二進表記した際の 2^k \, (k \geq 0) の位の数は、A,B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
一般に k 個の整数 p_1, \dots, p_k の XOR は (\cdots ((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k) と定義され、これは p_1, \dots, p_k の順番によらないことが証明できます。
制約
- 1\leq K\leq N\leq 2\times 10^5
- 0\leq A_i\lt 2^{60}
- \dbinom{N}{K}\leq 10^6
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
4 2 3 2 6 4
出力例 1
7
(3,2,6,4) から異なる 2 つの項を選ぶ方法は以下の 6 通りあります。
- (3,2) のとき、選んだ数の総 XOR は 3\oplus 2 = 1 です。
- (3,6) のとき、選んだ数の総 XOR は 3\oplus 6 = 5 です。
- (3,4) のとき、選んだ数の総 XOR は 3\oplus 4 = 7 です。
- (2,6) のとき、選んだ数の総 XOR は 2\oplus 6 = 4 です。
- (2,4) のとき、選んだ数の総 XOR は 2\oplus 4 = 6 です。
- (6,4) のとき、選んだ数の総 XOR は 6\oplus 4 = 2 です。
よって、求める最大値は 7 です。
入力例 2
10 4 1516 1184 1361 2014 1013 1361 1624 1127 1117 1759
出力例 2
2024
Score : 500 points
Problem Statement
You are given a sequence A of non-negative integers of length N, and an integer K. It is guaranteed that the binomial coefficient \dbinom{N}{K} is at most 10^6.
When choosing K distinct elements from A, find the maximum possible value of the XOR of the K chosen elements.
That is, find \underset{1\leq i_1\lt i_2\lt \ldots\lt i_K\leq N}{\max} A_{i_1}\oplus A_{i_2}\oplus \ldots \oplus A_{i_K}.
About XOR
For non-negative integers A,B, the XOR A \oplus B is defined as follows:- In the binary representation of A \oplus B, the bit corresponding to 2^k (k \ge 0) is 1 if and only if exactly one of the bits corresponding to 2^k in A and B is 1, and is 0 otherwise.
In general, the XOR of K integers p_1, \dots, p_k is defined as (\cdots((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k). It can be proved that it does not depend on the order of p_1, \dots, p_k.
Constraints
- 1\leq K\leq N\leq 2\times 10^5
- 0\leq A_i<2^{60}
- \dbinom{N}{K}\leq 10^6
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
4 2 3 2 6 4
Sample Output 1
7
Here are six ways to choose two distinct elements from (3,2,6,4).
- (3,2): The XOR is 3\oplus 2 = 1.
- (3,6): The XOR is 3\oplus 6 = 5.
- (3,4): The XOR is 3\oplus 4 = 7.
- (2,6): The XOR is 2\oplus 6 = 4.
- (2,4): The XOR is 2\oplus 4 = 6.
- (6,4): The XOR is 6\oplus 4 = 2.
Hence, the maximum possible value is 7.
Sample Input 2
10 4 1516 1184 1361 2014 1013 1361 1624 1127 1117 1759
Sample Output 2
2024
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 個の町と M 個のテレポーターがあり、
町は町 1, 町 2, \ldots, 町N と番号づけられています。
それぞれのテレポーターは 2 つの町を双方向に結んでおり、テレポーターを使用する事によってその 2 つの町の間を 1 分で移動することができます。
i 番目のテレポーターは町 U_i と町 V_i を双方向に結んでいますが、 いくつかのテレポーターについては結ぶ町の片方が決まっておらず、 U_i=0 のときそのテレポーターが結ぶ町の片方は町 V_i であるが、 もう片方が未定であることを意味します。
i=1,2,\ldots,N それぞれについて、次の問題を解いてください。
結ぶ町の片方が未定となっているテレポーターの結ぶ先をすべて町 i とする。 この時に町 1 から町 N まで移動するのに最小で何分かかるか求めよ。 町 1 から町 N までテレポーターのみを使って移動するのが不可能な場合は -1 を出力せよ。
制約
- 2 \leq N \leq 3\times 10^5
- 1\leq M\leq 3\times 10^5
- 0\leq U_i<V_i\leq N
- i \neq j ならば (U_i,V_i)\neq (U_j,V_j)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M U_1 V_1 U_2 V_2 \vdots U_M V_M
出力
N 個の整数を空白区切りで出力せよ。 ここで、k 番目の整数は i=k とした時の問題に対する答えである.
入力例 1
3 2 0 2 1 2
出力例 1
-1 -1 2
結ぶ先が未定となっているテレポーターの結び先を町 1 としたとき、
1 番目と 2 番目のテレポーターはともに町 1 と町 2 を結びます。
このとき、町 1 から町 3 への移動はできません。
結ぶ先が未定となっているテレポーターの結び先を町 2 としたとき、
1 番目のテレポーターは町 2 同士を、 2 番目のテレポーターは町 1 と町 2 を結びます。
このときもやはり、町 1 から町 3 への移動はできません。
結ぶ先が未定となっているテレポーターの結び先を町 3 としたとき、
1 番目のテレポーターは町 3 と町 2 を、 2 番目のテレポーターは町 1 と町 2 を結びます。
この時、次のようにして町 1 から町 3 へ 2 分で移動できます。
- 2 番目のテレポーターを使用し、町 1 から町 2 まで移動する。
- 1 番目のテレポーターを使用し、町 2 から町 3 まで移動する。
よって、-1,-1,2 をこの順に出力します。
結ぶ先が未定となっているテレポーターの結び先によっては、 同じ町同士を結ぶテレポーターが存在する可能性や、 ある 2 つの町を結ぶテレポーターが複数存在する可能性がある事に注意してください。
入力例 2
5 5 1 2 1 3 3 4 4 5 0 2
出力例 2
3 3 3 3 2
Score : 500 points
Problem Statement
There are N towns
numbered Town 1, Town 2, \ldots, Town N.
There are also M Teleporters, each of which connects two towns bidirectionally so that a person can travel from one to the other in one minute.
The i-th Teleporter connects Town U_i and Town V_i bidirectionally. However, for some of the Teleporters, one of the towns it connects is undetermined; U_i=0 means that one of the towns the i-th Teleporter connects is Town V_i, but the other end is undetermined.
For i=1,2,\ldots,N, answer the following question.
When the Teleporters with undetermined ends are all determined to be connected to Town i, how many minutes is required at minimum to travel from Town 1 to Town N? If it is impossible to travel from Towns 1 to N using Teleporters only, print -1 instead.
Constraints
- 2 \leq N \leq 3\times 10^5
- 1\leq M\leq 3\times 10^5
- 0\leq U_i<V_i\leq N
- If i \neq j, then (U_i,V_i)\neq (U_j,V_j).
- All values in input are integers.
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 N integers, with spaces in between. The k-th integer should be the answer to the question when i=k.
Sample Input 1
3 2 0 2 1 2
Sample Output 1
-1 -1 2
When the Teleporters with an undetermined end are all determined to be connected to Town 1,
the 1-st and the 2-nd Teleporters both connect Towns 1 and 2.
Then, it is impossible to travel from Town 1 to Town 3.
When the Teleporters with an undetermined end are all determined to be connected to Town 2,
the 1-st Teleporter connects Town 2 and itself, and the 2-nd one connects Towns 1 and 2.
Again, it is impossible to travel from Town 1 to Town 3.
When the Teleporters with an undetermined end are all determined to be connected to Town 3,
the 1-st Teleporter connects Town 3 and Town 2, and the 2-nd one connects Towns 1 and 2.
In this case, we can travel from Town 1 to Town 3 in two minutes.
- Use the 2-nd Teleporter to travel from Town 1 to Town 2.
- Use the 1-st Teleporter to travel from Town 2 to Town 3.
Therefore, -1,-1, and 2 should be printed in this order.
Note that, depending on which town the Teleporters with an undetermined end are connected to, there may be a Teleporter that connects a town and itself, or multiple Teleporters that connect the same pair of towns.
Sample Input 2
5 5 1 2 1 3 3 4 4 5 0 2
Sample Output 2
3 3 3 3 2