A - Five Integers

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

与えられる 5 つの整数 A, B, C, D, E の中に何種類の整数があるかを出力してください。

制約

  • 0 \leq A, B, C, D, E \leq 100
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

A B C D E

出力

答えを出力せよ。


入力例 1

31 9 24 31 24

出力例 1

3

与えられる 5 つの整数 31, 9, 24, 31, 24 の中には、9, 24, 31 という 3 種類の整数があります。 よって、3 を出力します。


入力例 2

0 0 0 0 0

出力例 2

1

Score : 100 points

Problem Statement

Print how many distinct integers there are in given five integers A, B, C, D, and E.

Constraints

  • 0 \leq A, B, C, D, E \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D E

Output

Print the answer.


Sample Input 1

31 9 24 31 24

Sample Output 1

3

In the given five integers 31, 9, 24, 31, and 24, there are three distinct integers 9, 24, and 31. Thus, 3 should be printed.


Sample Input 2

0 0 0 0 0

Sample Output 2

1
B - Cyclic

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

各桁が 1 以上 9 以下の整数である 3 桁の整数 N が与えられます。

N100 の位を a10 の位を b1 の位を c としたとき、b,c,a をこの順に並べた整数と c,a,b をこの順に並べた整数をそれぞれ出力してください。

制約

  • N は各桁が 1 以上 9 以下の整数である 3 桁の整数

入力

入力は以下の形式で標準入力から与えられる。

N

出力

b,c,a をこの順に並べた整数と c,a,b をこの順に並べた整数をこの順で空白区切りで出力せよ。


入力例 1

379

出力例 1

793 937

379100 の位は 310 の位は 71 の位は 9 です。よって、793937 をそれぞれ出力します。


入力例 2

919

出力例 2

199 991

919100 の位は 910 の位は 11 の位は 9 です。よって、199991 をそれぞれ出力します。

Score : 100 points

Problem Statement

You are given a three-digit integer N where each digit is an integer between 1 and 9, inclusive.

Let a, b, c be the hundreds, tens, ones digits of N, respectively. Print an integer formed by arranging b, c, a in this order, and an integer formed by arranging c, a, b in this order.

Constraints

  • N is a three-digit integer where each digit is an integer between 1 and 9, inclusive.

Input

The input is given from Standard Input in the following format:

N

Output

Print two integers separated by a space in the following order: an integer formed by arranging b, c, a in this order, and an integer formed by arranging c, a, b in this order.


Sample Input 1

379

Sample Output 1

793 937

The hundreds, tens, ones digits of 379 are 3, 7, 9, respectively, so print 793 and 937.


Sample Input 2

919

Sample Output 2

199 991

The hundreds, tens, ones digits of 919 are 9, 1, 9, respectively, so print 199 and 991.

C - 3-smooth Numbers

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

正の整数 N が与えられます。 N=2^x3^y を満たす整数 x,y が存在するなら Yes 、そうでなければ No と出力してください。

制約

  • 1\leq N\leq10^{18}
  • N は整数

入力

入力は以下の形式で標準入力から与えられる。

N

出力

条件を満たす整数 x,y が存在するなら Yes 、そうでなければ No1 行に出力せよ。


入力例 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
D - Find snuke

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 250

問題文

H マス \timesW マスのマス目があり、各マスに 1 つずつ英小文字が書き込まれています。 上から i 行目かつ左から j 列目のマスを (i,j) で表します。

マス目に書き込まれている英小文字は H 個の長さ W の文字列 S_1,S_2,\ldots, S_H によって与えられ、 S_ij 文字目が、(i, j) に書き込まれた英小文字を表します。

マス目の中に、s, n, u, k, e この順に(縦・横・ななめのいずれかの方向に) 連続して並んでいる 場所がただ 1 つ存在します。
そのような場所を見つけ、そのマスの位置を出力の形式に従って出力してください。

ただし、s, n, u, k, e この順に(縦・横・ななめのいずれかの方向に) 連続して並んでいる場所とは、 5 つのマスの組 (A_1,A_2,A_3,A_4,A_5) であって、次をすべてみたすものをさします。

  • A_1,A_2,A_3,A_4,A_5 に書き込まれた英小文字はそれぞれ s, n, u, k, e である。
  • 1\leq i\leq 4 について、A_iA_{i+1} は頂点または辺を共有している。
  • A_1,A_2,A_3,A_4,A_5 の中心はこの順に一直線上に等間隔で並んでいる。

制約

  • 5\leq H\leq 100
  • 5\leq W\leq 100
  • H,W は整数
  • S_i は英小文字のみからなる長さ W の文字列
  • 与えられるマス目の中に条件をみたす場所がただ 1 つ存在する

入力

入力は以下の形式で標準入力から与えられる。

H W
S_1
S_2
\vdots
S_H

出力

次の形式にしたがって、5 行出力せよ。

条件をみたす場所のうち s, n, u, k, e が書かれたマスがそれぞれ (R_1,C_1), (R_2,C_2)\ldots,(R_5,C_5) であるとき、 i 行目には R_iC_i をこの順に空白区切りで出力せよ。

すなわち、以下のように出力せよ。

R_1 C_1
R_2 C_2
\vdots
R_5 C_5

以下の入力例も参考にせよ。


入力例 1

6 6
vgxgpu
amkxks
zhkbpp
hykink
esnuke
zplvfj

出力例 1

5 2
5 3
5 4
5 5
5 6

この時、(A_1,A_2,A_3,A_4,A_5)=((5,2),(5,3),(5,4),(5,5),(5,6)) とすると、
それぞれのマスに書き込まれた英小文字は s, n, u, k, e であり、
1\leq i\leq 4 について、A_iA_{i+1} は辺を共有しており、
各マスの中心は一直線上に存在するため、条件をみたしています。


入力例 2

5 5
ezzzz
zkzzz
ezuzs
zzznz
zzzzs

出力例 2

5 5
4 4
3 3
2 2
1 1

(A_1,A_2,A_3,A_4,A_5)=((5,5),(4,4),(3,3),(2,2),(1,1)) が条件をみたしています。
例えば、(A_1,A_2,A_3,A_4,A_5)=((3,5),(4,4),(3,3),(2,2),(3,1)) は、1,2 つめの条件をみたしていますが、
マスの中心が一直線上に存在しないため、3 つめの条件をみたしていません。


入力例 3

10 10
kseeusenuk
usesenesnn
kskekeeses
nesnusnkkn
snenuuenke
kukknkeuss
neunnennue
sknuessuku
nksneekknk
neeeuknenk

出力例 3

9 3
8 3
7 3
6 3
5 3

Score : 250 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns. Each cell has a lowercase English letter written on it. We denote by (i, j) the cell at the i-th row from the top and j-th column from the left.

The letters written on the grid are represented by H strings S_1,S_2,\ldots, S_H, each of length W. The j-th letter of S_i represents the letter written on (i, j).

There is a unique set of contiguous cells (going vertically, horizontally, or diagonally) in the grid with s, n, u, k, and e written on them in this order.
Find the positions of such cells and print them in the format specified in the Output section.

A tuple of five cells (A_1,A_2,A_3,A_4,A_5) is said to form a set of contiguous cells (going vertically, horizontally, or diagonally) with s, n, u, k, and e written on them in this order if and only if all of the following conditions are satisfied.

  • A_1,A_2,A_3,A_4 and A_5 have letters s, n, u, k, and e written on them, respectively.
  • For all 1\leq i\leq 4, cells A_i and A_{i+1} share a corner or a side.
  • The centers of A_1,A_2,A_3,A_4, and A_5 are on a common line at regular intervals.

Constraints

  • 5\leq H\leq 100
  • 5\leq W\leq 100
  • H and W are integers.
  • S_i is a string of length W consisting of lowercase English letters.
  • The given grid has a unique conforming set of cells.

Input

The input is given from Standard Input in the following format:

H W
S_1
S_2
\vdots
S_H

Output

Print five lines in the following format.

Let (R_1,C_1), (R_2,C_2)\ldots,(R_5,C_5) be the cells in the sought set with s, n, u, k, and e written on them, respectively. The i-th line should contain R_i and C_i in this order, separated by a space.

In other words, print them in the following format:

R_1 C_1
R_2 C_2
\vdots
R_5 C_5

See also Sample Inputs and Outputs below.


Sample Input 1

6 6
vgxgpu
amkxks
zhkbpp
hykink
esnuke
zplvfj

Sample Output 1

5 2
5 3
5 4
5 5
5 6

Tuple (A_1,A_2,A_3,A_4,A_5)=((5,2),(5,3),(5,4),(5,5),(5,6)) satisfies the conditions.
Indeed, the letters written on them are s, n, u, k, and e;
for all 1\leq i\leq 4, cells A_i and A_{i+1} share a side;
and the centers of the cells are on a common line.


Sample Input 2

5 5
ezzzz
zkzzz
ezuzs
zzznz
zzzzs

Sample Output 2

5 5
4 4
3 3
2 2
1 1

Tuple (A_1,A_2,A_3,A_4,A_5)=((5,5),(4,4),(3,3),(2,2),(1,1)) satisfies the conditions.
However, for example, (A_1,A_2,A_3,A_4,A_5)=((3,5),(4,4),(3,3),(2,2),(3,1)) violates the third condition because the centers of the cells are not on a common line, although it satisfies the first and second conditions.


Sample Input 3

10 10
kseeusenuk
usesenesnn
kskekeeses
nesnusnkkn
snenuuenke
kukknkeuss
neunnennue
sknuessuku
nksneekknk
neeeuknenk

Sample Output 3

9 3
8 3
7 3
6 3
5 3
E - Count Connected Components

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の単純無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を結んでいます。
グラフに含まれる連結成分の個数を求めてください。

注釈

単純無向グラフ とは、単純で辺に向きの無いグラフのことをいいます。
グラフが 単純 であるとは、グラフが自己ループや多重辺を含まないことをいいます。

あるグラフの 部分グラフ とは、元のグラフのいくつかの頂点といくつかの辺を選んでできるグラフのことをいいます。
グラフが 連結 であるとは、グラフに含まれるすべての頂点同士が辺を経由して互いに行き来できることをいいます。
連結成分 とは、連結な部分グラフのうち、そのグラフを含んだより大きい連結な部分グラフが存在しないものをいいます。

制約

  • 1 \leq N \leq 100
  • 0 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq u_i, v_i \leq N
  • 入力で与えられるグラフは単純
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

答えを出力せよ。


入力例 1

5 3
1 2
1 3
4 5

出力例 1

2

与えられるグラフに含まれる連結成分は次の 2 個です。

  • 頂点 1, 2, 3 および辺 1, 2 からなる部分グラフ
  • 頂点 4, 5 および辺 3 からなる部分グラフ

image


入力例 2

5 0

出力例 2

5

入力例 3

4 6
1 2
1 3
1 4
2 3
2 4
3 4

出力例 3

1

Score : 300 points

Problem Statement

You are given a simple undirected graph with N vertices numbered 1 to N and M edges numbered 1 to M. Edge i connects vertex u_i and vertex v_i.
Find the number of connected components in this graph.

Notes

A simple undirected graph is a graph that is simple and has undirected edges.
A graph is simple if and only if it has no self-loop or multi-edge.

A subgraph of a graph is a graph formed from some of the vertices and edges of that graph.
A graph is connected if and only if one can travel between every pair of vertices via edges.
A connected component is a connected subgraph that is not part of any larger connected subgraph.

Constraints

  • 1 \leq N \leq 100
  • 0 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq u_i, v_i \leq N
  • The given graph is simple.
  • 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

Output

Print the answer.


Sample Input 1

5 3
1 2
1 3
4 5

Sample Output 1

2

The given graph contains the following two connected components:

  • a subgraph formed from vertices 1, 2, 3, and edges 1, 2;
  • a subgraph formed from vertices 4, 5, and edge 3.

image


Sample Input 2

5 0

Sample Output 2

5

Sample Input 3

4 6
1 2
1 3
1 4
2 3
2 4
3 4

Sample Output 3

1
F - ±1 Operation 1

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

整数 X が与えられます。この X に以下を施すことを「操作」と呼びます。

  • 以下の 2 つのうちどちらかを選択し、実行する。
    • X1 を加算する。
    • X から 1 を減算する。

初項 A 、公差 D 、項数 N の等差数列 S に含まれる数を「良い数」と呼びます。
「操作」を 0 回以上何度でも使って X を「良い数」にする時、必要な「操作」の最小回数を求めてください。

制約

  • 入力は全て整数
  • -10^{18} \le X,A \le 10^{18}
  • -10^6 \le D \le 10^6
  • 1 \le N \le 10^{12}

入力

入力は以下の形式で標準入力から与えられる。

X A D N

出力

答えを整数として出力せよ。


入力例 1

6 2 3 3

出力例 1

1

A=2,D=3,N=3 であるため、 S=(2,5,8) です。
X=6 を「良い数」にするためには、 X から 1 を減算することを 1 度行えば良いです。
0 回の操作で X を「良い数」にすることはできません。


入力例 2

0 0 0 1

出力例 2

0

D=0 である場合もあります。また、操作を 1 回も必要としない場合もあります。


入力例 3

998244353 -10 -20 30

出力例 3

998244363

入力例 4

-555555555555555555 -1000000000000000000 1000000 1000000000000

出力例 4

444445

Score : 300 points

Problem Statement

You are given an integer X. The following action on this integer is called an operation.

  • Choose and do one of the following.
    • Add 1 to X.
    • Subtract 1 from X.

The terms in the arithmetic progression S with N terms whose initial term is A and whose common difference is D are called good numbers.
Consider performing zero or more operations to make X a good number. Find the minimum number of operations required to do so.

Constraints

  • All values in input are integers.
  • -10^{18} \le X,A \le 10^{18}
  • -10^6 \le D \le 10^6
  • 1 \le N \le 10^{12}

Input

Input is given from Standard Input in the following format:

X A D N

Output

Print the answer as an integer.


Sample Input 1

6 2 3 3

Sample Output 1

1

Since A=2,D=3,N=3, we have S=(2,5,8).
You can subtract 1 from X once to make X=6 a good number.
It is impossible to make X good in zero operations.


Sample Input 2

0 0 0 1

Sample Output 2

0

We might have D=0. Additionally, no operation might be required.


Sample Input 3

998244353 -10 -20 30

Sample Output 3

998244363

Sample Input 4

-555555555555555555 -1000000000000000000 1000000 1000000000000

Sample Output 4

444445
G - Robot Arms 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

長さ N の正整数列 A = (A_1, A_2, \dots, A_N) および整数 x, y が与えられます。
次の条件をすべて満たすように、xy 座標平面上に N+1 個の点 p_1, p_2, \dots, p_N, p_{N+1} を配置することができるか判定してください。(同じ座標に 2 個以上の点を配置してもよいです。)

  • p_1 = (0, 0)
  • p_2 = (A_1, 0)
  • p_{N+1} = (x, y)
  • p_i と点 p_{i+1} の距離は A_i (1 \leq i \leq N)
  • 線分 p_i p_{i+1} と線分 p_{i+1} p_{i+2} のなす角は 90 度 (1 \leq i \leq N - 1)

制約

  • 2 \leq N \leq 10^3
  • 1 \leq A_i \leq 10
  • |x|, |y| \leq 10^4
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N x y
A_1 A_2 \dots A_N

出力

問題文の条件をすべて満たすように p_1, p_2, \dots, p_N, p_{N+1} を配置することができる場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

3 -1 1
2 1 3

出力例 1

Yes

xy 座標平面に p_1 = (0, 0), p_2 = (2, 0), p_3 = (2, 1), p_4 = (-1, 1) として点を配置したのが以下の図です。これは問題文の条件をすべて満たしています。


入力例 2

5 2 0
2 2 2 2 2

出力例 2

Yes

p_1 = (0, 0), p_2 = (2, 0), p_3 = (2, 2), p_4 = (0, 2), p_5 = (0, 0), p_6 = (2, 0) とすれば問題文の条件をすべて満たすことができます。同じ座標に複数の点を置いてもよいのに注意してください。


入力例 3

4 5 5
1 2 3 4

出力例 3

No

入力例 4

3 2 7
2 7 4

出力例 4

No

入力例 5

10 8 -7
6 10 4 1 5 9 8 6 5 1

出力例 5

Yes

Score : 400 points

Problem Statement

You are given a sequence A = (A_1, A_2, \dots, A_N) of length N consisting of positive integers, and integers x and y.
Determine whether it is possible to place N+1 points p_1, p_2, \dots, p_N, p_{N+1} in the xy-coordinate plane to satisfy all of the following conditions. (It is allowed to place two or more points at the same coordinates.)

  • p_1 = (0, 0).
  • p_2 = (A_1, 0).
  • p_{N+1} = (x, y).
  • The distance between the points p_i and p_{i+1} is A_i. (1 \leq i \leq N)
  • The segments p_i p_{i+1} and p_{i+1} p_{i+2} form a 90 degree angle. (1 \leq i \leq N - 1)

Constraints

  • 2 \leq N \leq 10^3
  • 1 \leq A_i \leq 10
  • |x|, |y| \leq 10^4
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N x y
A_1 A_2 \dots A_N

Output

If it is possible to place p_1, p_2, \dots, p_N, p_{N+1} to satisfy all of the conditions in the Problem Statement, print Yes; otherwise, print No.


Sample Input 1

3 -1 1
2 1 3

Sample Output 1

Yes

The figure below shows a placement where p_1 = (0, 0), p_2 = (2, 0), p_3 = (2, 1), and p_4 = (-1, 1). All conditions in the Problem Statement are satisfied.


Sample Input 2

5 2 0
2 2 2 2 2

Sample Output 2

Yes

Letting p_1 = (0, 0), p_2 = (2, 0), p_3 = (2, 2), p_4 = (0, 2), p_5 = (0, 0), and p_6 = (2, 0) satisfies all the conditions. Note that multiple points may be placed at the same coordinates.


Sample Input 3

4 5 5
1 2 3 4

Sample Output 3

No

Sample Input 4

3 2 7
2 7 4

Sample Output 4

No

Sample Input 5

10 8 -7
6 10 4 1 5 9 8 6 5 1

Sample Output 5

Yes
H - Bus Stops

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 450

問題文

高橋君ははじめ高橋君の家におり、これから青木君の家に遊びに行きます。

2 人の家の間には 1 から N までの番号がつけられた N 個のバス停があり、高橋君はそれらの間を下記の方法で移動できます。

  • 高橋君の家からバス停 1 まで X だけの時間をかけて徒歩で移動できます。
  • i = 1, 2, \ldots, N-1 について、バス停 i からは P_i の倍数である時刻それぞれにバスが出発し、そのバスに乗ることで T_i だけの時間をかけてバス停 (i+1) に移動できます。ここで、1 \leq P_i \leq 8 が制約として保証されます。
  • バス停 N から青木君の家まで、Y だけの時間をかけて徒歩で移動できます。

i = 1, 2, \ldots, Q に対して下記のクエリを処理してください。

高橋君が高橋君の家を時刻 q_i に出発するときの、高橋君が青木君の家に到着する時刻としてあり得る最も早いものを求めよ。

なお、バスの出発時刻ちょうどにそのバスが出発するバス停に到着した場合であっても、そのバスに乗ることができます。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq X, Y \leq 10^9
  • 1 \leq P_i \leq 8
  • 1 \leq T_i \leq 10^9
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq q_i \leq 10^9
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N X Y
P_1 T_1
P_2 T_2
\vdots
P_{N-1} T_{N-1}
Q
q_1
q_2
\vdots
q_Q

出力

Q 行出力せよ。 i = 1, 2, \ldots, Q について、i 行目には i 番目のクエリに対する答えを出力せよ。


入力例 1

4 2 3
5 4
6 6
3 1
7
13
0
710511029
136397527
763027379
644706927
447672230

出力例 1

34
22
710511052
136397548
763027402
644706946
447672250

1 番目のクエリについて、高橋君は下記の通りに移動を行って、時刻 34 に青木君の家に到着することができます。

  • 時刻 13 に高橋君の家を出発する。
  • 高橋君の家から徒歩で移動し、時刻 15 にバス停 1 に到着する。
  • 時刻 15 にバス停 1 を出発するバスに乗り、時刻 19 にバス停 2 に到着する。
  • 時刻 24 にバス停 2 を出発するバスに乗り、時刻 30 にバス停 3 に到着する。
  • 時刻 30 にバス停 3 を出発するバスに乗り、時刻 31 にバス停 4 に到着する。
  • バス停 4 から徒歩で移動し、時刻 34 に青木君の家に到着する。

2 番目のクエリについて、高橋君は下記の通りに移動を行って、時刻 22 に青木君の家に到着することができます。

  • 時刻 0 に高橋君の家を出発する。
  • 高橋君の家から徒歩で移動し、時刻 2 にバス停 1 に到着する。
  • 時刻 5 にバス停 1 を出発するバスに乗り、時刻 9 にバス停 2 に到着する。
  • 時刻 12 にバス停 2 を出発するバスに乗り、時刻 18 にバス停 3 に到着する。
  • 時刻 18 にバス停 3 を出発するバスに乗り、時刻 19 にバス停 4 に到着する。
  • バス停 4 から徒歩で移動し、時刻 22 に青木君の家に到着する。

Score : 450 points

Problem Statement

Takahashi is initially at his house and is about to visit Aoki's house.

There are N bus stops numbered 1 to N between the two houses, and Takahashi can move between them in the following ways:

  • He can walk from his house to bus stop 1 in X units of time.
  • For each i = 1, 2, \ldots, N-1, a bus departs from bus stop i at each time that is a multiple of P_i, and by taking this bus, he can get to bus stop (i+1) in T_i units of time. Here, the constraints guarantee that 1 \leq P_i \leq 8.
  • Takahashi can walk from bus stop N to Aoki's house in Y units of time.

For each i = 1, 2, \ldots, Q, process the following query.

Find the earliest time that Takahashi can arrive at Aoki's house when he leaves his house at time q_i.

Note that if he arrives at a bus stop exactly at the departure time of a bus, he can take that bus.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq X, Y \leq 10^9
  • 1 \leq P_i \leq 8
  • 1 \leq T_i \leq 10^9
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq q_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N X Y
P_1 T_1
P_2 T_2
\vdots
P_{N-1} T_{N-1}
Q
q_1
q_2
\vdots
q_Q

Output

Print Q lines. For each i = 1, 2, \ldots, Q, the i-th line should contain the answer to the i-th query.


Sample Input 1

4 2 3
5 4
6 6
3 1
7
13
0
710511029
136397527
763027379
644706927
447672230

Sample Output 1

34
22
710511052
136397548
763027402
644706946
447672250

For the first query, Takahashi can move as follows to arrive at Aoki's house at time 34.

  • Leave his house at time 13.
  • Walk from his house and arrive at bus stop 1 at time 15.
  • Take the bus departing from bus stop 1 at time 15 and arrive at bus stop 2 at time 19.
  • Take the bus departing from bus stop 2 at time 24 and arrive at bus stop 3 at time 30.
  • Take the bus departing from bus stop 3 at time 30 and arrive at bus stop 4 at time 31.
  • Walk from bus stop 4 and arrive at Aoki's house at time 34.

For the second query, Takahashi can move as follows and arrive at Aoki's house at time 22.

  • Leave his house at time 0.
  • Walk from his house and arrive at bus stop 1 at time 2.
  • Take the bus departing from bus stop 1 at time 5 and arrive at bus stop 2 at time 9.
  • Take the bus departing from bus stop 2 at time 12 and arrive at bus stop 3 at time 18.
  • Take the bus departing from bus stop 3 at time 18 and arrive at bus stop 4 at time 19.
  • Walk from bus stop 4 and arrive at Aoki's house at time 22.
I - Well-defined Path Queries on a Namori

実行時間制限: 3 sec / メモリ制限: 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)
  • GN 頂点 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