A - Last Letter

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

英小文字からなる長さ N の文字列 S が与えられます。S の末尾の文字を出力してください。

制約

  • N は整数
  • 1 ≤ N ≤ 1000
  • S は英小文字からなる長さ N の文字列

入力

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

N
S

出力

S の末尾の文字を出力せよ。


入力例 1

5
abcde

出力例 1

e

S = {}abcde です。S の末尾の文字は e なので、e を出力します。


入力例 2

1
a

出力例 2

a

Score : 100 points

Problem Statement

Given a string S of length N consisting of lowercase English alphabets, print the last character of S.

Constraints

  • N is an integer.
  • 1 ≤ N ≤ 1000
  • S is a string of length N consisting of lowercase English alphabets.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the last character of S.


Sample Input 1

5
abcde

Sample Output 1

e

The last character of S = {}abcde is e, so e should be printed.


Sample Input 2

1
a

Sample Output 2

a
B - Go Straight and Turn Right

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

xy 平面を考えます。x 軸の正の向きを東向き、y 軸の正の向きを北向きとします。
高橋君ははじめ、点 (x, y) = (0, 0) にいて東( x 軸の正の向き)を向いています。

SR のみからなる長さ N の文字列 T = t_1t_2\ldots t_N が与えられます。 高橋君は i = 1, 2, \ldots, N の順番で下記を行います。

  • t_i = S ならば、高橋君はいま向いている方向に距離 1 だけ進む。
  • t_i = R ならば、高橋君はその場で右に 90 度回転する。その結果、高橋君の向いている方向が下記の通りに変わる。
    • 回転前の向きが東向き( x 軸の正の向き)ならば、回転後の向きは南向き( y 軸の負の向き)になる。
    • 回転前の向きが南向き( y 軸の負の向き)ならば、回転後の向きは西向き( x 軸の負の向き)になる。
    • 回転前の向きが西向き( x 軸の負の向き)ならば、回転後の向きは北向き( y 軸の正の向き)になる。
    • 回転前の向きが北向き( y 軸の正の向き)ならば、回転後の向きは東向き( x 軸の正の向き)になる。

上記の手順を終えた後に高橋君がいる点の座標を出力してください。

制約

  • 1 \leq N \leq 10^5
  • N は整数
  • TSR のみからなる長さ N の文字列

入力

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

N
T

出力

問題文中の手順を終えた後に高橋君がいる点の座標 (x, y) を、下記の形式にしたがって空白区切りで出力せよ。

x y

入力例 1

4
SSRS

出力例 1

2 -1

高橋君ははじめ (0, 0) にいて東を向いています。その後、高橋君は下記の通りに行動します。

  1. t_1 = S であるので、高橋君は東に距離 1 だけ進んだ (1, 0) に移動します。
  2. t_2 = S であるので、高橋君は東に距離 1 だけ進んだ (2, 0) に移動します。
  3. t_3 = R であるので、高橋君は右に 90 度回転し、高橋君は南を向きます。
  4. t_4 = S であるので、高橋君は南に距離 1 だけ進んだ (2, -1) に移動します。

よって、高橋君の最終的な位置である (x, y) = (2, -1) を出力します。


入力例 2

20
SRSRSSRSSSRSRRRRRSRR

出力例 2

0 1

Score : 200 points

Problem Statement

Consider an xy-plane. The positive direction of the x-axis is in the direction of east, and the positive direction of the y-axis is in the direction of north.
Takahashi is initially at point (x, y) = (0, 0) and facing east (in the positive direction of the x-axis).

You are given a string T = t_1t_2\ldots t_N of length N consisting of S and R. Takahashi will do the following move for each i = 1, 2, \ldots, N in this order.

  • If t_i = S, Takahashi advances in the current direction by distance 1.
  • If t_i = R, Takahashi turns 90 degrees clockwise without changing his position. As a result, Takahashi's direction changes as follows.
    • If he is facing east (in the positive direction of the x-axis) before he turns, he will face south (in the negative direction of the y-axis) after he turns.
    • If he is facing south (in the negative direction of the y-axis) before he turns, he will face west (in the negative direction of the x-axis) after he turns.
    • If he is facing west (in the negative direction of the x-axis) before he turns, he will face north (in the positive direction of the y-axis) after he turns.
    • If he is facing north (in the positive direction of the y-axis) before he turns, he will face east (in the positive direction of the x-axis) after he turns.

Print the coordinates Takahashi is at after all the steps above have been done.

Constraints

  • 1 \leq N \leq 10^5
  • N is an integer.
  • T is a string of length N consisting of S and R.

Input

Input is given from Standard Input in the following format:

N
T

Output

Print the coordinates (x, y) Takahashi is at after all the steps described in the Problem Statement have been completed, in the following format, with a space in between:

x y

Sample Input 1

4
SSRS

Sample Output 1

2 -1

Takahashi is initially at (0, 0) facing east. Then, he moves as follows.

  1. t_1 = S, so he advances in the direction of east by distance 1, arriving at (1, 0).
  2. t_2 = S, so he advances in the direction of east by distance 1, arriving at (2, 0).
  3. t_3 = R, so he turns 90 degrees clockwise, resulting in facing south.
  4. t_4 = S, so he advances in the direction of south by distance 1, arriving at (2, -1).

Thus, Takahashi's final position, (x, y) = (2, -1), should be printed.


Sample Input 2

20
SRSRSSRSSSRSRRRRRSRR

Sample Output 2

0 1
C - Yamanote Line Game

Time Limit: 2 sec / Memory Limit: 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 - Swap Hats

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

1, 2, 3 の番号がついた 3 人の高橋くんがおり、赤・緑・青の色がついた 3 種類の帽子がそれぞれ 1 つずつあります。それぞれの高橋くんは帽子を 1 つかぶっており、高橋くん i がはじめにかぶっている帽子の色は文字 S_i で表されます。ここで、R は赤、G は緑、B は青に対応しています。これから、以下の操作をちょうど 10^{18} 回行います。

操作

  • 3 人の高橋くんのうち 2 人を選ぶ。2 人はお互いのかぶっている帽子を交換する。

10^{18} 回の操作の後、高橋くん i が文字 T_i に対応する色の帽子をかぶっているようにすることはできますか?

制約

  • S_1, S_2, S_3R, G, B の並べ替えである
  • T_1, T_2, T_3R, G, B の並べ替えである

入力

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

S_1 S_2 S_3
T_1 T_2 T_3

出力

10^{18} 回の操作の後、高橋くん i が文字 T_i に対応する色の帽子をかぶっているようにすることはできる場合は Yes を、できない場合は No を出力せよ。


入力例 1

R G B
R G B

出力例 1

Yes

例えば、高橋くん 1 と高橋くん 2 の帽子を交換する操作を 10^{18} 回行うと目的を達成できます。

Score : 400 points

Problem Statement

There are three Takahashis numbered 1, 2 and 3, and three hats colored red, green, and blue. Each Takahashi is wearing one hat. The color of the hat that Takahashi i is currently wearing is represented by a character S_i. Here, R corresponds to red, G to green, and B to blue. Now, they will do the following operation exactly 10^{18} times.

Operation

  • Choose two out of the three Takahashis. The two exchange the hats they are wearing.

Is it possible to make Takahashi i wearing the hat of color corresponding to character T_i after the 10^{18} repetitions?

Constraints

  • S_1, S_2, S_3 are a permutation of R, G, B.
  • T_1, T_2, T_3 are a permutation of R, G, B.

Input

Input is given from Standard Input in the following format:

S_1 S_2 S_3
T_1 T_2 T_3

Output

If it is possible to make Takahashi i wearing the hat of color corresponding to character T_i after the 10^{18} repetitions, print Yes; otherwise, print No.


Sample Input 1

R G B
R G B

Sample Output 1

Yes

For example, the objective can be achieved by repeating 10^{18} times the operation of swapping the hats of Takahashi 1 and Takahashi 2.

E - King Bombee

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点 M 辺の単純無向グラフが与えられます。このグラフの頂点には 1 から N の番号が付けられており、辺には 1 から M の番号が付けられています。辺 i は頂点 U_i と頂点 V_i の間を結んでいます。

整数 K, S, T, X が与えられます。以下の条件を満たす数列 A = (A_0, A_1, \dots, A_K) は何通りありますか?

  • A_i1 以上 N 以下の整数
  • A_0 = S
  • A_K = T
  • 頂点 A_i と頂点 A_{i + 1} の間を直接結ぶ辺が存在する
  • 数列 A の中に整数 X\ (X≠S,X≠T) は偶数回出現する ( 0 回でも良い)

ただし、答えは非常に大きくなることがあるので、答えを 998244353 で割ったあまりを求めてください。

制約

  • 入力は全て整数
  • 2≤N≤2000
  • 1≤M≤2000
  • 1≤K≤2000
  • 1≤S,T,X≤N
  • X≠S
  • X≠T
  • 1≤U_i<V_i≤N
  • i≠j ならば (U_i, V_i)≠(U_j, V_j)

入力

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

N M K S T X
U_1 V_1
U_2 V_2
\vdots
U_M V_M

出力

答えを 998244353 で割ったあまりを出力せよ。


入力例 1

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

出力例 1

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

4 個が条件を満たします。(1, 2, 3, 4, 3)(1, 4, 1, 2, 3)2 が奇数回出現するため、条件を満たしません。


入力例 2

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

出力例 2

0

グラフは連結であるとは限りません。


入力例 3

10 15 20 4 4 6
2 6
2 7
5 7
4 5
2 4
3 7
1 7
1 4
2 9
5 10
1 3
7 8
7 9
1 6
1 2

出力例 3

952504739

998244353 で割ったあまりを求めてください。

Score : 500 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The vertices are numbered from 1 through N, and the edges are numbered from 1 through M. Edge i connects Vertex U_i and Vertex V_i.

You are given integers K, S, T, and X. How many sequences A = (A_0, A_1, \dots, A_K) are there satisfying the following conditions?

  • A_i is an integer between 1 and N (inclusive).
  • A_0 = S
  • A_K = T
  • There is an edge that directly connects Vertex A_i and Vertex A_{i+1}.
  • Integer X\ (X≠S,X≠T) appears even number of times (possibly zero) in sequence A.

Since the answer can be very large, find the answer modulo 998244353.

Constraints

  • All values in input are integers.
  • 2≤N≤2000
  • 1≤M≤2000
  • 1≤K≤2000
  • 1≤S,T,X≤N
  • X≠S
  • X≠T
  • 1≤U_i<V_i≤N
  • If i ≠ j, then (U_i, V_i) ≠ (U_j, V_j).

Input

Input is given from Standard Input in the following format:

N M K S T X
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

Print the answer modulo 998244353.


Sample Input 1

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

Sample Output 1

4

The following 4 sequences satisfy the conditions:

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

On the other hand, (1, 2, 3, 4, 3) and (1, 4, 1, 2, 3) do not, since there are odd number of occurrences of 2.


Sample Input 2

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

Sample Output 2

0

The graph is not necessarily connected.


Sample Input 3

10 15 20 4 4 6
2 6
2 7
5 7
4 5
2 4
3 7
1 7
1 4
2 9
5 10
1 3
7 8
7 9
1 6
1 2

Sample Output 3

952504739

Find the answer modulo 998244353.

F - Shortest Good Path

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 個の頂点と M 本の辺からなる単純(自己ループおよび多重辺を持たない)かつ連結な無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結びます。

下記の 2 つの条件をともに満たす整数列 (A_1, A_2, \ldots, A_k) を長さ kパスと呼びます。

  • すべての i = 1, 2, \dots, k について、1 \leq A_i \leq N
  • すべての i = 1, 2, \ldots, k-1 について、頂点 A_i と頂点 A_{i+1} は辺で直接結ばれている。

空列も長さ 0 のパスとみなします。

S = s_1s_2\ldots s_N01 のみからなる長さ N の文字列とします。 パス A = (A_1, A_2, \ldots, A_k) が下記を満たすとき、パス AS に関する良いパスと呼びます。

  • すべての i = 1, 2, \ldots, N について、次を満たす。
    • s_i = 0 ならば、A に含まれる i の個数は偶数である。
    • s_i = 1 ならば、A に含まれる i の個数は奇数である。

S として考えられる文字列(すなわち、01 のみからなる長さ N の文字列)は 2^N 個ありますが、そのすべてにわたる「 S に関する良いパスのうち最短のものの長さ」の総和を出力してください。

この問題の制約下において、01 からなる長さ N のどのような文字列を S として選んでも、S に関する良いパスが少なくとも 1 つ存在することが示せます。

制約

  • 2 \leq N \leq 17
  • N-1 \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

3 2
1 2
2 3

出力例 1

14
  • S = 000 のとき、空列 ()S に関する最短の良いパスであり、その長さは 0 です。
  • S = 100 のとき、(1)S に関する最短の良いパスであり、その長さは 1 です。
  • S = 010 のとき、(2)S に関する最短の良いパスであり、その長さは 1 です。
  • S = 110 のとき、(1, 2)S に関する最短の良いパスであり、その長さは 2 です。
  • S = 001 のとき、(3)S に関する最短の良いパスであり、その長さは 1 です。
  • S = 101 のとき、(1, 2, 3, 2)S に関する最短の良いパスであり、その長さは 4 です。
  • S = 011 のとき、(2, 3)S に関する最短の良いパスであり、その長さは 2 です。
  • S = 111 のとき、(1, 2, 3)S に関する最短の良いパスであり、その長さは 3 です。

よって、求める答えは 0 + 1 + 1 + 2 + 1 + 4 + 2 + 3 = 14 です。


入力例 2

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

出力例 2

108

Score : 500 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges. (A graph is said to be simple if it has no multi-edges and no self-loops.)
For i = 1, 2, \ldots, M, the i-th edge connects Vertex u_i and Vertex v_i.

A sequence (A_1, A_2, \ldots, A_k) is said to be a path of length k if both of the following two conditions are satisfied:

  • For all i = 1, 2, \dots, k, it holds that 1 \leq A_i \leq N.
  • For all i = 1, 2, \ldots, k-1, Vertex A_i and Vertex A_{i+1} are directly connected by an edge.

An empty sequence is regarded as a path of length 0.

Let S = s_1s_2\ldots s_N be a string of length N consisting of 0 and 1. A path A = (A_1, A_2, \ldots, A_k) is said to be a good path with respect to S if the following conditions are satisfied:

  • For all i = 1, 2, \ldots, N, it holds that:
    • if s_i = 0, then A has even number of i's.
    • if s_i = 1, then A has odd number of i's.

There are 2^N possible S (in other words, there are 2^N strings of length N consisting of 0 and 1). Find the sum of "the length of the shortest good path with respect to S" over all those S.

Under the Constraints of this problem, it can be proved that, for any string S of length N consisting of 0 and 1, there is at least one good path with respect to S.

Constraints

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


Sample Input 1

3 2
1 2
2 3

Sample Output 1

14
  • For S = 000, the empty sequence () is the shortest good path with respect to S, whose length is 0.
  • For S = 100, (1) is the shortest good path with respect to S, whose length is 1.
  • For S = 010, (2) is the shortest good path with respect to S, whose length is 1.
  • For S = 110, (1, 2) is the shortest good path with respect to S, whose length is 2.
  • For S = 001, (3) is the shortest good path with respect to S, whose length is 1.
  • For S = 101, (1, 2, 3, 2) is the shortest good path with respect to S, whose length is 4.
  • For S = 011, (2, 3) is the shortest good path with respect to S, whose length is 2.
  • For S = 111, (1, 2, 3) is the shortest good path with respect to S, whose length is 3.

Therefore, the sought answer is 0 + 1 + 1 + 2 + 1 + 4 + 2 + 3 = 14.


Sample Input 2

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

Sample Output 2

108
G - Construct Good Path

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 個の頂点と M 本の辺からなる単純(自己ループおよび多重辺を持たない)かつ連結な無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結びます。

下記の 2 つの条件をともに満たす整数列 (A_1, A_2, \ldots, A_k) を長さ kパスと呼びます。

  • すべての i = 1, 2, \dots, k について、1 \leq A_i \leq N
  • すべての i = 1, 2, \ldots, k-1 について、頂点 A_i と頂点 A_{i+1} は辺で直接結ばれている。

空列も長さ 0 のパスとみなします。

01 のみからなる長さ N の文字列 S = s_1s_2\ldots s_N が与えられます。 パス A = (A_1, A_2, \ldots, A_k) が下記を満たすとき、パス AS に関する良いパスと呼びます。

  • すべての i = 1, 2, \ldots, N について、次を満たす。
    • s_i = 0 ならば、A に含まれる i の個数は偶数である。
    • s_i = 1 ならば、A に含まれる i の個数は奇数である。

この問題の制約下において、S に関する長さ 4N 以下の良いパスが少なくとも 1 つ存在することが示せます。 S に関する長さ 4N 以下の良いパスを 1 つ出力してください。

制約

  • 2 \leq N \leq 10^5
  • N-1 \leq M \leq \min\lbrace 2 \times 10^5, \frac{N(N-1)}{2}\rbrace
  • 1 \leq u_i, v_i \leq N
  • 与えられるグラフは単純かつ連結
  • N, M, u_i, v_i は整数
  • S01 のみからなる長さ N の文字列

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M
S

出力

S に関する長さ 4N 以下の良いパスを下記の形式にしたがって出力せよ。 すなわち、1 行目にパスの長さ K を出力し、2 行目にパスの各要素を空白区切りで出力せよ。

K
A_1 A_2 \ldots A_K

入力例 1

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

出力例 1

9
2 5 6 5 6 3 1 3 6

パス (2, 5, 6, 5, 6, 3, 1, 3, 6) は、長さが 4N 以下であり、

  • 含まれる 1 の個数は奇数( 1 個)
  • 含まれる 2 の個数は奇数( 1 個)
  • 含まれる 3 の個数は偶数( 2 個)
  • 含まれる 4 の個数は偶数( 0 個)
  • 含まれる 5 の個数は偶数( 2 個)
  • 含まれる 6 の個数は奇数( 3 個)

であるため、S = 110001 に関する良いパスです。


入力例 2

3 3
3 1
3 2
1 2
000

出力例 2

0

空のパス () は、S = 000000 に関する良いパスです。 代わりにパス (1, 2, 3, 1, 2, 3) などを出力しても正解となります。

Score : 600 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges. (A graph is said to be simple if it has no multi-edges and no self-loops.)
For i = 1, 2, \ldots, M, the i-th edge connects Vertex u_i and Vertex v_i.

A sequence (A_1, A_2, \ldots, A_k) is said to be a path of length k if both of the following two conditions are satisfied:

  • For all i = 1, 2, \dots, k, it holds that 1 \leq A_i \leq N.
  • For all i = 1, 2, \ldots, k-1, Vertex A_i and Vertex A_{i+1} are directly connected with an edge.

An empty sequence is regarded as a path of length 0.

You are given a sting S = s_1s_2\ldots s_N of length N consisting of 0 and 1. A path A = (A_1, A_2, \ldots, A_k) is said to be a good path with respect to S if the following conditions are satisfied:

  • For all i = 1, 2, \ldots, N, it holds that:
    • if s_i = 0, then A has even number of i's.
    • if s_i = 1, then A has odd number of i's.

Under the Constraints of this problem, it can be proved that there is at least one good path with respect to S of length at most 4N. Print a good path with respect to S of length at most 4N.

Constraints

  • 2 \leq N \leq 10^5
  • N-1 \leq M \leq \min\lbrace 2 \times 10^5, \frac{N(N-1)}{2}\rbrace
  • 1 \leq u_i, v_i \leq N
  • The given graph is simple and connected.
  • N, M, u_i, and v_i are integers.
  • S is a string of length N consisting of 0 and 1.

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
S

Output

Print a good path with respect to S of length at most 4N in the following format. Specifically, the first line should contain the length K of the path, and the second line should contain the elements of the path, with spaces in between.

K
A_1 A_2 \ldots A_K

Sample Input 1

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

Sample Output 1

9
2 5 6 5 6 3 1 3 6

The path (2, 5, 6, 5, 6, 3, 1, 3, 6) has a length no greater than 4N, and

  • it has odd number (1) of 1
  • it has odd number (1) of 2
  • it has even number (2) of 3
  • it has even number (0) of 4
  • it has even number (2) of 5
  • it has odd number (3) of 6

so it is a good path with respect to S = 110001.


Sample Input 2

3 3
3 1
3 2
1 2
000

Sample Output 2

0

An empty path () is a good path with respect to S = 000000. Alternatively, paths like (1, 2, 3, 1, 2, 3) are also accepted.

Ex - Linear Maximization

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

2 次元平面上の点の集合 S があります。S ははじめ空です。

i = 1, 2, \dots, Q の順に、以下のクエリを処理してください。

  • 整数 X_i, Y_i, A_i, B_i が与えられる。S に点 (X_i, Y_i) を追加した後、\displaystyle \max_{(x,y) \in S}\left\{A_ix + B_iy\right\} を求める。

制約

  • 入力は全て整数
  • 1≤Q≤2 \times 10^5
  • |X_i|, |Y_i|, |A_i|, |B_i|≤10^9
  • i ≠ j ならば (X_i, Y_i)≠(X_j, Y_j)

入力

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

Q
X_1 Y_1 A_1 B_1
X_2 Y_2 A_2 B_2
\vdots
X_Q Y_Q A_Q B_Q

出力

Q 行出力せよ。i 行目には、i 個目のクエリに対する答えを出力せよ。


入力例 1

4
1 0 -1 -1
0 1 2 0
-1 0 1 1
0 -1 1 -2

出力例 1

-1
2
1
2
  • i = 1 のとき : S に点 (1, 0) を追加し、S = \{(1, 0)\} とします。(x, y) = (1, 0) のとき -x - y = -1 となり、これが最大値を取ります。
  • i = 2 のとき : S に点 (0, 1) を追加し、S = \{(0, 1), (1, 0)\} とします。(x, y) = (1, 0) のとき 2x = 2 となり、これが最大値を取ります。
  • i = 3 のとき : S に点 (-1, 0) を追加し、S = \{(-1, 0), (0, 1), (1, 0)\} とします。(x, y) = (1, 0) または (x, y) = (0, 1) のとき x + y = 1 となり、これが最大値を取ります。
  • i = 4 のとき : S に点 (0, -1) を追加し、S = \{(-1, 0), (0, -1), (0, 1), (1, 0)\} とします。(x, y) = (0, -1) のとき x - 2y = 2 となり、これが最大値を取ります。

入力例 2

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

出力例 2

0
35
31
21
36
87
0
36
31

Score : 600 points

Problem Statement

There is a set S of points on a two-dimensional plane. S is initially empty.

For each i = 1, 2, \dots, Q in this order, process the following query.

  • You are given integers X_i, Y_i, A_i, and B_i. Add point (X_i, Y_i) to S, and then find \displaystyle \max_{(x,y) \in S}\left\{A_ix + B_iy\right\}.

Constraints

  • All values in input are integers.
  • 1≤Q≤2 \times 10^5
  • |X_i|, |Y_i|, |A_i|, |B_i| ≤10^9
  • If i ≠ j, then (X_i, Y_i) ≠ (X_j, Y_j).

Input

Input is given from Standard Input in the following format:

Q
X_1 Y_1 A_1 B_1
X_2 Y_2 A_2 B_2
\vdots
X_Q Y_Q A_Q B_Q

Output

Print Q lines. The i-th line should contain the answer for the i-th query.


Sample Input 1

4
1 0 -1 -1
0 1 2 0
-1 0 1 1
0 -1 1 -2

Sample Output 1

-1
2
1
2
  • When i = 1: add point (1, 0) to S, then it will become S = \{(1, 0)\}. For (x, y) = (1, 0), we have -x - y = -1, which is the maximum.
  • When i = 2: add point (0, 1) to S, then it will become S = \{(0, 1), (1, 0)\}. For (x, y) = (1, 0), we have 2x = 2, which is the maximum.
  • When i = 3: add point (-1, 0) to S, then it will become S = \{(-1, 0), (0, 1), (1, 0)\}. For (x, y) = (1, 0) or (x, y) = (0, 1), we have x + y = 1, which is the maximum.
  • When i = 4: add point (0, -1) to S, then it will become S = \{(-1, 0), (0, -1), (0, 1), (1, 0)\}. For (x, y) = (0, -1), we have x - 2y = 2, which is the maximum.

Sample Input 2

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

Sample Output 2

0
35
31
21
36
87
0
36
31