E - Dango

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

正の整数 L に対して、 レベル L のダンゴ文字列とは、以下の条件を満たす文字列です。

  • o- からなる長さ L+1 の文字列である。
  • 先頭の文字と末尾の文字のうちちょうど一方が - であり、そのほかの L 文字はすべて o である。

例えば、ooo- はレベル 3 のダンゴ文字列ですが、-ooo-ooo-oo- などはダンゴ文字列ではありません(より正確には、どのような正の整数 L に対してもレベル L のダンゴ文字列ではありません)。

2 種類の文字 o - からなる、長さ N の文字列 S が与えられます。 次の条件を満たすような正整数 X のうち、最大のものを求めてください。

  • S の連続する部分文字列であって、レベル X のダンゴ文字列であるものが存在する。

ただし、そのような整数が存在しない場合、-1 と出力してください。

制約

  • 1\leq N\leq 2\times10^5
  • So - からなる長さ N の文字列

入力

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

N
S

出力

S にレベル X のダンゴ文字列が含まれるような最大の X の値を 1 行で出力せよ。 そのような値が存在しない場合、-1 を出力せよ。


入力例 1

10
o-oooo---o

出力例 1

4

たとえば、S3 文字目から 7 文字目までに対応する部分文字列 oooo- は、レベル 4 のダンゴ文字列です。 S の部分文字列であってレベル 5 以上のダンゴ文字列であるようなものは存在しないため、4 と出力してください。


入力例 2

1
-

出力例 2

-1

S の連続する部分文字列は空文字列と -2 種類だけです。 これらはダンゴ文字列ではないため、-1 と出力してください。


入力例 3

30
-o-o-oooo-oo-o-ooooooo--oooo-o

出力例 3

7

Score : 300 points

Problem Statement

For a positive integer L, a level-L dango string is a string that satisfies the following conditions.

  • It is a string of length L+1 consisting of o and -.
  • Exactly one of the first character and the last character is -, and the other L characters are o.

For instance, ooo- is a level-3 dango string, but none of -ooo-, oo, and o-oo- is a dango string (more precisely, none of them is a level-L dango string for any positive integer L).

You are given a string S of length N consisting of the two characters o and -. Find the greatest positive integer X that satisfies the following condition.

  • There is a contiguous substring of S that is a level-X dango string.

If there is no such integer, print -1.

Constraints

  • 1\leq N\leq 2\times10^5
  • S is a string of length N consisting of o and -.

Input

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

N
S

Output

Print the greatest positive integer X such that S contains a level-X dango string, or -1 if there is no such integer.


Sample Input 1

10
o-oooo---o

Sample Output 1

4

For instance, the substring oooo- corresponding to the 3-rd through 7-th characters of S is a level-4 dango string. No substring of S is a level-5 dango string or above, so you should print 4.


Sample Input 2

1
-

Sample Output 2

-1

Only the empty string and - are the substrings of S. They are not dango strings, so you should print -1.


Sample Input 3

30
-o-o-oooo-oo-o-ooooooo--oooo-o

Sample Output 3

7
F - Jumping Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は数直線上の座標 0 の位置にいます。

これから高橋君は N 回のジャンプを行います。i \, (1 \leq i \leq N) 回目のジャンプでは、正の方向に a_i または b_i 移動します。

N 回のジャンプの後に座標 X の位置にいるようにすることはできますか?

制約

  • 1 \leq N \leq 100
  • 1 \leq a_i \lt b_i \leq 100 \, (1 \leq i \leq N)
  • 1 \leq X \leq 10000
  • 入力は全て整数

入力

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

N X
a_1 b_1
\vdots
a_N b_N

出力

N 回のジャンプの後に座標 X の位置にいるようにすることができるならば Yes と、そうでないなら No と出力せよ。


入力例 1

2 10
3 6
4 5

出力例 1

Yes

1 回目のジャンプでは b_1 (= 6) 移動し、2 回目のジャンプでは a_2 (= 4) 移動することで、座標 X (= 10) の位置にいるようにすることができます。


入力例 2

2 10
10 100
10 100

出力例 2

No

1 回目のジャンプの後に座標 X (= 10) の位置にいるようにすることはできますが、全てのジャンプの後に座標 X (= 10) の位置にいるようにすることはできません。


入力例 3

4 12
1 8
5 7
3 4
2 6

出力例 3

Yes

Score : 300 points

Problem Statement

Takahashi is standing at the coordinate 0 on a number line.

He will now perform N jumps. In the i-th jump (1 \leq i \leq N), he moves a_i or b_i in the positive direction.

Is it possible for him to be at the coordinate X after N jumps?

Constraints

  • 1 \leq N \leq 100
  • 1 \leq a_i \lt b_i \leq 100 \, (1 \leq i \leq N)
  • 1 \leq X \leq 10000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
a_1 b_1
\vdots
a_N b_N

Output

If it is possible for Takahashi to be at the coordinate X after N jumps, print Yes; otherwise, print No.


Sample Input 1

2 10
3 6
4 5

Sample Output 1

Yes

By moving b_1 (= 6) in the first jump and a_2 (= 4) in the second jump, he can be at the coordinate X (= 10).


Sample Input 2

2 10
10 100
10 100

Sample Output 2

No

He can be at the coordinate X (= 10) after the first jump, but not after all jumps.


Sample Input 3

4 12
1 8
5 7
3 4
2 6

Sample Output 3

Yes
G - Counting Ls

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

N \times N のマス目が与えられます。このうち上から i 行目、左から j 列目のマスを (i,j) と書きます。
各マスの状態を表す N 個の長さ N の文字列 S_1,S_2,\dots,S_N が以下の形式で与えられます。

  • S_ij 文字目が o であるとき、 (i,j) には o が書かれている。
  • S_ij 文字目が x であるとき、 (i,j) には x が書かれている。

以下の条件を全て満たすマスの三つ組の個数を求めてください。

  • 組に含まれる 3 マスは相異なる。
  • 3 マス全てに o が書かれている。
  • マスのうち、丁度 2 つが同じ行にある。
  • マスのうち、丁度 2 つが同じ列にある。

但し、ふたつの三つ組は、丁度一方に含まれるマスが存在する場合のみ区別します。

制約

  • N2 以上 2000 以下の整数
  • S_i は長さ Nox からなる文字列

入力

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

N
S_1
S_2
\vdots
S_N

出力

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


入力例 1

3
ooo
oxx
xxo

出力例 1

4

以下の 4 つの三つ組が条件を満たします。

  • (1,1),(1,2),(2,1)
  • (1,1),(1,3),(2,1)
  • (1,1),(1,3),(3,3)
  • (1,2),(1,3),(3,3)

入力例 2

4
oxxx
xoxx
xxox
xxxo

出力例 2

0

入力例 3

15
xooxxooooxxxoox
oxxoxoxxxoxoxxo
oxxoxoxxxoxoxxx
ooooxooooxxoxxx
oxxoxoxxxoxoxxx
oxxoxoxxxoxoxxo
oxxoxooooxxxoox
xxxxxxxxxxxxxxx
xooxxxooxxxooox
oxxoxoxxoxoxxxo
xxxoxxxxoxoxxoo
xooxxxooxxoxoxo
xxxoxxxxoxooxxo
oxxoxoxxoxoxxxo
xooxxxooxxxooox

出力例 3

2960

Score : 400 points

Problem Statement

You are given an N \times N grid. Let (i,j) denote the cell in the i-th row from the top and the j-th column from the left.
The states of the cells are given by N strings of length N, S_1, S_2, \dots, S_N, in the following format:

  • If the j-th character of S_i is o, there is an o written in cell (i,j).
  • If the j-th character of S_i is x, there is an x written in cell (i,j).

Find the number of triples of cells that satisfy all of the following conditions:

  • The three cells in the triple are distinct.
  • All three cells have an o written in them.
  • Exactly two of the cells are in the same row.
  • Exactly two of the cells are in the same column.

Here, two triples are considered different if and only if some cell is contained in exactly one of the triples.

Constraints

  • N is an integer between 2 and 2000, inclusive.
  • S_i is a string of length N consisting of o and x.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer as an integer.


Sample Input 1

3
ooo
oxx
xxo

Sample Output 1

4

The following four triples satisfy the conditions:

  • (1,1),(1,2),(2,1)
  • (1,1),(1,3),(2,1)
  • (1,1),(1,3),(3,3)
  • (1,2),(1,3),(3,3)

Sample Input 2

4
oxxx
xoxx
xxox
xxxo

Sample Output 2

0

Sample Input 3

15
xooxxooooxxxoox
oxxoxoxxxoxoxxo
oxxoxoxxxoxoxxx
ooooxooooxxoxxx
oxxoxoxxxoxoxxx
oxxoxoxxxoxoxxo
oxxoxooooxxxoox
xxxxxxxxxxxxxxx
xooxxxooxxxooox
oxxoxoxxoxoxxxo
xxxoxxxxoxoxxoo
xooxxxooxxoxoxo
xxxoxxxxoxooxxo
oxxoxoxxoxoxxxo
xooxxxooxxxooox

Sample Output 3

2960
H - Minimum OR Path

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

頂点に 1 から N の、辺に 1 から M の番号がついた N 頂点 M 辺の自己ループを持たない連結な無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を双方向に結んでおり、ラベル w_i がつけられています。

頂点 1 から頂点 N までの単純パス(同じ頂点を 2 度以上通らないパス)のうち、パスに含まれる辺につけられたラベルの総ビット単位 \mathrm{OR} としてあり得る最小値を求めてください。

ビット単位 \mathrm{OR} 演算とは

非負整数 A, B のビット単位 \mathrm{OR}A\ \mathrm{OR}\ B は以下のように定義されます。

  • A\ \mathrm{OR}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち少なくとも片方が 1 であれば 1、そうでなければ 0 である。
例えば、3\ \mathrm{OR}\ 5 = 7 となります (二進表記すると: 011\ \mathrm{OR}\ 101 = 111)。
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{OR}(\dots ((p_1\ \mathrm{OR}\ p_2)\ \mathrm{OR}\ p_3)\ \mathrm{OR}\ \dots\ \mathrm{OR}\ p_k) と定義され、これは p_1, p_2, p_3, \dots p_k の順番によらないことが証明できます。

制約

  • 2\le N\le 2\times 10^5
  • N-1\le M\le 2\times 10^5
  • 1\le u_i < v_i\le N
  • 0\le w_i< 2^{30}
  • 与えられるグラフは連結である
  • 入力される値は全て整数

入力

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

N M
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_M v_M w_M

出力

答えを出力せよ。


入力例 1

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

出力例 1

3

1,3,5 を順番に通り頂点 1,2,3,4 を順番に通ると総ビット単位 \mathrm{OR}1\ \mathrm{OR}\ 2\ \mathrm{OR}\ 3=3 となります。

総ビット単位 \mathrm{OR}3 より小さくすることはできないので、 3 を出力してください。


入力例 2

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

出力例 2

4

グラフには多重辺が存在する場合もあります。


入力例 3

8 12
4 5 16691344
5 7 129642441
2 7 789275447
3 8 335307651
3 5 530163333
5 6 811293773
3 8 333712701
1 2 2909941
2 3 160265478
5 7 465414272
1 3 903373004
6 7 408299562

出力例 3

468549631

Score : 450 points

Problem Statement

You are given a connected undirected graph with N vertices and M edges without self-loops, where vertices are numbered from 1 to N and edges are numbered from 1 to M. Edge i connects vertices u_i and v_i bidirectionally and has a label w_i.

Among the simple paths (paths that do not visit the same vertex more than once) from vertex 1 to vertex N, find the minimum possible value of the bitwise \mathrm{OR} of all labels on edges included in the path.

What is bitwise \mathrm{OR} operation?

The bitwise \mathrm{OR} of non-negative integers A and B, A\ \mathrm{OR}\ B, is defined as follows:

  • The digit in the 2^k (k \geq 0) place of A\ \mathrm{OR}\ B in binary representation is 1 if at least one of the digits in the 2^k place of A and B in binary representation is 1, and 0 otherwise.
For example, 3\ \mathrm{OR}\ 5 = 7 (in binary: 011\ \mathrm{OR}\ 101 = 111).
In general, the bitwise \mathrm{OR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1\ \mathrm{OR}\ p_2)\ \mathrm{OR}\ p_3)\ \mathrm{OR}\ \dots\ \mathrm{OR}\ p_k), and it can be proven that this does not depend on the order of p_1, p_2, p_3, \dots p_k.

Constraints

  • 2\le N\le 2\times 10^5
  • N-1\le M\le 2\times 10^5
  • 1\le u_i < v_i\le N
  • 0\le w_i< 2^{30}
  • The given graph is connected.
  • 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

Output the answer.


Sample Input 1

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

Sample Output 1

3

By traversing edges 1,3,5 in order and visiting vertices 1,2,3,4 in order, the total bitwise \mathrm{OR} is 1\ \mathrm{OR}\ 2\ \mathrm{OR}\ 3=3.

It is impossible to make the total bitwise \mathrm{OR} smaller than 3, so output 3.


Sample Input 2

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

Sample Output 2

4

The graph may contain multi-edges.


Sample Input 3

8 12
4 5 16691344
5 7 129642441
2 7 789275447
3 8 335307651
3 5 530163333
5 6 811293773
3 8 333712701
1 2 2909941
2 3 160265478
5 7 465414272
1 3 903373004
6 7 408299562

Sample Output 3

468549631
I - Pay or Receive

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

1,\ldots,N の番号がついた N 個の街と、1,\ldots,M の番号がついた M 本の道路があります。

道路 i は街 A_iB_i を結んでいます。道路を通行すると、所持している ポイント が次の通り増減します。

  • 道路 i を使って、街 A_i から街 B_i に移動するときにはポイントが C_i 増加し、街 B_i から街 A_i に移動するときにはポイントが C_i 減少する。

所持しているポイントは負にもなりえます。

次の Q 個の質問に答えてください。

  • 所持しているポイントが 0 である状態で街 X_i から移動を始めたとき、街 Y_i にいる状態で所持しているポイントの最大値を出力せよ。
    ただし、街 X_i から街 Y_i に到達できないときは nan、街 Y_i にいる状態で所持しているポイントをいくらでも増やせるときは inf を代わりに出力せよ。

制約

  • 2\leq N \leq 10^5
  • 0\leq M \leq 10^5
  • 1\leq Q \leq 10^5
  • 1\leq A_i,B_i,X_i,Y_i \leq N
  • 0\leq C_i \leq 10^9
  • 入力は全て整数である

入力

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

N M Q
A_1 B_1 C_1
\vdots
A_M B_M C_M
X_1 Y_1
\vdots
X_Q Y_Q

出力

問題文の指示通りに Q 行出力せよ。
i 行目には i 番目の質問に対する答えを出力せよ。


入力例 1

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

出力例 1

-2
inf
nan

1 番目の質問では、道路 5 を使って街 5 から街 3 に移動すると、ポイントを -2 所持している状態で街 3 にいることができます。
これ以上ポイントを大きくすることはできないので答えは -2 になります。

2 番目の質問では、「道路 2 を使って街 1 から街 2 に移動し、道路 1 を使って街 2 から街 1 に移動する」 という行動を好きなだけ繰り返したあと、道路 2 を使って街 1 から街 2 に移動することで、 街 2 にいる状態で所持しているポイントをいくらでも増やすことができます。

3 番目の質問では、街 3 から移動を始めて街 1 へ到達することはできません。


入力例 2

2 1 1
1 1 1
1 1

出力例 2

inf

始点と終点が同じ街である道路や、始点と終点が同じ街である質問が含まれることもあります。


入力例 3

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

出力例 3

inf
nan
nan
inf
-9

Score : 500 points

Problem Statement

There are N towns numbered 1,\ldots,N and M roads numbered 1,\ldots,M.

Road i connects towns A_i and B_i. When you use a road, your score changes as follows:

  • when you move from town A_i to town B_i using road i, your score increases by C_i; when you move from town B_i to town A_i using road i, your score decreases by C_i.

Your score may become negative.

Answer the following Q questions.

  • If you start traveling from town X_i with initial score 0, find the maximum possible score when you are at town Y_i.
    Here, if you cannot get from town X_i to town Y_i, print nan instead; if you can have as large a score as you want when you are at town Y_i, print inf instead.

Constraints

  • 2\leq N \leq 10^5
  • 0\leq M \leq 10^5
  • 1\leq Q \leq 10^5
  • 1\leq A_i,B_i,X_i,Y_i \leq N
  • 0\leq C_i \leq 10^9
  • All values in the input are integers.

Input

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

N M Q
A_1 B_1 C_1
\vdots
A_M B_M C_M
X_1 Y_1
\vdots
X_Q Y_Q

Output

Print Q lines as specified in the Problem Statement.
The i-th line should contain the answer to the i-th question.


Sample Input 1

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

Sample Output 1

-2
inf
nan

For the first question, if you use road 5 to move from town 5 to town 3, you can have a score -2 when you are at town 3.
Since you cannot make the score larger, the answer is -2.

For the second question, you can have as large a score as you want when you are at town 2 if you travel as follows: repeatedly "use road 2 to move from town 1 to town 2 and then use road 1 to move from town 2 to town 1" as many times as you want, and finally use road 2 to move from town 1 to town 2.

For the third question, you cannot get from town 3 to town 1.


Sample Input 2

2 1 1
1 1 1
1 1

Sample Output 2

inf

The endpoints of a road may be the same, and so may the endpoints given in a question.


Sample Input 3

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

Sample Output 3

inf
nan
nan
inf
-9