A - Range Swap

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

長さ N の数列 A=(A_1,A_2,\ldots,A_N) および正整数 P,Q,R,S が与えられます。
ここで、P,Q,R,S は、1\leq P\leq Q<R\leq S \leq N および Q-P=S-R をみたしています。

数列 AP 番目から Q 番目の項までと R 番目から S 番目の項までを入れ替えた数列を B=(B_1, B_2,\ldots, B_N) とします。
数列 B を出力してください。

制約

  • 1\leq N \leq 100
  • 1\leq A_i\leq 100
  • 1\leq P\leq Q<R\leq S \leq N
  • Q-P=S-R
  • 入力はすべて整数

入力

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

N P Q R S
A_1 A_2 \ldots A_N

出力

B_1, B_2,\ldots, B_N を空白区切りで出力せよ。


入力例 1

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

出力例 1

5 6 7 4 1 2 3 8

数列 A=(1,2,3,4,5,6,7,8)1 番目から 3 番目の項 (1,2,3)5 番目から 7 番目までの項 (5,6,7) を 入れ替えると, B=(5,6,7,4,1,2,3,8) となります。 よってこれを空白区切りで出力します。


入力例 2

5 2 3 4 5
2 2 1 1 1

出力例 2

2 1 1 2 1

数列には同じ整数が複数回現れる事もあります。


入力例 3

2 1 1 2 2
50 100

出力例 3

100 50

入力例 4

10 2 4 7 9
22 75 26 45 72 81 47 29 97 2

出力例 4

22 47 29 97 72 81 75 26 45 2

Score : 100 points

Problem Statement

You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N and positive integers P,Q,R, and S.
Here, P,Q,R, and S satisfy 1\leq P\leq Q<R\leq S \leq N and Q-P=S-R.

Let B=(B_1, B_2,\ldots, B_N) be the sequence obtained by swapping the P-th through Q-th terms and the R-th through S-th terms of A.
Print the sequence B.

Constraints

  • 1\leq N \leq 100
  • 1\leq A_i\leq 100
  • 1\leq P\leq Q<R\leq S \leq N
  • Q-P=S-R
  • All values in the input are integers.

Input

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

N P Q R S
A_1 A_2 \ldots A_N

Output

Print B_1, B_2,\ldots, B_N, with spaces in between.


Sample Input 1

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

Sample Output 1

5 6 7 4 1 2 3 8

Swapping the 1-st through 3-rd terms (1,2,3) and the 5-th through 7-th terms (5,6,7) of the sequence A=(1,2,3,4,5,6,7,8) results in B=(5,6,7,4,1,2,3,8), which should be printed with spaces in between.


Sample Input 2

5 2 3 4 5
2 2 1 1 1

Sample Output 2

2 1 1 2 1

The same integer may occur multiple times in the sequence.


Sample Input 3

2 1 1 2 2
50 100

Sample Output 3

100 50

Sample Input 4

10 2 4 7 9
22 75 26 45 72 81 47 29 97 2

Sample Output 4

22 47 29 97 72 81 75 26 45 2
B - Cat

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

長さ N の文字列 S が与えられます。

S に連続して含まれる na を全て nya に置き換えて得られる文字列を答えてください。

制約

  • N1 以上 1000 以下の整数
  • S は英小文字からなる長さ N の文字列

入力

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

N
S

出力

答えを出力せよ。


入力例 1

4
naan

出力例 1

nyaan

naan に連続して含まれる na を全て nya に置き換えて得られる文字列は nyaan です。


入力例 2

4
near

出力例 2

near

Sna が連続して含まれないこともあります。


入力例 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
C - Rotate and Palindrome

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

長さ N の文字列 S が与えられます。S_i\ (1\leq i \leq N)S の左から i 番目の文字とします。

あなたは以下の 2 種類の操作を好きな順番で 0 回以上好きな回数行うことができます。

  • A 円払う。 S の左端の文字を右端に移動する。すなわち、S_1S_2\ldots S_NS_2\ldots S_NS_1 に変える。

  • B 円払う。 1 以上 N 以下の整数 i を選び、 S_i を好きな英小文字で置き換える。

S を回文にするためには最低で何円必要ですか?

回文とは ある文字列 T について、 T の長さを |T| として、全ての整数 i (1 \le i \le |T|) について、 T の前から i 文字目と後ろから i 文字目が同じであるとき、またそのときに限って、 T は回文です。

制約

  • 1\leq N \leq 5000
  • 1\leq A,B\leq 10^9
  • S は英小文字からなる長さ N の文字列
  • S 以外の入力は全て整数

入力

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

N A B
S

出力

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


入力例 1

5 1 2
rrefa

出力例 1

3

最初に 2 番目の操作を 1 回行います。2 円払い、i=5 として S_5e で置き換えます。 Srrefe となります。

次に 1 番目の操作を 1 回行います。1 円払い、Srefer となります。これは回文です。

よって 3 円払うことで S を回文にすることができました。 2 円以下払うことで S を回文にすることは不可能なので、これが答えです。


入力例 2

8 1000000000 1000000000
bcdfcgaa

出力例 2

4000000000

答えは 32 bit 整数に収まらない場合があることに注意してください。

Score : 300 points

Problem Statement

You are given a string S of length N. Let S_i\ (1\leq i \leq N) be the i-th character of S from the left.

You may perform the following two kinds of operations zero or more times in any order:

  • Pay A yen (the currency in Japan). Move the leftmost character of S to the right end. In other words, change S_1S_2\ldots S_N to S_2\ldots S_NS_1.

  • Pay B yen. Choose an integer i between 1 and N, and replace S_i with any lowercase English letter.

How many yen do you need to pay to make S a palindrome?

What is a palindrome? A string T is a palindrome if and only if the i-th character from the left and the i-th character from the right are the same for all integers i (1 \le i \le |T|), where |T| is the length of T.

Constraints

  • 1\leq N \leq 5000
  • 1\leq A,B\leq 10^9
  • S is a string of length N consisting of lowercase English letters.
  • All values in the input except for S are integers.

Input

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

N A B
S

Output

Print the answer as an integer.


Sample Input 1

5 1 2
rrefa

Sample Output 1

3

First, pay 2 yen to perform the operation of the second kind once: let i=5 to replace S_5 with e. S is now rrefe.

Then, pay 1 yen to perform the operation of the first kind once. S is now refer, which is a palindrome.

Thus, you can make S a palindrome for 3 yen. Since you cannot make S a palindrome for 2 yen or less, 3 is the answer.


Sample Input 2

8 1000000000 1000000000
bcdfcgaa

Sample Output 2

4000000000

Note that the answer may not fit into a 32-bit integer type.

D - Money in Hand

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋君は N 種類の硬貨をそれぞれ何枚か持っており、 具体的には、1\leq i\leq N について A_i 円硬貨を B_i 枚持っています。

高橋君が現在持っている硬貨を用いて、(お釣りが出ないように)ちょうど X 円を支払うことができるか判定してください。

制約

  • 1\leq N\leq 50
  • 1\leq X\leq 10^4
  • 1\leq A_i\leq 100
  • 1\leq B_i\leq 50
  • A_i はすべて異なる。
  • 入力はすべて整数

入力

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

N X
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

高橋君が現在持っている硬貨を用いてちょうど X 円を支払うことができる場合は Yes を、 できない場合は No を出力せよ。


入力例 1

2 19
2 3
5 6

出力例 1

Yes

高橋君は 2 円硬貨を 3 枚、5 円硬貨を 6 枚持っています。 このうち、2 円硬貨を 2 枚、5 円硬貨を 3 枚用いることでちょうど 2\times 2+5\times 3=19 円を支払うことができます。 よって、Yes を出力します。


入力例 2

2 18
2 3
5 6

出力例 2

No

持っている硬貨をどのように組み合わせてもちょうど 18 円を支払うことはできません。 よって、No を出力します。


入力例 3

3 1001
1 1
2 1
100 10

出力例 3

Yes

1 枚も使用しない硬貨が存在しても構いません。

Score : 400 points

Problem Statement

Takahashi has N kinds of coins; specifically, for 1\leq i\leq N, he has B_i coins worth A_i yen (the currency in Japan) each.

Determine if Takahashi can pay exactly X yen (without change) with the coins he currently has.

Constraints

  • 1\leq N\leq 50
  • 1\leq X\leq 10^4
  • 1\leq A_i\leq 100
  • 1\leq B_i\leq 50
  • A_i are pairwise distinct.
  • All values in the input are integers.

Input

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

N X
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print Yes if Takahashi can pay exactly X yen with the coins he currently has; print No otherwise.


Sample Input 1

2 19
2 3
5 6

Sample Output 1

Yes

Takahashi has three 2-yen coins and six 5-yen coins. He can use two 2-yen coins and three 5-yen coins to pay exactly 2\times 2+5\times 3=19 yen. Thus, Yes should be printed.


Sample Input 2

2 18
2 3
5 6

Sample Output 2

No

There is no combination of the coins that he can use to pay exactly 18 yen. Thus, No should be printed.


Sample Input 3

3 1001
1 1
2 1
100 10

Sample Output 3

Yes

He need not use all kinds of coins.

E - Souvenir

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 個の都市があり、いくつかの相異なる都市の間は一方通行の直行便によって移動することができます。
どの直行便が存在するかは N 個の長さ N の文字列 S_1,S_2,\ldots,S_N によって表され、 S_ij 文字目が Y のとき都市 i から都市 j へ向かう直行便が存在することを、 N のとき存在しないことを示します。
また、各都市ではお土産が 1 つずつ売られており、都市 i では価値 A_i のお土産が売られています。

次のような問題を考えます。

高橋君は今都市 S におり、いくつかの直行便を乗り継いで(S とは異なる)都市 T に向かおうとしています。
さらに、高橋君は移動する途中で訪れた都市(S,T を含む)において 1 つずつお土産を購入します。
都市 S から都市 T へ向かう経路が複数存在する時、高橋君は次のような経路で移動することを考えています。

  • 都市 S から 都市 T に向かう経路のうち、使う直行便の数が最小である。
  • さらにそのような経路のうち、購入するお土産の価値の総和が最大である。

高橋君が都市 S から T に直行便を乗り継いで移動することが可能か判定し、 可能な時は高橋君が上の条件をみたすように経路を選んだ時の「使用する直行便の本数」と「お土産の価値の総和」を求めよ。

相異なる都市の組 (U_i,V_i)Q 個与えられます。
1\leq i\leq Q について、S=U_i, T=V_i とした時の上記の問題の答えを出力してください。

制約

  • 2 \leq N \leq 300
  • 1\leq A_i\leq 10^9
  • S_iYN のみからなる長さ N の文字列
  • S_ii 文字目は N
  • 1\leq Q\leq N(N-1)
  • 1\leq U_i,V_i\leq N
  • U_i\neq V_i
  • i\neq j ならば (U_i,V_i)\neq (U_j,V_J)
  • N,A_i,Q,U_i,V_i は全て整数

入力

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

N
A_1 A_2 \ldots A_N
S_1
S_2
\vdots
S_N
Q
U_1 V_1
U_2 V_2
\vdots
U_Q V_Q

出力

Q 行出力せよ。
i (1\leq i\leq Q) 行目には、 都市 U_i から都市 V_i に移動することが不可能な時は Impossible を、 可能な時は上記のように経路を選んだ時の「使用する直行便の本数」と「お土産の価値の総和」をこの順に空白区切りで出力せよ。


入力例 1

5
30 50 70 20 60
NYYNN
NNYNN
NNNYY
YNNNN
YNNNN
3
1 3
3 1
4 5

出力例 1

1 100
2 160
3 180

(S,T)=(U_1,V_1)=(1,3) の時について、都市 1 から都市 3 への直行便が存在するため、 使用する直行便としてあり得る最小の値はその直行便を使用した時の 1 本となります。 この時、お土産の価値の総和は A_1+A_3=30+70=100 となります。

(S,T)=(U_2,V_2)=(3,1) の時について、使用する直行便としてあり得る最小の値は 2 本で、 最小値を達成する経路としては都市 3\to 4\to 1 と都市 3\to 5\to 12 通りが考えられます。 それぞれの経路の時のお土産の価値の総和は 70+20+30=120, 70+60+30=160 なので、 高橋君は後者の経路を選び、この時お土産の価値の総和は 160 となります。

(S,T)=(U_3,V_3)=(4,5) の時について、都市 4\to 1\to 3\to 5 と進んだ時に使用する直行便の本数は最小となり、 この時お土産の価値の総和は 20+30+70+60=180 となります。


入力例 2

2
100 100
NN
NN
1
1 2

出力例 2

Impossible

直行便が全く存在しないこともあります。

Score : 500 points

Problem Statement

There are N cities. There are also one-way direct flights that connect different cities.
The availability of direct flights is represented by N strings S_1,S_2,\ldots,S_N of length N each. If the j-th character of S_i is Y, there is a direct flight from city i to city j; if it is N, there is not.
Each city sells a souvenir; city i sells a souvenir of value A_i.

Consider the following problem:

Takahashi is currently at city S and wants to get to city T (that is different from city S) using some direct flights.
Every time he visits a city (including S and T), he buys a souvenir there.
If there are multiple routes from city S to city T, Takahashi decides the route as follows:

  • He tries to minimize the number of direct flights in the route from city S to city T.
  • Then he tries to maximize the total value of the souvenirs he buys.

Determine if he can travel from city S to city T using the direct flights. If he can, find the "number of direct flights" and "total value of souvenirs" in the route that satisfies the conditions above.

You are given Q pairs (U_i,V_i) of distinct cities.
For each 1\leq i\leq Q, print the answer to the problem above when S=U_i and T=V_i.

Constraints

  • 2 \leq N \leq 300
  • 1\leq A_i\leq 10^9
  • S_i is a string of length N consisting of Y and N.
  • The i-th character of S_i is N.
  • 1\leq Q\leq N(N-1)
  • 1\leq U_i,V_i\leq N
  • U_i\neq V_i
  • If i \neq j, then (U_i,V_i)\neq (U_j,V_J).
  • N,A_i,Q,U_i, and V_i are all integers.

Input

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

N
A_1 A_2 \ldots A_N
S_1
S_2
\vdots
S_N
Q
U_1 V_1
U_2 V_2
\vdots
U_Q V_Q

Output

Print Q lines.
The i-th (1\leq i\leq Q) line should contain Impossible if he cannot travel from city U_i to city V_i; if he can, the line should contain "the number of direct flights" and "the total value of souvenirs" in the route chosen as described above, in this order, separated by a space.


Sample Input 1

5
30 50 70 20 60
NYYNN
NNYNN
NNNYY
YNNNN
YNNNN
3
1 3
3 1
4 5

Sample Output 1

1 100
2 160
3 180

For (S,T)=(U_1,V_1)=(1,3), there is a direct flight from city 1 to city 3, so the minimum possible number of direct flights is 1, which is achieved when he uses that direct flight. In this case, the total value of the souvenirs is A_1+A_3=30+70=100.

For (S,T)=(U_2,V_2)=(3,1), the minimum possible number of direct flights is 2. The following two routes achieve the minimum: cities 3\to 4\to 1, and cities 3\to 5\to 1. Since the total value of souvenirs in the two routes are 70+20+30=120 and 70+60+30=160, respectively, so he chooses the latter route, and the total value of the souvenirs is 160.

For (S,T)=(U_3,V_3)=(4,5), the number of direct flights is minimum when he travels cities 4\to 1\to 3\to 5, where the total value of the souvenirs is 20+30+70+60=180.


Sample Input 2

2
100 100
NN
NN
1
1 2

Sample Output 2

Impossible

There may be no direct flight at all.

F - Guess The Number 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

この問題は インタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。

あなたとジャッジは下記の手順を行います。 手順はフェイズ 1 とフェイズ 2 からなり、まずフェイズ 1 を行った直後、続けてフェイズ 2 を行います。

(フェイズ 1

  • ジャッジが 1 以上 10^9 以下の整数 N を決める。この整数は隠されている。
  • あなたは 1 以上 110 以下の整数 M を出力する。
  • さらにあなたは、すべての i = 1, 2, \ldots, M について 1 \leq A_i \leq M を満たす、長さ M の整数列 A=(A_1,A_2,\ldots,A_M) を出力する。

(フェイズ 2

  • ジャッジから、長さ M の整数列 B=(B_1,B_2,\ldots,B_M) が与えられる。ここで、 B_i = f^N(i) である。 f(i)1 以上 M 以下の整数 i に対し f(i)=A_i で定められ、 f^N(i)if(i) で置き換える操作を N 回行った際に得られる整数である。
  • あなたは、B の情報から、ジャッジが決めた整数 N を特定し、N を出力する。

上記の手順を行った後、直ちにプログラムを終了することで正解となります。

制約

  • N1 以上 10^9 以下の整数

入出力

この問題はインタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。

(フェイズ 1

  • まず、1 以上 110 以下の整数 M を出力してください。出力後、必ず改行してください。
M
  • その後、空白区切りで 1 以上 M 以下の整数からなる長さ M の整数列 A=(A_1,A_2,\ldots,A_M) を出力してください。出力後、必ず改行してください。
A_1 A_2 \ldots A_M

(フェイズ 2

  • まず、長さ M の整数列 B=(B_1,B_2,\ldots,B_M) が入力から与えられます。
B_1 B_2 \ldots B_M
  • 整数 N を求め、出力してください。出力後、必ず改行してください。
N

不正な出力がなされた場合、ジャッジは -1 を出力します。この時、提出はすでに不正解と判定されています。ジャッジプログラムはこの時点で終了するため、あなたのプログラムも終了するのが望ましいです。

注意点

  • 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 対話の途中で不正な出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
  • 答えを出力したら(または -1 を受け取ったら)直ちにプログラムを正常終了してください。そうしなかった場合、ジャッジ結果は不定です。
  • 特に、余計な改行も不正なフォーマットの出力とみなされるので注意してください。

入出力例

以下は、N = 2 の場合の入出力例です。

入力 出力 説明
ジャッジは N=2 と決めました。N は入力としては与えられず、隠されています。
4 M を出力します。
2 3 4 4 A=(2,3,4,4) を出力します。
3 4 4 4 f^2(1)=3,f^2(2)=4,f^2(3)=4,f^2(4)=4 なので、ジャッジは B=(3,4,4,4) をあなたのプログラムに与えます。
2 B から N=2 であると特定しました。 N を出力し、プログラムを正常終了させてください。

Score : 500 points

Problem Statement

This is an interactive task, where your and the judge's programs interact via Standard Input and Output.

You and the judge will follow the procedure below. The procedure consists of phases 1 and 2; phase 1 is immediately followed by phase 2.

(Phase 1)

  • The judge decides an integer N between 1 and 10^9 (inclusive), which is hidden.
  • You print an integer M between 1 and 110 (inclusive).
  • You also print an integer sequence A=(A_1,A_2,\ldots,A_M) of length M such that 1 \leq A_i \leq M for all i = 1, 2, \ldots, M.

(Phase 2)

  • The judge gives you an integer sequence B=(B_1,B_2,\ldots,B_M) of length M. Here, B_i = f^N(i). f(i) is defined by f(i)=A_i for all integers i between 1 and M (inclusive), and f^N(i) is the integer resulting from replacing i with f(i) N times.
  • Based on the given B, you identify the integer N that the judge has decided, and print N.

After the procedure above, terminate the program immediately to be judged correct.

Constraints

  • N is an integer between 1 and 10^9 (inclusive).

Input and Output

This is an interactive task, where your and the judge's programs interact via Standard Input and Output.

(Phase 1)

  • First, print an integer M between 1 and 110 (inclusive). It must be followed by a newline.
M
  • Then, print a sequence A=(A_1,A_2,\ldots,A_M) of length M consisting of integers between 1 and M (inclusive), with spaces in between. It must be followed by a newline.
A_1 A_2 \ldots A_M

(Phase 2)

  • First, an integer sequence B=(B_1,B_2,\ldots,B_M) of length M is given from the input.
B_1 B_2 \ldots B_M
  • Find the integer N and print it. It must be followed by a newline.
N

If you print something illegal, the judge prints -1. In that case, your submission is already considered incorrect. Since the judge program terminates at this point, it is desirable that your program terminates too.

Notes

  • After each output, add a newline and then flush Standard Output. Otherwise, you may get a TLE verdict.
  • If an invalid output is printed during the interaction, or if the program terminates halfway, the verdict will be indeterminate.
  • After you print the answer (or you receive -1), immediately terminate the program normally. Otherwise, the verdict will be indeterminate.
  • Note that an excessive newline is also considered an invalid input.

Sample Interaction

The following is a sample interaction with N = 2.

Input Output Description
The judge has decided that N=2. N is hidden: it is not given as an input.
4 You print M.
2 3 4 4 You print A=(2,3,4,4).
3 4 4 4 f^2(1)=3,f^2(2)=4,f^2(3)=4, and f^2(4)=4, so the judge gives B=(3,4,4,4) to your program.
2 Based on B, you have identified that N=2. Print N and terminate the program normally.
G - Unique Walk

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 頂点 M 辺の単純連結無向グラフ G が与えられます。
G の頂点は頂点 1 , 頂点 2, \ldots,頂点 N、辺は辺 1 , 辺 2, \ldots,辺 M と番号づけられており、 辺 i は、頂点 U_i と頂点 V_i を結んでいます。
また、辺の部分集合 S=\{x_1,x_2,\ldots,x_K\} が与えられます。

G 上の歩道であって、任意の x\in S について、辺 x をちょうど 1 回ずつ通るようなものが存在するか判定してください。
S に含まれていない辺は何回(0 回でも良い)通っていてもかまいません。

歩道とは 無向グラフ G 上の歩道とは、k 個 (k は正整数) の頂点と k-1 個の辺を交互に並べた列 v_1,e_1,v_2,\ldots,v_{k-1},e_{k-1},v_k であって、 辺 e_i が頂点 v_i と頂点 v_{i+1} を結んでいるようなものを指す。列の中に同じ頂点や同じ辺が何回登場しても良い。 歩道が辺 x をちょうど 1 回通るとは、e_i=x となるような 1\leq i\leq k-1 がただ 1 つ存在することをいう。

制約

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq \min(\frac{N(N-1)}{2},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 は連結
  • 1 \leq K \leq M
  • 1 \leq x_1<x_2<\cdots<x_K \leq M
  • 入力はすべて整数

入力

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

N M
U_1 V_1
U_2 V_2
\vdots
U_M V_M
K
x_1 x_2 \ldots x_K

出力

問題文の条件をみたす歩道が存在するならば Yes を、存在しないならば No を出力せよ。


入力例 1

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

出力例 1

Yes

頂点 iv_i, 辺 je_j と表すことにすると、 (v_1,e_1,v_3,e_3,v_4,e_4,v_5,e_6,v_6,e_5,v_4,e_3,v_3,e_2,v_2) で表される歩道が条件をみたします。
すなわち、G 上を頂点1\to 3\to 4\to 5\to 6\to 4\to 3\to 2 の順に移動するような歩道です。
この歩道は、辺 1,2,4,5 をいずれもちょうど 1 回ずつ通っているため条件をみたします。


入力例 2

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

出力例 2

No

1,2,3 をちょうど 1 回ずつ通るような歩道は存在しないため、No を出力します。

Score : 600 points

Problem Statement

You are given a simple connected undirected graph G with N vertices and M edges.
The vertices of G are numbered vertex 1, vertex 2, \ldots, and vertex N, and its edges are numbered edge 1, edge 2, \ldots, and edge M. Edge i connects vertex U_i and vertex V_i.
You are also given a subset of the edges: S=\{x_1,x_2,\ldots,x_K\}.

Determine if there is a walk on G that contains edge x exactly once for all x \in S.
The walk may contain an edge not in S any number of times (possibly zero).

What is a walk? A walk on an undirected graph G is a sequence consisting of k vertices (k is a positive integer) and (k-1) edges occurring alternately, v_1,e_1,v_2,\ldots,v_{k-1},e_{k-1},v_k, such that edge e_i connects vertex v_i and vertex v_{i+1}. The sequence may contain the same edge or vertex multiple times. A walk is said to contain an edge x exactly once if and only if there is exactly one 1\leq i\leq k-1 such that e_i=x.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq \min(\frac{N(N-1)}{2},2\times 10^5)
  • 1 \leq U_i<V_i\leq N
  • If i\neq j, then (U_i,V_i)\neq (U_j,V_j) .
  • G is connected.
  • 1 \leq K \leq M
  • 1 \leq x_1<x_2<\cdots<x_K \leq M
  • 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
K
x_1 x_2 \ldots x_K

Output

Print Yes if there is a walk satisfying the condition in the Problem Statement; print No otherwise.


Sample Input 1

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

Sample Output 1

Yes

The walk (v_1,e_1,v_3,e_3,v_4,e_4,v_5,e_6,v_6,e_5,v_4,e_3,v_3,e_2,v_2) satisfies the condition, where v_i denotes vertex i and e_i denotes edge i.
In other words, the walk travels the vertices on G in this order: 1\to 3\to 4\to 5\to 6\to 4\to 3\to 2.
This walk satisfies the condition because it contains edges 1, 2, 4, and 5 exactly once each.


Sample Input 2

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

Sample Output 2

No

There is no walk that contains edges 1, 2, and 3 exactly once each, so No should be printed.

Ex - Don't Swim

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

二次元平面上に N 頂点の凸多角形 C 、点 S=(s_x, s_y), T=(t_x,t_y) があります。 C の頂点は、反時計回りに (x_1,y_1),(x_2,y_2),\ldots,(x_N,y_N) です。 S,TC の外部にあることが保証されています。

C の外周を除いた内部を通らずに点 S から点 T まで移動するときの最短距離を求めてください。

制約

  • 3\leq N \leq 10^5
  • |x_i|,|y_i|,|s_x|,|s_y|,|t_x|,|t_y|\leq 10^9
  • (x_1,y_1),(x_2,y_2),\ldots,(x_N,y_N) は反時計回りに凸多角形をなす
  • C のどの 3 点も同一直線上にない
  • S,TC の外部に存在し、C の外周上にない
  • 入力は全て整数

入力

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

N 
x_1 y_1
x_2 y_2
\vdots
x_N y_N
s_x s_y
t_x t_y

出力

答えを出力せよ。

なお、真の値との絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われる。


入力例 1

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

出力例 1

5.65028153987288474496

最短距離を達成する移動方法の一例を以下の図で示します。

image

(0,2) \to (1,3) \to(3,3)\to(5,2) と移動すると、点 S から点 T への移動距離が 5.650281... となり、これが最小であることが証明できます。 C の外周上は通れることに注意してください。

なお、絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われるので、5.650287 などと出力しても正解と判定されます。


入力例 2

3
0 0
2 0
1 10
3 7
10 3

出力例 2

8.06225774829854965279

Score : 600 points

Problem Statement

On a two-dimensional plane, there is a convex polygon C with N vertices, and points S=(s_x, s_y) and T=(t_x,t_y). The vertices of C are (x_1,y_1),(x_2,y_2),\ldots, and (x_N,y_N) in counterclockwise order. It is guaranteed that S and T are outside the polygon.

Find the shortest distance that needs to be traveled to get from point S to point T without entering the interior of C except for its circumference.

Constraints

  • 3\leq N \leq 10^5
  • |x_i|,|y_i|,|s_x|,|s_y|,|t_x|,|t_y|\leq 10^9
  • (x_1,y_1),(x_2,y_2),\ldots, and (x_N,y_N) form a convex polygon in counterclockwise order.
  • No three points of C are colinear.
  • S and T are outside C and not on the circumference of C.
  • All values in the input are integers.

Input

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

N 
x_1 y_1
x_2 y_2
\vdots
x_N y_N
s_x s_y
t_x t_y

Output

Print the answer.

Your output is considered correct if the absolute or relative error from the true value is at most 10^{-6}.


Sample Input 1

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

Sample Output 1

5.65028153987288474496

One way to achieve the shortest distance is shown in the following figure.

image

If you travel as (0,2) \to (1,3) \to(3,3)\to(5,2), the length of the path from point S to point T is 5.650281.... We can prove that it is the minimum. Note that you may enter the circumference of C.

Note that your output is considered correct if the absolute or relative error is at most 10^{-6}. For example, output like 5.650287 is also considered correct.


Sample Input 2

3
0 0
2 0
1 10
3 7
10 3

Sample Output 2

8.06225774829854965279