A - "atcoder".substr()

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

配点 : 100

問題文

文字列 atcoderL 文字目から R 文字目までを出力してください。

制約

  • L,R は整数
  • 1 \le L \le R \le 7

入力

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

L R

出力

答えを出力せよ。


入力例 1

3 6

出力例 1

code

atcoder3 文字目から 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
B - Broken Rounding

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

配点 : 200

問題文

非負整数 X に対し、 i=0,1,\dots,K-1 の順に次の操作を行ったとき、操作を全て終えた時点での X を求めてください。

  • X10^i の位以下を四捨五入する。
    • 厳密には、 X を「 |Y-X| が最小となる 10^{i+1} の倍数のうち最大のもの」である Y に置き換える。
    • 具体例を挙げる。
      • 27310^1 の位以下を四捨五入すれば 300 となる。
      • 99910^2 の位以下を四捨五入すれば 1000 となる。
      • 10010^9 の位以下を四捨五入すれば 0 となる。
      • 101510^0 の位以下を四捨五入すれば 1020 となる。

制約

  • X,K は整数
  • 0 \le X < 10^{15}
  • 1 \le K \le 15

入力

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

X K

出力

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


入力例 1

2048 2

出力例 1

2100

操作の過程で、 X2048 \rightarrow 2050 \rightarrow 2100 と変化します。


入力例 2

1 15

出力例 2

0

入力例 3

999 3

出力例 3

1000

入力例 4

314159265358979 12

出力例 4

314000000000000

X32bit 整数型に収まらない可能性があります。

Score : 200 points

Problem Statement

Given a non-negative integer X, perform the following operation for i=1,2,\dots,K in this order and find the resulting X.

  • Round X off to the nearest 10^i.
    • Formally, replace X with Y that is "the largest multiple of 10^i that minimizes |Y-X|."
    • Here are some examples:
      • Rounding 273 off to the nearest 10^2 yields 300.
      • Rounding 999 off to the nearest 10^3 yields 1000.
      • Rounding 100 off to the nearest 10^{10} yields 0.
      • Rounding 1015 off to the nearest 10^1 yields 1020.

Constraints

  • X and K are integers.
  • 0 \le X < 10^{15}
  • 1 \le K \le 15

Input

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

X K

Output

Print the answer as an integer.


Sample Input 1

2048 2

Sample Output 1

2100

X changes as 2048 \rightarrow 2050 \rightarrow 2100 by the operations.


Sample Input 2

1 15

Sample Output 2

0

Sample Input 3

999 3

Sample Output 3

1000

Sample Input 4

314159265358979 12

Sample Output 4

314000000000000

X may not fit into a 32-bit integer type.

C - Yamanote Line Game

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

配点 : 300

問題文

高橋君と青木君は 2 人で次の対戦ゲームをします。

高橋君が先手でゲームを始め、ゲームが終了するまでの間、 2 人は交互に 1 以上 2N+1 以下の整数を 1 つずつ宣言します。 どちらかが一度でも宣言した整数は、それ以降どちらも二度と宣言することが出来ません。 先に整数を宣言することが出来なくなった方のプレイヤーの負けとなり、負けなかった方のプレイヤーの勝ちとなります。

このゲームでは必ず高橋君が勝ちます。 高橋君の立場で実際にゲームを行い、ゲームに勝ってください。

制約

  • 1 \leq N \leq 1000
  • N は整数

入出力

この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。
あなたのプログラムが高橋君の立場で、ジャッジプログラムが青木君の立場でゲームを行います。

まず、あなたのプログラムに標準入力から正の整数 N が与えられます。 その後、ゲームが終了するまで下記の手順を繰り返します。

  1. あなたのプログラムが、高橋君が宣言する整数として、1 以上 2N+1 以下の整数を標準出力に出力します。(どちらかのプレイヤーによってすでに宣言されている整数を出力することは出来ません。)
  2. ジャッジプログラムによって、青木君が宣言する整数があなたのプログラムに標準入力から与えられます。(どちらかのプレイヤーによってすでに宣言されている整数が入力されることはありません。) ただし、青木君が宣言できる整数が残っていない場合は、代わりに 0 が与えられ高橋君の勝ちでゲームが終了します。

注意点

  • 出力を行うたびに標準出力をflushしてください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 高橋君の勝ちでゲームが終了したあと、あなたのプログラムは直ちに終了しなければなりません。そうしなかった場合、ジャッジ結果が AC とならない可能性があります。
  • ゲームの途中で不正な出力を行った場合(例えば、すでにどちらかのプレイヤーによって宣言されている整数を出力した場合)は不正解となりますが、そのときのジャッジ結果は不定です。WA になるとは限りません。

入出力例

入力 出力 説明
2 まず整数 N が与えられます。
1 高橋君が 1 を宣言します。
3 青木君が 3 を宣言します。
2 高橋君が 2 を宣言します。
4 青木君が 4 を宣言します。
5 高橋君が 5 を宣言します。
0 青木君が宣言できる整数が残っていないため、高橋君の勝ちでゲームが終了します。

Score : 300 points

Problem Statement

Takahashi and Aoki will play the following game against each other.

Starting from Takahashi, the two alternatingly declare an integer between 1 and 2N+1 (inclusive) until the game ends. Any integer declared by either player cannot be declared by either player again. The player who is no longer able to declare an integer loses; the player who didn't lose wins.

In this game, Takahashi will always win. Your task is to actually play the game on behalf of Takahashi and win the game.

Constraints

  • 1 \leq N \leq 1000
  • N is an integer.

Input and Output

This task is an interactive task (in which your program and the judge program interact with each other via inputs and outputs).
Your program plays the game on behalf of Takahashi, and the judge program plays the game on behalf of Aoki.

First, your program is given a positive integer N from Standard Input. Then, the following procedures are repeated until the game ends.

  1. Your program outputs an integer between 1 and 2N+1 (inclusive) to Standard Output, which defines the integer that Takahashi declares. (You cannot output an integer that is already declared by either player.)
  2. The integer that Aoki declares is given by the judge program to your program from Standard Input. (No integer that is already declared by either player will be given.) If Aoki has no more integer to declare, 0 is given instead, which means that the game ended and Takahashi won.

Notes

  • After each output, you must flush Standard Output. Otherwise, you may get TLE.
  • After the game ended and Takahashi won, the program must be terminated immediately. Otherwise, the judge does not necessarily give AC.
  • If your program outputs something that violates the rules of the game (such as an integer that has already been declared by either player), your answer is considered incorrect. In such case, the verdict is indeterminate. It does not necessarily give WA.

Sample Input and Output

Input Output Description
2 First, an integer N is given.
1 Takahashi declares an integer 1.
3 Aoki declares an integer 3.
2 Takahashi declares an integer 2.
4 Aoki declares an integer 4.
5 Takahashi declares an integer 5.
0 Aoki has no more integer to declare, so Takahashi wins, and the game ends.
D - Add One Edge

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

配点 : 400

問題文

N_1+N_2 頂点 M 辺の無向グラフがあります。i=1,2,\ldots,M に対し、i 番目の辺は頂点 a_i と頂点 b_i を結びます。
また、以下を満たすことが保障されます。

  • 1 \leq u,v \leq N_1 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • N_1+1 \leq u,v \leq N_1+N_2 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • 頂点 1 と頂点 N_1+N_2 は非連結

次の操作をちょうど 1 回行います。

  • 1 \leq u \leq N_1 を満たす整数 uN_1+1 \leq v \leq N_1+N_2 を満たす整数 v を選び、頂点 u と頂点 v を結ぶ辺を追加する

操作後のグラフにおいて、頂点 1 と頂点 N_1+N_2 は必ず連結であることが示せます。そこで、頂点 1 と頂点 N_1+N_2 を結ぶ経路の長さ(辺の本数)の最小値を d とします。

操作で追加する辺を適切に選んだ時にありえる d の最大値を求めてください。

連結とは? 無向グラフの頂点 u,v が連結であるとは、頂点 u と頂点 v を結ぶ経路が存在することをいいます。

制約

  • 1 \leq N_1,N_2 \leq 1.5 \times 10^5
  • 0 \leq M \leq 3 \times 10^5
  • 1 \leq a_i \leq b_i \leq N_1+N_2
  • i \neq j ならば (a_i,b_i) \neq (a_j,b_j)
  • 1 \leq u,v \leq N_1 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • N_1+1 \leq u,v \leq N_1+N_2 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • 頂点 1 と頂点 N_1+N_2 は非連結
  • 入力はすべて整数

入力

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

N_1 N_2 M
a_1 b_1
\vdots
a_M b_M

出力

答えを出力せよ。


入力例 1

3 4 6
1 2
2 3
4 5
4 6
1 3
6 7

出力例 1

5

u=2,v=5 として操作することで d=5 と出来ます。これが最大値です。


入力例 2

7 5 20
10 11
4 5
10 12
1 2
1 5
5 6
2 4
3 5
9 10
2 5
1 4
11 12
9 12
8 9
5 7
3 7
3 6
3 4
8 12
9 11

出力例 2

4

Score : 400 points

Problem Statement

We have an undirected graph with (N_1+N_2) vertices and M edges. For i=1,2,\ldots,M, the i-th edge connects vertex a_i and vertex b_i.
The following properties are guaranteed:

  • Vertex u and vertex v are connected, for all integers u and v with 1 \leq u,v \leq N_1.
  • Vertex u and vertex v are connected, for all integers u and v with N_1+1 \leq u,v \leq N_1+N_2.
  • Vertex 1 and vertex (N_1+N_2) are disconnected.

Consider performing the following operation exactly once:

  • choose an integer u with 1 \leq u \leq N_1 and an integer v with N_1+1 \leq v \leq N_1+N_2, and add an edge connecting vertex u and vertex v.

We can show that vertex 1 and vertex (N_1+N_2) are always connected in the resulting graph; so let d be the minimum length (number of edges) of a path between vertex 1 and vertex (N_1+N_2).

Find the maximum possible d resulting from adding an appropriate edge to add.

Definition of "connected" Two vertices u and v of an undirected graph are said to be connected if and only if there is a path between vertex u and vertex v.

Constraints

  • 1 \leq N_1,N_2 \leq 1.5 \times 10^5
  • 0 \leq M \leq 3 \times 10^5
  • 1 \leq a_i \leq b_i \leq N_1+N_2
  • (a_i,b_i) \neq (a_j,b_j) if i \neq j.
  • Vertex u and vertex v are connected for all integers u and v such that 1 \leq u,v \leq N_1.
  • Vertex u and vertex v are connected for all integers u and v such that N_1+1 \leq u,v \leq N_1+N_2.
  • Vertex 1 and vertex (N_1+N_2) are disconnected.
  • All input values are integers.

Input

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

N_1 N_2 M
a_1 b_1
\vdots
a_M b_M

Output

Print the answer.


Sample Input 1

3 4 6
1 2
2 3
4 5
4 6
1 3
6 7

Sample Output 1

5

If we set u=2 and v=5, the operation yields d=5, which is the maximum possible.


Sample Input 2

7 5 20
10 11
4 5
10 12
1 2
1 5
5 6
2 4
3 5
9 10
2 5
1 4
11 12
9 12
8 9
5 7
3 7
3 6
3 4
8 12
9 11

Sample Output 2

4
E - Good Graph

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

配点 : 475

問題文

N 頂点 M 辺の無向グラフ G が与えられます。 i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ無向辺です。

N 頂点のグラフは、すべての i = 1, 2, \ldots, K について下記の条件が成り立つとき、良いグラフと呼ばれます。

  • G 上で頂点 x_i と頂点 y_i を結ぶパスが存在しない。

与えられるグラフ G は良いグラフです。

Q 個の独立な質問が与えられるので、それらすべてに答えてください。 i = 1, 2, \ldots, Q について、i 番目の質問は

  • 入力で与えられたグラフ G に頂点 p_i と頂点 q_i を結ぶ無向辺を 1 本追加して得られるグラフ G^{(i)} は良いグラフですか?

という質問です。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N
  • 1 \leq K \leq 2 \times 10^5
  • 1 \leq x_i, y_i \leq N
  • x_i \neq y_i
  • i \neq j \implies \lbrace x_i, y_i \rbrace \neq \lbrace x_j, y_j \rbrace
  • すべての i = 1, 2, \ldots, K について次が成り立つ:頂点 x_i と頂点 y_i を結ぶパスは存在しない。
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq p_i, q_i \leq N
  • p_i \neq q_i
  • 入力はすべて整数

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M
K
x_1 y_1
x_2 y_2
\vdots
x_K y_K
Q
p_1 q_1
p_2 q_2
\vdots
p_Q q_Q

出力

Q 行出力せよ。 i = 1, 2, \ldots, Q について、i 行目には i 番目の質問に対する答えを出力せよ。 具体的には、グラフ G^{(i)} が良いグラフである場合は Yes を、良いグラフでない場合は No を出力せよ。


入力例 1

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

出力例 1

No
No
Yes
Yes
  • 1 番目の質問に関して、グラフ G^{(1)} は良いグラフではありません。なぜなら、頂点 x_1 = 1 と頂点 y_1 = 5 を結ぶパス 1 \rightarrow 2 \rightarrow 5 を持つからです。よって、No と出力します。
  • 2 番目の質問に関して、グラフ G^{(2)} は良いグラフではありません。なぜなら、頂点 x_2 = 2 と頂点 y_2 = 6 を結ぶパス 2 \rightarrow 6 を持つからです。よって、No と出力します。
  • 3 番目の質問に関して、グラフ G^{(3)} は良いグラフです。よって、Yes と出力します。
  • 4 番目の質問に関して、グラフ G^{(4)} は良いグラフです。よって、Yes と出力します。

この入力例のように、与えられるグラフ G が自己ループや多重辺を持つ場合があることに注意してください。

Score : 475 points

Problem Statement

You are given an undirected graph G with N vertices and M edges. For i = 1, 2, \ldots, M, the i-th edge is an undirected edge connecting vertices u_i and v_i.

A graph with N vertices is called good if the following condition holds for all i = 1, 2, \ldots, K:

  • there is no path connecting vertices x_i and y_i in G.

The given graph G is good.

You are given Q independent questions. Answer all of them. For i = 1, 2, \ldots, Q, the i-th question is as follows.

  • Is the graph G^{(i)} obtained by adding an undirected edge connecting vertices p_i and q_i to the given graph G good?

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times10^5
  • 1 \leq u_i, v_i \leq N
  • 1 \leq K \leq 2 \times 10^5
  • 1 \leq x_i, y_i \leq N
  • x_i \neq y_i
  • i \neq j \implies \lbrace x_i, y_i \rbrace \neq \lbrace x_j, y_j \rbrace
  • For all i = 1, 2, \ldots, K, there is no path connecting vertices x_i and y_i.
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq p_i, q_i \leq N
  • p_i \neq q_i
  • All input values 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
K
x_1 y_1
x_2 y_2
\vdots
x_K y_K
Q
p_1 q_1
p_2 q_2
\vdots
p_Q q_Q

Output

Print Q lines. For i = 1, 2, \ldots, Q, the i-th line should contain the answer to the i-th question: Yes if the graph G^{(i)} is good, and No otherwise.


Sample Input 1

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

Sample Output 1

No
No
Yes
Yes
  • For the first question, the graph G^{(1)} is not good because it has a path 1 \rightarrow 2 \rightarrow 5 connecting vertices x_1 = 1 and y_1 = 5. Therefore, print No.
  • For the second question, the graph G^{(2)} is not good because it has a path 2 \rightarrow 6 connecting vertices x_2 = 2 and y_2 = 6. Therefore, print No.
  • For the third question, the graph G^{(3)} is good. Therefore, print Yes.
  • For the fourth question, the graph G^{(4)} is good. Therefore, print Yes.

As seen in this sample input, note that the given graph G may have self-loops or multi-edges.

F - Find 4-cycle

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

配点 : 500

問題文

S+T 頂点 M 辺の単純無向グラフ G があります。頂点には 1 から S+T の番号が、辺には 1 から M の番号が割り振られています。辺 i は頂点 u_i と頂点 v_i を結んでいます。
また、頂点集合 V_1 = \lbrace 1, 2,\dots, S\rbrace および V_2 = \lbrace S+1, S+2, \dots, S+T \rbrace はともに独立集合です。

長さ 4 のサイクルを 4-cycle と呼びます。
G が 4-cycle を含む場合、4-cycle をどれか 1 つ選び、選んだサイクルに含まれる頂点を出力してください。頂点を出力する順番は自由です。
G が 4-cycle を含まない場合、 -1 を出力してください。

独立集合とは? グラフ G の独立集合とは、G に含まれるいくつかの頂点からなる集合 V' であって、V' に含まれる任意の 2 つの頂点の間に辺がないものを言います。

制約

  • 2 \leq S \leq 3 \times 10^5
  • 2 \leq T \leq 3000
  • 4 \leq M \leq \min(S \times T,3 \times 10^5)
  • 1 \leq u_i \leq S
  • S + 1 \leq v_i \leq S + T
  • i \neq j ならば (u_i, v_i) \neq (u_j, v_j)
  • 入力される値はすべて整数

入力

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

S T M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

G が 4-cycle を含む場合は、任意の 4-cycle を 1 つ選び、選んだサイクルに含まれている相異なる 4 個の頂点の番号を出力せよ。(頂点の順番は問わない。)
G が 4-cycle を含まない場合は -1 を出力せよ。


入力例 1

2 3 5
1 3
1 4
1 5
2 4
2 5

出力例 1

1 2 4 5

頂点 14422551 の間に辺があるので 頂点 1,2,4,5 は 4-cycle をなします。よって 1, 2, 4, 5 を出力すればよいです。
頂点を出力する順番は自由で、出力例のほかにも例えば 2 5 1 4 のような出力も正答となります。


入力例 2

3 2 4
1 4
1 5
2 5
3 5

出力例 2

-1

G が 4-cycle を含まない入力もあります。


入力例 3

4 5 9
3 5
1 8
3 7
1 9
4 6
2 7
4 8
1 7
2 9

出力例 3

1 7 2 9

Score : 500 points

Problem Statement

We have a simple undirected graph G with (S+T) vertices and M edges. The vertices are numbered 1 through (S+T), and the edges are numbered 1 through M. Edge i connects Vertices u_i and v_i.
Here, vertex sets V_1 = \lbrace 1, 2,\dots, S\rbrace and V_2 = \lbrace S+1, S+2, \dots, S+T \rbrace are both independent sets.

A cycle of length 4 is called a 4-cycle.
If G contains a 4-cycle, choose any of them and print the vertices in the cycle. You may print the vertices in any order.
If G does not contain a 4-cycle, print -1.

What is an independent set? An independent set of a graph G is a set V' of some of the vertices in G such that no two vertices of V' have an edge between them.

Constraints

  • 2 \leq S \leq 3 \times 10^5
  • 2 \leq T \leq 3000
  • 4 \leq M \leq \min(S \times T,3 \times 10^5)
  • 1 \leq u_i \leq S
  • S + 1 \leq v_i \leq S + T
  • 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:

S T M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

If G contains a 4-cycle, choose any of them, and print the indices of the four distinct vertices in the cycle. (The order of the vertices does not matter.)
If G does not contain a 4-cycle, print -1.


Sample Input 1

2 3 5
1 3
1 4
1 5
2 4
2 5

Sample Output 1

1 2 4 5

There are edges between Vertices 1 and 4, 4 and 2, 2 and 5, and 5 and 1, so Vertices 1, 2, 4, and 5 form a 4-cycle. Thus, 1, 2, 4, and 5 should be printed.
The vertices may be printed in any order. Besides the Sample Output, 2 5 1 4 is also considered correct, for example.


Sample Input 2

3 2 4
1 4
1 5
2 5
3 5

Sample Output 2

-1

Some inputs may give G without a 4-cycle.


Sample Input 3

4 5 9
3 5
1 8
3 7
1 9
4 6
2 7
4 8
1 7
2 9

Sample Output 3

1 7 2 9
G - Elevators

実行時間制限: 6 sec / メモリ制限: 1024 MB

配点 : 600

問題文

N 棟の 10^9 階建てのビルからなる建物があります。ビルには 1 から N の番号がついています。

任意の異なるビルの同じ階は連絡通路で結ばれているため 1 分で移動可能です。

また、M 基のエレベーターがあります。i 個目のエレベーターはビル A_iB_i 階から C_i 階を結ぶものです。このエレベーターを使うと、B_i \le x,y \le C_i を満たす全ての整数の組 x,y に対し、ビル A_ix 階から y 階に |x-y| 分で移動することができます。

以下の Q 個のクエリに答えてください。

  • ビル X_iY_i 階からビル Z_iW_i 階に移動することが可能か判定し、可能な場合は移動時間の最小値を求めてください。

制約

  • 1 \le N,M,Q \le 2 \times 10^5
  • 1 \le A_i \le N
  • 1 \le B_i < C_i \le 10^9
  • 1 \le X_i,Z_i \le N
  • 1 \le Y_i,W_i \le 10^9
  • 入力はすべて整数である。

入力

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

N M Q
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下の形式で与えられる。

X_i Y_i Z_i W_i

出力

Q 行出力せよ。i 行目には \mathrm{query}_i について、移動することが不可能であれば -1 を、そうでないならば移動時間の最小値を出力せよ。


入力例 1

3 4 3
1 2 10
2 3 7
3 9 14
3 1 3
1 3 3 14
3 1 2 7
1 100 1 101

出力例 1

12
7
-1

1 番目のクエリについては、以下のようにすることで 12 分で移動が可能です。

  • エレベーター 1 を使い、ビル 13 階から 9 階へ移動する。この移動には 6 分かかる。
  • 9 階の連絡通路を使い、ビル 1 からビル 3 へ移動する。この移動には 1 分かかる。
  • エレベーター 3 を使い、ビル 39 階から 14 階で移動する。この移動には 5 分かかる。

また、3 番目のクエリについては、移動することが不可能であるため -1 を出力します。


入力例 2

1 1 1
1 1 2
1 1 1 2

出力例 2

1

Score : 600 points

Problem Statement

There is a complex composed of N 10^9-story skyscrapers. The skyscrapers are numbered 1 to N, and the floors are numbered 1 to 10^9.

From any floor of any skyscraper, one can use a skybridge to get to the same floor of any other skyscraper in one minute.

Additionally, there are M elevators. The i-th elevator runs between Floor B_i and Floor C_i of Skyscraper A_i. With this elevator, one can get from Floor x to Floor y of Skyscraper A_i in |x-y| minutes, for every pair of integers x,y such that B_i \le x,y \le C_i.

Answer the following Q queries.

  • Determine whether it is possible to get from Floor Y_i of Skyscraper X_i to Floor W_i of Skyscraper Z_i, and find the shortest time needed to get there if it is possible.

Constraints

  • 1 \le N,M,Q \le 2 \times 10^5
  • 1 \le A_i \le N
  • 1 \le B_i < C_i \le 10^9
  • 1 \le X_i,Z_i \le N
  • 1 \le Y_i,W_i \le 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is in the following format:

X_i Y_i Z_i W_i

Output

Print Q lines. The i-th line should contain -1 if, for \mathrm{query}_i, the destination is unreachable; otherwise, it should contain the minimum number of minutes needed to get there.


Sample Input 1

3 4 3
1 2 10
2 3 7
3 9 14
3 1 3
1 3 3 14
3 1 2 7
1 100 1 101

Sample Output 1

12
7
-1

For the 1-st query, you can get to the destination in 12 minutes as follows.

  • Use Elevator 1 to get from Floor 3 to Floor 9 of Skyscraper 1, in 6 minutes.
  • Use the skybridge on Floor 9 to get from Skyscraper 1 to Skyscraper 3, in 1 minute.
  • Use Elevator 3 to get from Floor 9 to Floor 14 of Skyscraper 3, in 5 minutes.

For the 3-rd query, the destination is unreachable, so -1 should be printed.


Sample Input 2

1 1 1
1 1 2
1 1 1 2

Sample Output 2

1
H - K-Coloring

実行時間制限: 8 sec / メモリ制限: 1024 MB

配点 : 600

問題文

頂点に 1 から N の、辺に 1 から M の番号がついた N 頂点 M 辺の単純無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を結んでいます。

このグラフのそれぞれの頂点に 1 以上 K 以下の整数を書きこむ方法のうち、次の条件を満たす方法の個数を 998244353 で割った余りを求めてください。

  • 辺で結ばれた頂点同士には異なる数が書きこまれている。

制約

  • 1 \leq N \leq 30
  • 0 \leq M \leq \min \left(30, \frac{N(N-1)}{2} \right)
  • 1 \leq K \leq 10^9
  • 1 \leq u_i \lt v_i \leq N
  • 入力で与えられるグラフは単純

入力

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

N M K
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

条件を満たすように頂点に 1 以上 K 以下の整数を書きこむ方法の個数を 998244353 で割った余りを出力せよ。


入力例 1

4 3 2
1 2
2 4
2 3

出力例 1

2

条件を満たす整数の書きこみ方は次の 2 通りです。

  • 頂点 1, 3, 41 を、頂点 22 を書きこむ。
  • 頂点 21 を、頂点 1, 3, 42 を書きこむ。

入力例 2

4 0 10

出力例 2

10000

10^4 通り全ての書きこみ方が条件を満たします。


入力例 3

5 10 5
3 5
1 3
1 2
1 4
3 4
2 5
4 5
1 5
2 3
2 4

出力例 3

120

入力例 4

5 6 294
1 2
2 4
1 3
2 3
4 5
3 5

出力例 4

838338733

入力例 5

7 12 1000000000
4 5
2 7
3 4
6 7
3 5
5 6
5 7
1 3
4 7
1 5
2 3
3 6

出力例 5

418104233

Score : 600 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, modulo 998244353, of ways to write an integer between 1 and K, inclusive, on each vertex of this graph to satisfy the following condition:

  • two vertices connected by an edge always have different numbers written on them.

Constraints

  • 1 \leq N \leq 30
  • 0 \leq M \leq \min \left(30, \frac{N(N-1)}{2} \right)
  • 1 \leq K \leq 10^9
  • 1 \leq u_i \lt v_i \leq N
  • The given graph is simple.

Input

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

N M K
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

Print the number, modulo 998244353, of ways to write integers between 1 and K, inclusive, on the vertices to satisfy the condition.


Sample Input 1

4 3 2
1 2
2 4
2 3

Sample Output 1

2

Here are the two ways to satisfy the condition.

  • Write 1 on vertices 1, 3, 4, and write 2 on vertex 2.
  • Write 1 on vertex 2, and write 2 on vertex 1, 3, 4.

Sample Input 2

4 0 10

Sample Output 2

10000

All 10^4 ways satisfy the condition.


Sample Input 3

5 10 5
3 5
1 3
1 2
1 4
3 4
2 5
4 5
1 5
2 3
2 4

Sample Output 3

120

Sample Input 4

5 6 294
1 2
2 4
1 3
2 3
4 5
3 5

Sample Output 4

838338733

Sample Input 5

7 12 1000000000
4 5
2 7
3 4
6 7
3 5
5 6
5 7
1 3
4 7
1 5
2 3
3 6

Sample Output 5

418104233