Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
文字列 atcoder の L 文字目から R 文字目までを出力してください。
制約
- L,R は整数
- 1 \le L \le R \le 7
入力
入力は以下の形式で標準入力から与えられる。
L R
出力
答えを出力せよ。
入力例 1
3 6
出力例 1
code
atcoder の 3 文字目から 6 文字目までを出力すると code となります。
入力例 2
4 4
出力例 2
o
入力例 3
1 7
出力例 3
atcoder
Score : 100 points
Problem Statement
Print the L-th through R-th characters of the string atcoder.
Constraints
- L and R are integers.
- 1 \le L \le R \le 7
Input
Input is given from Standard Input in the following format:
L R
Output
Print the answer.
Sample Input 1
3 6
Sample Output 1
code
The 3-rd through 6-th characters of atcoder are code.
Sample Input 2
4 4
Sample Output 2
o
Sample Input 3
1 7
Sample Output 3
atcoder
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 点
問題文
正の整数 X に対して、X を 2 進表記したときに 末尾 に連続する 0 の個数(の最大値)を \text{ctz}(X) で表します。
ただし、X を 2 進表記したとき末尾が 1 ならば \text{ctz}(X)=0 です。
正の整数 N が与えられるので、\text{ctz}(N) を出力してください。
制約
- 1\leq N\leq 10^9
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
\text{ctz}(N) を出力せよ。
入力例 1
2024
出力例 1
3
2024 を 2 進表記すると 11111101000 であり、末尾から 0 が 3 個連続しているため、\text{ctz}(2024)=3 です。
よって、3 を出力します。
入力例 2
18
出力例 2
1
18 を 2 進表記すると 10010 であるため、\text{ctz}(18)=1 です。
0 は末尾から連続する必要があることに注意してください。
入力例 3
5
出力例 3
0
Score: 200 points
Problem Statement
For a positive integer X, let \text{ctz}(X) be the (maximal) number of consecutive zeros at the end of the binary notation of X.
If the binary notation of X ends with a 1, then \text{ctz}(X)=0.
You are given a positive integer N. Print \text{ctz}(N).
Constraints
- 1\leq N\leq 10^9
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print \text{ctz}(N).
Sample Input 1
2024
Sample Output 1
3
2024 is 11111101000 in binary, with three consecutive 0s from the end, so \text{ctz}(2024)=3.
Thus, print 3.
Sample Input 2
18
Sample Output 2
1
18 is 10010 in binary, so \text{ctz}(18)=1.
Note that we count the trailing zeros.
Sample Input 3
5
Sample Output 3
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
長さ N の文字列 S が与えられます。
S に連続して含まれる na を全て nya に置き換えて得られる文字列を答えてください。
制約
- N は 1 以上 1000 以下の整数
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
4 naan
出力例 1
nyaan
naan に連続して含まれる na を全て nya に置き換えて得られる文字列は nyaan です。
入力例 2
4 near
出力例 2
near
S に na が連続して含まれないこともあります。
入力例 3
8 national
出力例 3
nyationyal
Score : 200 points
Problem Statement
You are given a string S of length N.
Find the string obtained by replacing all contiguous occurrences of na in S with nya.
Constraints
- N is an integer between 1 and 1000, inclusive.
- 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
4 naan
Sample Output 1
nyaan
Replacing all contiguous occurrences of na in naan with nya results in the string nyaan.
Sample Input 2
4 near
Sample Output 2
near
S may not contain a contiguous na.
Sample Input 3
8 national
Sample Output 3
nyationyal
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英小文字のみからなる長さ N の文字列 S が与えられます。
S の文字を並び替えて得られる文字列(S 自身を含む)であって、長さ K の回文を部分文字列として 含まない ものの個数を求めてください。
ただし、長さ N の文字列 T が「長さ K の回文を部分文字列として含む」とは、
ある (N-K) 以下の非負整数 i が存在して、1 以上 K 以下の任意の整数 j について T_{i+j}=T_{i+K+1-j} が成り立つことをいいます。
ここで、T_k は文字列 T の k 文字目を表すものとします。
制約
- 2\leq K \leq N \leq 10
- N,K は整数
- S は英小文字のみからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
S の文字を並び替えて得られる文字列であって、長さ K の回文を部分文字列として含まないものの個数を出力せよ。
入力例 1
3 2 aab
出力例 1
1
aab を並び替えて得られる文字列は aab, aba, baa の 3 つであり、このうち aab および baa は長さ 2 の回文 aa を部分文字列として含んでいます。
よって、条件をみたす文字列は aba のみであり、1 を出力します。
入力例 2
5 3 zzyyx
出力例 2
16
zzyyx を並べて得られる文字列は 30 個ありますが、そのうち長さ 3 の回文を含まないようなものは 16 個です。よって、16 を出力します。
入力例 3
10 5 abcwxyzyxw
出力例 3
440640
Score : 300 points
Problem Statement
You are given a string S of length N consisting only of lowercase English letters.
Find the number of strings obtained by permuting the characters of S (including the string S itself) that do not contain a palindrome of length K as a substring.
Here, a string T of length N is said to "contain a palindrome of length K as a substring" if and only if there exists a non-negative integer i not greater than (N-K) such that T_{i+j} = T_{i+K+1-j} for every integer j with 1 \leq j \leq K.
Here, T_k denotes the k-th character of the string T.
Constraints
- 2 \leq K \leq N \leq 10
- N and K are integers.
- S is a string of length N consisting only of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N K S
Output
Print the number of strings obtained by permuting S that do not contain a palindrome of length K as a substring.
Sample Input 1
3 2 aab
Sample Output 1
1
The strings obtained by permuting aab are aab, aba, and baa. Among these, aab and baa contain the palindrome aa of length 2 as a substring.
Thus, the only string that satisfies the condition is aba, so print 1.
Sample Input 2
5 3 zzyyx
Sample Output 2
16
There are 30 strings obtained by permuting zzyyx, 16 of which do not contain a palindrome of length 3. Thus, print 16.
Sample Input 3
10 5 abcwxyzyxw
Sample Output 3
440640
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
N 種類の薬品 1,2,\dots,N があります。あなたの目標はこれらが全て混ざった状態にすることです。
0, 1 からなる長さ 2^N-1 の文字列 S が与えられます。この文字列は次の情報を表します。
- まず、 1 種類以上の薬品が混ざった状態 i ( 1 \le i \le 2^N-1 ) を次のように定義する。
- i を 2 進法で表記した際に下から k ( 1 \le k \le N ) 桁目が 1 である、またその時に限り、薬品 k が含まれている。
- 例えば、 13 を 2 進法で表記すると 1101_{(2)} となるため、状態 13 は薬品 1,3,4 が混ざった状態を表現します。
- S の i 文字目が
0であるとき、状態 i は 安全 である。 - S の i 文字目が
1であるとき、状態 i は 危険 である。
あなたは次の操作を使って薬品を混ぜ合わせます。
- まず、空の瓶を用意する。
- 次に、以下を繰り返す。
- まだ瓶に注いでいない薬品を 1 種類選択し、瓶に注ぐ。
- この時、瓶の中で混ざった薬品が危険な状態であってはならない。
この操作によって全ての薬品が混ざった状態を作れるか判定してください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- T は 1 以上 40000 以下の整数
- N は 1 以上 18 以下の整数
- S は
0,1からなる長さ 2^N-1 の文字列 - ひとつの入力に含まれる |S| = 2^N-1 の総和は 5 \times 10^5 を超えない
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i は i 番目のテストケースを表す。 各テストケースは以下の形式で与えられる。
N S
出力
T 行出力せよ。i 行目 には i 番目のテストケースに対する答えを出力せよ。
各テストケースについて、全ての薬品が混ざった状態を作れるなら Yes 、作れないなら No と出力せよ。
入力例 1
5 3 0010000 3 0010110 1 1 2 100 4 001110010101110
出力例 1
Yes No No Yes Yes
この入力には 5 個のテストケースが含まれます。
1 番目のケースは次の通りです。
- 3 種類の薬品が存在する。
- 薬品 1,2 が混ざった状態 3 のみが危険で、他の状態は安全である。
- 例えば、以下の手順で全ての薬品が混ざった状態を作ることができます。
- はじめに、瓶に薬品 2 を注ぐ。瓶の中で薬品 2 のみが混ざっており、これは状態 2 なので安全である。
- 次に、瓶に薬品 3 を注ぐ。瓶の中で薬品 2,3 が混ざっており、これは状態 6 なので安全である。
- 最後に、瓶に薬品 1 を注ぐ。瓶の中で薬品 1,2,3 が混ざっており、これは状態 7 なので安全である。
2 番目のケースは次の通りです。
- 3 種類の薬品が存在する。
- 薬品 1,2 が混ざった状態 3 、薬品 1,3 が混ざった状態 5 、薬品 2,3 が混ざった状態 6 が危険で、他の状態は安全である。
- このケースについて、全ての薬品が混ざった状態を作ることはできません。
3 番目のケースは次の通りです。
- 1 種類の薬品が存在する。
- 薬品 1 のみが混ざった状態 1 が危険であるため、全ての薬品が混ざった状態を作ることはできません。
Score : 350 points
Problem Statement
There are N types of chemicals 1,2,\dots,N. Your goal is to create a state where all of them are mixed.
You are given a string S of length 2^N-1 consisting of 0 and 1, which represents the following information:
- First, define state i (1 \le i \le 2^N-1) where one or more types of chemicals are mixed as follows:
- When the k-th digit (1 \le k \le N) from the bottom in the binary representation of i is 1, and only then, chemical k is included.
- For example, 13 in binary is 1101_{(2)}, so state 13 represents a state where chemicals 1,3,4 are mixed.
- When the i-th character of S is
0, state i is safe. - When the i-th character of S is
1, state i is dangerous.
You mix chemicals using the following operation:
- First, prepare an empty bottle.
- Next, repeat the following:
- Choose one type of chemical that has not yet been poured into the bottle and pour it into the bottle.
- At this time, the chemicals mixed in the bottle must not be in a dangerous state.
Determine whether this operation can create a state where all chemicals are mixed.
You are given T test cases; solve each of them.
Constraints
- T is an integer between 1 and 40000, inclusive.
- N is an integer between 1 and 18, inclusive.
- S is a string of length 2^N-1 consisting of
0and1. - The sum of |S| = 2^N-1 in each input does not exceed 5 \times 10^5.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i represents the i-th test case. Each test case is given in the following format:
N S
Output
Output T lines. The i-th line should contain the answer for the i-th test case.
For each test case, if it is possible to create a state where all chemicals are mixed, print Yes; otherwise, print No.
Sample Input 1
5 3 0010000 3 0010110 1 1 2 100 4 001110010101110
Sample Output 1
Yes No No Yes Yes
This input contains five test cases.
The 1st case is as follows:
- There are three types of chemicals.
- Only state 3 where chemicals 1,2 are mixed is dangerous, and the other states are safe.
- For example, you can create a state where all chemicals are mixed with the following procedure:
- First, pour chemical 2 into the bottle. Only chemical 2 is mixed in the bottle, which is state 2, so it is safe.
- Next, pour chemical 3 into the bottle. Chemicals 2,3 are mixed in the bottle, which is state 6, so it is safe.
- Finally, pour chemical 1 into the bottle. Chemicals 1,2,3 are mixed in the bottle, which is state 7, so it is safe.
The 2nd case is as follows:
- There are three types of chemicals.
- State 3 where chemicals 1,2 are mixed, state 5 where chemicals 1,3 are mixed, and state 6 where chemicals 2,3 are mixed are dangerous, and the other states are safe.
- For this case, it is impossible to create a state where all chemicals are mixed.
The 3rd case is as follows:
- There is one type of chemical.
- Since state 1 where only chemical 1 is mixed is dangerous, it is impossible to create a state where all chemicals are mixed.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
頂点に 1 から N の、辺に 1 から M の番号がついた N 頂点 M 辺の単純連結無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を結んでおり、ラベル w_i が付けられています。
頂点 1 から頂点 N への単純パス(同じ頂点を 2 度以上通らないパス)のうち、パスに含まれる辺につけられたラベルの総 XOR としてあり得る最小値を求めてください。
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 の順番によらないことが証明できます。
制約
- 2\leq N\leq 10
- N-1\leq M\leq \frac{N(N-1)}{2}
- 1\leq u_i\lt v_i\leq N
- 0\leq w_i\lt 2^{60}
- 入力で与えられるグラフは単純連結無向グラフ
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 w_1 u_2 v_2 w_2 \vdots u_M v_M w_M
出力
答えを出力せよ。
入力例 1
4 4 1 2 3 2 4 5 1 3 4 3 4 7
出力例 1
3
頂点 1 から頂点 4 への単純パスは以下の 2 つ存在します。
- 頂点 1 \to 頂点 2 \to 頂点 4
- 頂点 1 \to 頂点 3 \to 頂点 4
1 つ目のパスに含まれる辺につけられたラベルの総 XOR は 6、 2 つ目のパスに含まれる辺につけられたラベルの総 XOR は 3 であるため、答えは 3 です。
入力例 2
4 3 1 2 1 2 3 2 3 4 4
出力例 2
7
入力例 3
7 10 1 2 726259430069220777 1 4 988687862609183408 1 5 298079271598409137 1 6 920499328385871537 1 7 763940148194103497 2 4 382710956291350101 3 4 770341659133285654 3 5 422036395078103425 3 6 472678770470637382 5 7 938201660808593198
出力例 3
186751192333709144
Score : 400 points
Problem Statement
You are given a simple connected undirected graph with N vertices numbered 1 through N and M edges numbered 1 through M. Edge i connects vertices u_i and v_i, and has a label w_i.
Among all simple paths (paths that do not pass through the same vertex more than once) from vertex 1 to vertex N, find the minimum XOR of the labels of the edges on the path.
Notes on XOR
For non-negative integers A and B, their XOR A \oplus B is defined as follows:- In the binary representation of A \oplus B, the digit in the place corresponding to 2^k \,(k \ge 0) is 1 if and only if exactly one of the digits in the same place of A and B is 1; otherwise, it is 0.
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
- 2 \leq N \leq 10
- N-1 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq u_i < v_i \leq N
- 0 \leq w_i < 2^{60}
- The given graph is a simple connected undirected graph.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 w_1 u_2 v_2 w_2 \vdots u_M v_M w_M
Output
Print the answer.
Sample Input 1
4 4 1 2 3 2 4 5 1 3 4 3 4 7
Sample Output 1
3
There are two simple paths from vertex 1 to vertex 4:
- 1 \to 2 \to 4
- 1 \to 3 \to 4
The XOR of the labels on the edges of the first path is 6, and that of the second path is 3. Therefore, the answer is 3.
Sample Input 2
4 3 1 2 1 2 3 2 3 4 4
Sample Output 2
7
Sample Input 3
7 10 1 2 726259430069220777 1 4 988687862609183408 1 5 298079271598409137 1 6 920499328385871537 1 7 763940148194103497 2 4 382710956291350101 3 4 770341659133285654 3 5 422036395078103425 3 6 472678770470637382 5 7 938201660808593198
Sample Output 3
186751192333709144
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の単純な無向グラフが与えられます。
辺 i は、頂点 A_i と B_i を結んでいます。
頂点 1,2,\ldots,N を順番に消していきます。
なお、頂点 i を消すとは、頂点 i と、頂点 i に接続する全ての辺をグラフから削除することです。
i=1,2,\ldots,N について、頂点 i まで消した時にグラフはいくつの連結成分に分かれていますか?
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min(\frac{N(N-1)}{2} , 2 \times 10^5 )
- 1 \leq A_i \lt B_i \leq N
- i \neq j ならば (A_i,B_i) \neq (A_j,B_j)
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
出力
N 行出力せよ。
i 行目には、頂点 i まで消した時のグラフの連結成分の数を出力せよ。
入力例 1
6 7 1 2 1 4 1 5 2 4 2 3 3 5 3 6
出力例 1
1 2 3 2 1 0

グラフは上図のように変化していきます。
入力例 2
8 7 7 8 3 4 5 6 5 7 5 8 6 7 6 8
出力例 2
3 2 2 1 1 1 1 0
はじめからグラフが非連結なこともあります。
Score : 500 points
Problem Statement
Given is an undirected graph with N vertices and M edges.
Edge i connects Vertices A_i and B_i.
We will erase Vertices 1, 2, \ldots, N one by one.
Here, erasing Vertex i means deleting Vertex i and all edges incident to Vertex i from the graph.
For each i=1, 2, \ldots, N, how many connected components does the graph have when vertices up to Vertex i are deleted?
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min(\frac{N(N-1)}{2} , 2 \times 10^5 )
- 1 \leq A_i \lt B_i \leq N
- (A_i,B_i) \neq (A_j,B_j) if i \neq j.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
Output
Print N lines.
The i-th line should contain the number of connected components in the graph when vertices up to Vertex i are deleted.
Sample Input 1
6 7 1 2 1 4 1 5 2 4 2 3 3 5 3 6
Sample Output 1
1 2 3 2 1 0

The figure above shows the transition of the graph.
Sample Input 2
8 7 7 8 3 4 5 6 5 7 5 8 6 7 6 8
Sample Output 2
3 2 2 1 1 1 1 0
The graph may be disconnected from the beginning.
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
頂点に 1 から N の番号がついた N 頂点 N 辺の連結な単純無向グラフ G が与えられます。i 番目の辺は頂点 u_i と頂点 v_i を双方向に結んでいます。
以下の Q 個のクエリに答えてください。
- 頂点 x_i から頂点 y_i に向かう単純パス(同じ頂点を 2 度通らないパス)が一意に定まるか判定せよ。
制約
- 3 \leq N \leq 2 \times 10^5
- 1 \leq u_i < v_i\leq N
- i \neq j ならば (u_i,v_i) \neq (u_j,v_j)
- G は N 頂点 N 辺の連結な単純無向グラフ
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq x_i < y_i\leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N u_1 v_1 u_2 v_2 \vdots u_N v_N Q x_1 y_1 x_2 y_2 \vdots x_Q y_Q
出力
Q 行出力せよ。
i\ (1 \leq i \leq Q) 行目には、頂点 x_i から頂点 y_i に向かう単純パスが一意に定まる場合 Yes、そうでない場合 No を出力せよ。
入力例 1
5 1 2 2 3 1 3 1 4 2 5 3 1 2 1 4 1 5
出力例 1
No Yes No
頂点 1 から 2 に向かう単純パスは (1,2),(1,3,2) と一意に定まらないので、 1 個目のクエリの答えは No です。
頂点 1 から 4 に向かう単純パスは (1,4) と一意に定まるので、2 個目のクエリの答えは Yes です。
頂点 1 から 5 に向かう単純パスは (1,2,5),(1,3,2,5) と一意に定まらないので、3 個目のクエリの答えは No です。
入力例 2
10 3 5 5 7 4 8 2 9 1 2 7 9 1 6 4 10 2 5 2 10 10 1 8 6 9 8 10 6 8 3 10 3 9 1 10 5 8 1 10 7 8
出力例 2
Yes No Yes Yes No No Yes No Yes No
Score : 500 points
Problem Statement
You are given a connected simple undirected graph G with N vertices numbered 1 to N and N edges. The i-th edge connects Vertex u_i and Vertex v_i bidirectionally.
Answer the following Q queries.
- Determine whether there is a unique simple path from Vertex x_i to Vertex y_i (a simple path is a path without repetition of vertices).
Constraints
- 3 \leq N \leq 2 \times 10^5
- 1 \leq u_i < v_i\leq N
- (u_i,v_i) \neq (u_j,v_j) if i \neq j.
- G is a connected simple undirected graph with N vertices and N edges.
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq x_i < y_i\leq N
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N u_1 v_1 u_2 v_2 \vdots u_N v_N Q x_1 y_1 x_2 y_2 \vdots x_Q y_Q
Output
Print Q lines.
The i-th line (1 \leq i \leq Q) should contain Yes if there is a unique simple path from Vertex x_i to Vertex y_i, and No otherwise.
Sample Input 1
5 1 2 2 3 1 3 1 4 2 5 3 1 2 1 4 1 5
Sample Output 1
No Yes No
The simple paths from Vertex 1 to 2 are (1,2) and (1,3,2), which are not unique, so the answer to the first query is No.
The simple path from Vertex 1 to 4 is (1,4), which is unique, so the answer to the second query is Yes.
The simple paths from Vertex 1 to 5 are (1,2,5) and (1,3,2,5), which are not unique, so the answer to the third query is No.
Sample Input 2
10 3 5 5 7 4 8 2 9 1 2 7 9 1 6 4 10 2 5 2 10 10 1 8 6 9 8 10 6 8 3 10 3 9 1 10 5 8 1 10 7 8
Sample Output 2
Yes No Yes Yes No No Yes No Yes No