A - Six Characters

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

英小文字からなる文字列 S が与えられます。 S の長さは 1 以上かつ 3 以下です。

S を繰り返して得られる文字列であって、長さが 6 のものを出力してください。

本問題の制約下で、そのような文字列はただ一つ存在することが示せます。

制約

  • S は英小文字からなる長さ 1 以上 3 以下の文字列

入力

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

S

出力

答えとなる長さ 6 の文字列を出力せよ。


入力例 1

abc

出力例 1

abcabc

S = abc を繰り返してできる文字列として、abcabcabcabcabcabcabcabcabcabc などがあります。 そのうち、長さが 6 のものは abcabc です。よって、abcabc と出力します。


入力例 2

zz

出力例 2

zzzzzz

Score : 100 points

Problem Statement

You are given a string S consisting of lowercase English characters. The length of S is between 1 and 3, inclusive.

Print the string of length 6 that is a repetition of S.

It can be shown that there uniquely exists such a string under the Constraints of this problem.

Constraints

  • S is a string consisting of lowercase English characters of length between 1 and 3, inclusive.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer string, which is of length 6.


Sample Input 1

abc

Sample Output 1

abcabc

These are strings that are repetitions of S = abc: abc, abcabc, abcabcabc, abcabcabcabc, and so on. Among them, abcabc has the length of 6, so abcabc should be printed.


Sample Input 2

zz

Sample Output 2

zzzzzz
B - At Most 3 (Judge ver.)

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

おもり 1, おもり 2, \dots, おもり NN 個のおもりがあります。おもり i の重さは A_i です。
以下の条件を満たす正整数 n良い整数 と呼びます。

  • \bf{3} 個以下 の異なるおもりを自由に選んで、選んだおもりの重さの和を n にすることができる。

W 以下の正整数のうち、良い整数は何個ありますか?

制約

  • 1 \leq N \leq 300
  • 1 \leq W \leq 10^6
  • 1 \leq A_i \leq 10^6
  • 入力される値はすべて整数

入力

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

N W
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

2 10
1 3

出力例 1

3

おもり 1 のみを選ぶと重さの和は 1 になります。よって 1 は良い整数です。
おもり 2 のみを選ぶと重さの和は 3 になります。よって 3 は良い整数です。
おもり 1 とおもり 2 を選ぶと重さの和は 4 になります。よって 4 は良い整数です。
これら以外に良い整数は存在しません。また、1,3,4 のいずれも W 以下の整数です。よって答えは 3 個になります。


入力例 2

2 1
2 3

出力例 2

0

W 以下の良い整数は存在しません。


入力例 3

4 12
3 3 3 3

出力例 3

3

良い整数は 3,6,93 個です。
たとえばおもり 1, おもり 2, おもり 3 を選ぶと重さの和は 9 になるので、9 は良い整数です。
12 は良い整数 ではない ことに注意してください。


入力例 4

7 251
202 20 5 1 4 2 100

出力例 4

48

Score : 200 points

Problem Statement

There are N weights called Weight 1, Weight 2, \dots, Weight N. Weight i has a mass of A_i.
Let us say a positive integer n is a good integer if the following condition is satisfied:

  • We can choose at most 3 different weights so that they have a total mass of n.

How many positive integers less than or equal to W are good integers?

Constraints

  • 1 \leq N \leq 300
  • 1 \leq W \leq 10^6
  • 1 \leq A_i \leq 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N W
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

2 10
1 3

Sample Output 1

3

If we choose only Weight 1, it has a total mass of 1, so 1 is a good integer.
If we choose only Weight 2, it has a total mass of 3, so 3 is a good integer.
If we choose Weights 1 and 2, they have a total mass of 4, so 4 is a good integer.
No other integer is a good integer. Also, all of 1, 3, and 4 are integers less than or equal to W. Therefore, the answer is 3.


Sample Input 2

2 1
2 3

Sample Output 2

0

There are no good integers less than or equal to W.


Sample Input 3

4 12
3 3 3 3

Sample Output 3

3

There are 3 good integers: 3, 6, and 9.
For example, if we choose Weights 1, 2, and 3, they have a total mass of 9, so 9 is a good integer.
Note that 12 is not a good integer.


Sample Input 4

7 251
202 20 5 1 4 2 100

Sample Output 4

48
C - Poem Online Judge

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

ポエムオンラインジャッジ (Poem Online Judge, 以下 POJ と表記) は提出された文字列に得点をつけるオンラインジャッジです。
POJ に N 回の提出がありました。早い方から i 番目の提出では文字列 S_i が提出されて、得点は T_i でした。(同じ文字列が複数回提出される場合もあります)
ただし、POJ では 同じ文字列を提出しても得点が等しいとは限らない のに注意してください。

N 回の提出のうち、その提出よりも早い提出であって文字列が一致するものが存在しないような提出を オリジナル であると呼びます。
また、オリジナルな提出の中で最も得点が高いものを 最優秀賞 と呼びます。ただし、そのような提出が複数ある場合は、最も提出が早いものを最優秀賞とします。

最優秀賞は早い方から何番目の提出ですか?

制約

  • 1 \leq N \leq 10^5
  • S_i は英小文字からなる文字列
  • S_i の長さは 1 以上 10 以下
  • 0 \leq T_i \leq 10^9
  • N, T_i は整数

入力

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

N
S_1 T_1
S_2 T_2
\vdots
S_N T_N

出力

答えを出力せよ。


入力例 1

3
aaa 10
bbb 20
aaa 30

出力例 1

2

以下では早い方から i 番目の提出を提出 i と呼びます。
オリジナルな提出は提出 1 と 提出 2 です。提出 3 は提出 1 と文字列が一致しているためオリジナルではありません。
オリジナルな提出のうち最も得点が高い提出は提出 2 です。よってこれが最優秀賞になります。


入力例 2

5
aaa 9
bbb 10
ccc 10
ddd 10
bbb 11

出力例 2

2

オリジナルな提出は提出 1・提出 2・提出 3・提出 4 です。
その中で最も得点が高い提出は提出 2・提出 3・提出 4 です。この場合はこの中でもっとも提出の早い提出 2 を最優秀賞とします。
このように、オリジナルな提出の中で最も得点が高い提出が複数ある場合は、さらにその中で最も提出が早いものを最優秀賞とするのに注意してください。


入力例 3

10
bb 3
ba 1
aa 4
bb 1
ba 5
aa 9
aa 2
ab 6
bb 5
ab 3

出力例 3

8

Score : 300 points

Problem Statement

Poem Online Judge (POJ) is an online judge that gives scores to submitted strings.
There were N submissions to POJ. In the i-th earliest submission, string S_i was submitted, and a score of T_i was given. (The same string may have been submitted multiple times.)
Note that POJ may not necessarily give the same score to submissions with the same string.

A submission is said to be an original submission if the string in the submission is never submitted in any earlier submission.
A submission is said to be the best submission if it is an original submission with the highest score. If there are multiple such submissions, only the earliest one is considered the best submission.

Find the index of the best submission.

Constraints

  • 1 \leq N \leq 10^5
  • S_i is a string consisting of lowercase English characters.
  • S_i has a length between 1 and 10, inclusive.
  • 0 \leq T_i \leq 10^9
  • N and T_i are integers.

Input

Input is given from Standard Input in the following format:

N
S_1 T_1
S_2 T_2
\vdots
S_N T_N

Output

Print the answer.


Sample Input 1

3
aaa 10
bbb 20
aaa 30

Sample Output 1

2

We will refer to the i-th earliest submission as Submission i.
Original submissions are Submissions 1 and 2. Submission 3 is not original because it has the same string as that in Submission 1.
Among the original submissions, Submission 2 has the highest score. Therefore, this is the best submission.


Sample Input 2

5
aaa 9
bbb 10
ccc 10
ddd 10
bbb 11

Sample Output 2

2

Original submissions are Submissions 1, 2, 3, and 4.
Among them, Submissions 2, 3, and 4 have the highest scores. In this case, the earliest submission among them, Submission 2, is the best.
As in this sample, beware that if multiple original submissions have the highest scores, only the one with the earliest among them is considered the best submission.


Sample Input 3

10
bb 3
ba 1
aa 4
bb 1
ba 5
aa 9
aa 2
ab 6
bb 5
ab 3

Sample Output 3

8
D - At Most 3 (Contestant ver.)

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

整数 W が与えられます。
あなたは以下の条件をすべて満たすようにいくつかのおもりを用意することにしました。

  • おもりの個数は 1 個以上 300 個以下である。
  • おもりの重さは 10^6 以下の正整数である。
  • 1 以上 W 以下のすべての正整数は 良い整数 である。ここで、以下の条件を満たす正整数 n を良い整数と呼ぶ。
    • 用意したおもりのうち \bf{3} 個以下 の異なるおもりを自由に選んで、選んだおもりの重さの和を n にすることができる。  

条件を満たすようなおもりの組を 1 つ出力してください。

制約

  • 1 \leq W \leq 10^6
  • W は整数

入力

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

W

出力

N をおもりの個数、A_ii 番目のおもりの重さとして、以下の形式で出力せよ。答えが複数存在する場合、どれを出力しても正解とみなされる。

N
A_1 A_2 \dots A_N

ただし、N および A_1,A_2,\dots,A_N は以下の条件を満たす必要がある。

  • 1 \leq N \leq 300
  • 1 \leq A_i \leq 10^6

入力例 1

6

出力例 1

3
1 2 3

上の出力は重さ 1 のおもり、重さ 2 のおもり、重さ 3 のおもりの 3 個のおもりを用意しています。
この出力は条件を満たしています。特に 3 番目の条件について、以下のようにおもりを選ぶことで 1 以上 W 以下の整数すべてが良い整数であることが確認できます。

  • 1 番目のおもりのみを選ぶと、重さの和は 1 になる。
  • 2 番目のおもりのみを選ぶと、重さの和は 2 になる。
  • 3 番目のおもりのみを選ぶと、重さの和は 3 になる。
  • 1 番目と 3 番目のおもりを選ぶと、重さの和は 4 になる。
  • 2 番目と 3 番目のおもりを選ぶと、重さの和は 5 になる。
  • 1 番目、2 番目と 3 番目のおもりを選ぶと、重さの和は 6 になる。

入力例 2

12

出力例 2

6
2 5 1 2 5 1

同じ重さのおもりを 2 個以上用意しても良いです。

Score : 400 points

Problem Statement

You are given an integer W.
You are going to prepare some weights so that all of the conditions below are satisfied.

  • The number of weights is between 1 and 300, inclusive.
  • Each weight has a mass of positive integer not exceeding 10^6.
  • Every integer between 1 and W, inclusive, is a good integer. Here, a positive integer n is said to be a good integer if the following condition is satisfied:
    • We can choose at most 3 different weights from the prepared weights with a total mass of n.

Print a combination of weights that satisfies the conditions.

Constraints

  • 1 \leq W \leq 10^6
  • W is an integer.

Input

Input is given from Standard Input in the following format:

W

Output

Print in the following format, where N is the number of weights and A_i is the mass of the i-th weight. If multiple solutions exist, printing any of them is accepted.

N
A_1 A_2 \dots A_N

Here, N and A_1,A_2,\dots,A_N should satisfy the following conditions:

  • 1 \leq N \leq 300
  • 1 \leq A_i \leq 10^6

Sample Input 1

6

Sample Output 1

3
1 2 3

In the output above, 3 weights with masses 1, 2, and 3 are prepared.
This output satisfies the conditions. Especially, regarding the 3-rd condition, we can confirm that every integer between 1 and W, inclusive, is a good integer.

  • If we choose only the 1-st weight, it has a total mass of 1.
  • If we choose only the 2-nd weight, it has a total mass of 2.
  • If we choose only the 3-rd weight, it has a total mass of 3.
  • If we choose the 1-st and the 3-rd weights, they have a total mass of 4.
  • If we choose the 2-nd and the 3-rd weights, they have a total mass of 5.
  • If we choose the 1-st, the 2-nd, and the 3-rd weights, they have a total mass of 6.

Sample Input 2

12

Sample Output 2

6
2 5 1 2 5 1

You may prepare multiple weights with the same mass.

E - Takahashi and Animals

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

高橋君と N 匹の動物がいます。 N 匹の動物はそれぞれ動物 1 、動物 2\ldots 、動物 N と呼ばれます。

高橋君は下記の N 種類の行動をそれぞれ好きな回数だけ( 0 回でも良い)行います。

  • A_1 円払い、動物 1 と動物 2 に餌をあげる。
  • A_2 円払い、動物 2 と動物 3 に餌をあげる。
  • A_3 円払い、動物 3 と動物 4 に餌をあげる。
  • \cdots
  • A_i 円払い、動物 i と動物 (i+1) に餌をあげる。
  • \cdots
  • A_{N-2} 円払い、動物 (N-2) と動物 (N-1) に餌をあげる。
  • A_{N-1} 円払い、動物 (N-1) と動物 N に餌をあげる。
  • A_N 円払い、動物 N と動物 1 に餌をあげる。

上記の N 種類目の行動では、「動物 N と動物 1 に」餌をあげることに注意してください。

すべての動物にそれぞれ 1 回以上餌をあげるまでにかかる費用の合計として考えられる最小値を出力してください。

制約

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

すべての動物にそれぞれ 1 回以上餌をあげるまでにかかる費用の合計として考えられる最小値を出力せよ。


入力例 1

5
2 5 3 2 5

出力例 1

7

高橋君が 1 種類目、3 種類目、4 種類目の行動をそれぞれ 1 回ずつ行うと、 動物 11 回、動物 21 回、動物 31 回、動物 42 回、動物 51 回餌をあげることになり、すべての動物にそれぞれ 1 回以上餌をあげることができます。 このときにかかる費用の合計は A_1 + A_3 + A_4 = 2 + 3 + 2 = 7 円であり、これが考えられる最小値です。


入力例 2

20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62

出力例 2

426

Score : 500 points

Problem Statement

Takahashi is with N animals. The N animals are called Animal 1, Animal 2, \ldots, Animal N.

Takahashi will perform the following N kinds of action. Each action can be performed any number of (possibly zero) times.

  • Pay A_1 yen (the currency in Japan) to feed Animals 1 and 2.
  • Pay A_2 yen to feed Animals 2 and 3.
  • Pay A_3 yen to feed Animals 3 and 4.
  • \cdots
  • Pay A_i yen to feed Animals i and (i+1).
  • \cdots
  • Pay A_{N-2} yen to feed Animals (N-2) and (N-1).
  • Pay A_{N-1} yen to feed Animals (N-1) and N.
  • Pay A_N yen to feed Animals N and 1.

Note that the N-th action above feeds "Animals N and 1."

Print the minimum possible total cost to feed every animal at least once.

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print the minimum possible total cost to feed every animal at least once.


Sample Input 1

5
2 5 3 2 5

Sample Output 1

7

If Takahashi performs the 1-st, 3-rd, and 4-th actions once each, Animals 1, 2, 3, 4, and 5 are fed once, once, once, twice, once, respectively, so every animal is fed at least once. The total cost to do so is A_1 + A_3 + A_4 = 2 + 3 + 2 = 7 yen, which is the minimum possible.


Sample Input 2

20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62

Sample Output 2

426
F - Two Spanning Trees

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点 M 辺の無向グラフ G が与えられます。 G単純(自己ループおよび多重辺を持たない)かつ連結です。

i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ無向辺 \lbrace u_i, v_i \rbrace です。

下記の 2 つの条件をともに満たすような G2 つの全域木 T_1,T_21 組構成してください。( T_1T_2 は異なる全域木である必要はありません。)

  • T_1 は下記を満たす。

    T_1 を頂点 1 を根とする根付き木とみなしたとき、G の辺のうち T_1 に含まれないすべての辺 \lbrace u, v \rbrace について、uvT_1 において祖先と子孫の関係にある。

  • T_2 は下記を満たす。

    T_2 を頂点 1 を根とする根付き木とみなしたとき、G の辺のうち T_2 に含まれない辺 \lbrace u, v \rbrace であって、uvT_2 において祖先と子孫の関係にあるようなものは存在しない。

ここで、「根付き木 T において頂点 u と頂点 v が祖先と子孫の関係にある」とは、「 T において uv の祖先である」と「 T において vu の祖先である」のうちどちらかが成り立つことをいいます。

本問題の制約下において上記の条件を満たす T_1T_2 は必ず存在することが示せます。

制約

  • 2 \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq \min\lbrace 2 \times 10^5, N(N-1)/2 \rbrace
  • 1 \leq u_i, v_i \leq N
  • 入力はすべて整数
  • 与えられるグラフは単純かつ連結

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

T_1T_2 を下記の形式にしたがって、2N-2 行にわたって出力してください。すなわち、

  • 1 行目から N-1 行目には、T_1 に含まれる N-1 本の無向辺 \lbrace x_1, y_1\rbrace, \lbrace x_2, y_2\rbrace, \ldots, \lbrace x_{N-1}, y_{N-1}\rbrace を、各行に 1 本ずつ出力してください。
  • N 行目から 2N-2 行目には、T_2 に含まれる N-1 本の無向辺 \lbrace z_1, w_1\rbrace, \lbrace z_2, w_2\rbrace, \ldots, \lbrace z_{N-1}, w_{N-1}\rbrace を、各行に 1 本ずつ出力してください。

各全域木を構成する辺をどのような順番で出力するかや、各辺の出力においてどちらの端点を先に出力するかは任意です。

x_1 y_1
x_2 y_2
\vdots
x_{N-1} y_{N-1}
z_1 w_1
z_2 w_2
\vdots
z_{N-1} w_{N-1}

入力例 1

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

出力例 1

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

上記の出力例において、T_15 本の辺 \lbrace 1, 4 \rbrace, \lbrace 4, 3 \rbrace, \lbrace 5, 3 \rbrace, \lbrace 4, 2 \rbrace, \lbrace 6, 2 \rbrace を持つ G の全域木です。この T_1 は問題文中の条件を満たします。実際、G の辺のうち T_1 に含まれない各辺に関して、

  • \lbrace 5, 1 \rbrace について、頂点 1 は頂点 5 の祖先であり、
  • \lbrace 1, 2 \rbrace について、頂点 1 は頂点 2 の祖先であり、
  • \lbrace 1, 6 \rbrace について、頂点 1 は頂点 6 の祖先です。

また、T_25 本の辺 \lbrace 1, 5 \rbrace, \lbrace 5, 3 \rbrace, \lbrace 1, 4 \rbrace, \lbrace 2, 1 \rbrace, \lbrace 1, 6 \rbrace を持つ G の全域木です。この T_2 は問題文中の条件を満たします。実際、G の辺のうち T_2 に含まれない各辺に関して、

  • \lbrace 4, 3 \rbrace について、頂点 4 と頂点 3 は祖先と子孫の関係になく、
  • \lbrace 2, 6 \rbrace について、頂点 2 と頂点 6 は祖先と子孫の関係になく、
  • \lbrace 4, 2 \rbrace について、頂点 4 と頂点 2 は祖先と子孫の関係にありません。

入力例 2

4 3
3 1
1 2
1 4

出力例 2

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

3 本の辺 \lbrace 1, 2\rbrace, \lbrace 1, 3 \rbrace, \lbrace 1, 4 \rbrace を持つ木 TG の唯一の全域木です。 G の辺のうちこの木 T に含まれない辺は存在しないので、明らかに、TT_1 の条件と T_2 の条件をともに満たします。

Score : 500 points

Problem Statement

You are given an undirected graph G with N vertices and M edges. G is simple (it has no self-loops and multiple edges) and connected.

For i = 1, 2, \ldots, M, the i-th edge is an undirected edge \lbrace u_i, v_i \rbrace connecting Vertices u_i and v_i.

Construct two spanning trees T_1 and T_2 of G that satisfy both of the two conditions below. (T_1 and T_2 do not necessarily have to be different spanning trees.)

  • T_1 satisfies the following.

    If we regard T_1 as a rooted tree rooted at Vertex 1, for any edge \lbrace u, v \rbrace of G not contained in T_1, one of u and v is an ancestor of the other in T_1.

  • T_2 satisfies the following.

    If we regard T_2 as a rooted tree rooted at Vertex 1, there is no edge \lbrace u, v \rbrace of G not contained in T_2 such that one of u and v is an ancestor of the other in T_2.

We can show that there always exists T_1 and T_2 that satisfy the conditions above.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • N-1 \leq M \leq \min\lbrace 2 \times 10^5, N(N-1)/2 \rbrace
  • 1 \leq u_i, v_i \leq N
  • All values in input are integers.
  • The given graph is simple and connected.

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 (2N-2) lines to output T_1 and T_2 in the following format. Specifically,

  • The 1-st through (N-1)-th lines should contain the (N-1) undirected edges \lbrace x_1, y_1\rbrace, \lbrace x_2, y_2\rbrace, \ldots, \lbrace x_{N-1}, y_{N-1}\rbrace contained in T_1, one edge in each line.
  • The N-th through (2N-2)-th lines should contain the (N-1) undirected edges \lbrace z_1, w_1\rbrace, \lbrace z_2, w_2\rbrace, \ldots, \lbrace z_{N-1}, w_{N-1}\rbrace contained in T_2, one edge in each line.

You may print edges in each spanning tree in any order. Also, you may print the endpoints of each edge in any order.

x_1 y_1
x_2 y_2
\vdots
x_{N-1} y_{N-1}
z_1 w_1
z_2 w_2
\vdots
z_{N-1} w_{N-1}

Sample Input 1

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

Sample Output 1

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

In the Sample Output above, T_1 is a spanning tree of G containing 5 edges \lbrace 1, 4 \rbrace, \lbrace 4, 3 \rbrace, \lbrace 5, 3 \rbrace, \lbrace 4, 2 \rbrace, \lbrace 6, 2 \rbrace. This T_1 satisfies the condition in the Problem Statement. Indeed, for each edge of G not contained in T_1:

  • for edge \lbrace 5, 1 \rbrace, Vertex 1 is an ancestor of 5;
  • for edge \lbrace 1, 2 \rbrace, Vertex 1 is an ancestor of 2;
  • for edge \lbrace 1, 6 \rbrace, Vertex 1 is an ancestor of 6.

T_2 is another spanning tree of G containing 5 edges \lbrace 1, 5 \rbrace, \lbrace 5, 3 \rbrace, \lbrace 1, 4 \rbrace, \lbrace 2, 1 \rbrace, \lbrace 1, 6 \rbrace. This T_2 satisfies the condition in the Problem Statement. Indeed, for each edge of G not contained in T_2:

  • for edge \lbrace 4, 3 \rbrace, Vertex 4 is not an ancestor of Vertex 3, and vice versa;
  • for edge \lbrace 2, 6 \rbrace, Vertex 2 is not an ancestor of Vertex 6, and vice versa;
  • for edge \lbrace 4, 2 \rbrace, Vertex 4 is not an ancestor of Vertex 2, and vice versa.

Sample Input 2

4 3
3 1
1 2
1 4

Sample Output 2

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

Tree T, containing 3 edges \lbrace 1, 2\rbrace, \lbrace 1, 3 \rbrace, \lbrace 1, 4 \rbrace, is the only spanning tree of G. Since there are no edges of G that are not contained in T, obviously this T satisfies the conditions for both T_1 and T_2.

G - Intersection of Polygons

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

xy -平面上の凸 N 角形 P の頂点が、反時計回り(x_1, y_1), (x_2, y_2), \ldots, (x_N, y_N) として与えられます。(ただし、x 軸の正の方向を右向き、y 軸の正の方向を上向きとします。)

この多角形 P に対して、M 個の凸 N 多角形 P_1, P_2, \ldots, P_M を考えます。
i = 1, 2, \ldots, M について多角形 P_i は、 多角形 Px 軸の正の方向に u_iy 軸の正の方向に v_i だけ平行移動して得られる多角形です。すなわち、P_iN 個の点 (x_1+u_i, y_1+v_i), (x_2+u_i, y_2+v_i), \ldots, (x_N+u_i, y_N+v_i) を頂点とする凸 N 角形です。

Q 個の点 (a_1, b_1), (a_2, b_2), \ldots, (a_Q, b_Q) のそれぞれについて、 「その点が M 個の多角形 P_1, P_2, \ldots, P_M のすべてに含まれるか」を判定してください。

ただし、点が多角形の境界上にある場合も、その点はその多角形に含まれるとみなします。

制約

  • 3 \leq N \leq 50
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • -10^8 \leq x_i, y_i \leq 10^8
  • -10^8 \leq u_i, v_i \leq 10^8
  • -10^8 \leq a_i, b_i \leq 10^8
  • 入力はすべて整数
  • (x_1, y_1), (x_2, y_2), \ldots, (x_N, y_N) は反時計回りに凸多角形をなす
  • 多角形 P のそれぞれの内角の大きさは 180 度未満

入力

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

N
x_1 y_1
x_2 y_2
\vdots
x_N y_N
M
u_1 v_1
u_2 v_2
\vdots
u_M v_M
Q
a_1 b_1
a_2 b_2
\vdots
a_Q b_Q

出力

Q 行出力せよ。i = 1, 2, \ldots, Q について、i 行目には点 (a_i, b_i)M 個の多角形 P_1, P_2, \ldots, P_M のすべてに含まれる場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

5
-2 -3
0 -2
1 0
0 2
-2 1
2
0 1
1 0
6
0 0
1 0
0 1
1 1
-1 -1
-1 -2

出力例 1

Yes
No
Yes
Yes
Yes
No

多角形 P(-2, -3), (0, -2), (1, 0), (0, 2), (-2, 1) を頂点とする 5 角形です。

  • 多角形 P_1 は、Px 軸の正の方向に 0y 軸の正の方向に 1 だけ平行移動させた、(-2, -2), (0, -1), (1, 1), (0, 3), (-2, 2) を頂点とする 5 角形です。
  • 多角形 P_2 は、Px 軸の正の方向に 1y 軸の正の方向に 0 だけ平行移動させた、(-1, -3), (1, -2), (2, 0), (1, 2), (-1, 1) を頂点とする 5 角形です。

よって、下記の通りに 6 行出力します。

  • (a_1, b_1) = (0, 0)P_1P_2 の両方に含まれるので、1 行目には Yes を出力します。
  • (a_2, b_2) = (1, 0)P_2 には含まれますが P_1 には含まれないので、2 行目には No を出力します。
  • (a_3, b_3) = (0, 1)P_1P_2 の両方に含まれるので、3 行目には Yes を出力します。
  • (a_4, b_4) = (1, 1)P_1P_2 の両方に含まれるので、4 行目には Yes を出力します。
  • (a_5, b_5) = (-1, -1)P_1P_2 の両方に含まれるので、5 行目には Yes を出力します。
  • (a_6, b_6) = (-1, -2)P_2 には含まれますが P_1 には含まれないので、6 行目には No を出力します。

多角形の境界上にある点も多角形に含まれるとみなすことに注意してください。


入力例 2

10
45 100
-60 98
-95 62
-95 28
-78 -41
-54 -92
-8 -99
87 -94
98 23
87 91
5
-57 -40
-21 -67
25 39
-30 25
39 -20
16
4 5
-34 -8
-63 53
78 84
19 -16
64 9
-13 7
13 53
-20 4
2 -7
3 18
-12 10
-69 -93
2 9
27 64
-92 -100

出力例 2

Yes
Yes
No
No
Yes
No
Yes
No
Yes
Yes
Yes
Yes
No
Yes
No
No

Score : 600 points

Problem Statement

The vertices of a convex N-gon P in an xy-plane are given as (x_1, y_1), (x_2, y_2), \ldots, (x_N, y_N) in the counterclockwise order. (Here, the positive direction of the x-axis is right, and the positive direction of the y-axis is up.)

Based on this polygon P, we consider M convex N-gons P_1, P_2, \ldots, P_M.
For i = 1, 2, \ldots, M, the polygon P_i is obtained by shifting P in the positive direction of the x-axis by u_i and in the positive direction of the y-axis by v_i. In other words, P_i is a convex N-gon whose vertices are (x_1+u_i, y_1+v_i), (x_2+u_i, y_2+v_i), \ldots, (x_N+u_i, y_N+v_i).

For each of Q points (a_1, b_1), (a_2, b_2), \ldots, (a_Q, b_Q), determine if "the point is contained in all of the M polygons P_1, P_2, \ldots, P_M."

Here, we regard a point is also contained in a polygon if the point is on the polygon's boundary.

Constraints

  • 3 \leq N \leq 50
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • -10^8 \leq x_i, y_i \leq 10^8
  • -10^8 \leq u_i, v_i \leq 10^8
  • -10^8 \leq a_i, b_i \leq 10^8
  • All values in input are integers.
  • (x_1, y_1), (x_2, y_2), \ldots, (x_N, y_N) forms a convex N-gon in the counterclockwise order.
  • Each interior angle of the polygon P is less than 180 degrees.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
\vdots
x_N y_N
M
u_1 v_1
u_2 v_2
\vdots
u_M v_M
Q
a_1 b_1
a_2 b_2
\vdots
a_Q b_Q

Output

Print Q lines. For i = 1, 2, \ldots, Q, the i-th line should contain Yes if point (a_i, b_i) is contained in all of the M polygons P_1, P_2, \ldots, P_M; it should contain No otherwise.


Sample Input 1

5
-2 -3
0 -2
1 0
0 2
-2 1
2
0 1
1 0
6
0 0
1 0
0 1
1 1
-1 -1
-1 -2

Sample Output 1

Yes
No
Yes
Yes
Yes
No

Polygon P is a pentagon (5-gon) whose vertices are (-2, -3), (0, -2), (1, 0), (0, 2), (-2, 1).

  • Polygon P_1 is a pentagon (5-gon) obtained by shifting P in the positive direction of the x-axis by 0 and in the positive direction of the y-axis by 1, so its vertices are (-2, -2), (0, -1), (1, 1), (0, 3), (-2, 2).
  • Polygon P_2 is a pentagon (5-gon) obtained by shifting P in the positive direction of the x-axis by 1 and in the positive direction of the y-axis by 0, so its vertices are (-1, -3), (1, -2), (2, 0), (1, 2), (-1, 1).

Thus, the following 6 lines should be printed.

  • The 1-st line should be Yes because (a_1, b_1) = (0, 0) is contained in both P_1 and P_2.
  • The 2-nd line should be No because (a_2, b_2) = (1, 0) is contained in P_2 but not in P_1.
  • The 3-rd line should be Yes because (a_3, b_3) = (0, 1) is contained in both P_1 and P_2.
  • The 4-th line should be Yes because (a_4, b_4) = (1, 1) is contained in both P_1 and P_2.
  • The 5-th line should be Yes because (a_5, b_5) = (-1, -1) is contained in both P_1 and P_2.
  • The 6-th line should be No because (a_6, b_6) = (-1, -2) is contained in P_2 but not in P_1.

Note that a point on the boundary of a polygon is also considered to be contained in the polygon.


Sample Input 2

10
45 100
-60 98
-95 62
-95 28
-78 -41
-54 -92
-8 -99
87 -94
98 23
87 91
5
-57 -40
-21 -67
25 39
-30 25
39 -20
16
4 5
-34 -8
-63 53
78 84
19 -16
64 9
-13 7
13 53
-20 4
2 -7
3 18
-12 10
-69 -93
2 9
27 64
-92 -100

Sample Output 2

Yes
Yes
No
No
Yes
No
Yes
No
Yes
Yes
Yes
Yes
No
Yes
No
No
Ex - Fill Triangle

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

ブロックが三角形状に N 段並んでいます。上から i 段目には i 個のブロックが並んでいます。

6 以下の非負整数からなる列 A = (A_1, A_2, ..., A_N) を連長圧縮した列 P = ((a_1, c_1), (a_2, c_2), ..., (a_M, c_M)) が与えられます。

  • 例えば A = (2, 2, 2, 5, 5, 1) のとき P = ((2, 3), (5, 2), (1, 1)) になります。

上から i 段目で左から j 番目のブロックに書きこむ数を B_{i,j} として、次の条件を満たすようにすべてのブロックに数を書きこみます。

  • すべての 1 \leq i \leq N を満たす整数 i について B_{N,i} = A_{i}
  • すべての 1 \leq j \leq i \leq N-1 を満たす整数の組 i,j について B_{i,j}= (B_{i+1,j}+B_{i+1,j+1})\bmod 7

上から K 段目のブロックに書かれた数を列挙してください。

連長圧縮とは? 数列 A を以下の手続きによって整数の組からなる列に変換することを連長圧縮と呼びます。
  1. A を異なる要素が隣り合っている部分で分割する。
  2. 分割した各数列 B に対して、B を 「B を構成する数」 と 「B の長さ」 からなる整数の組に置き換える。
  3. 置き換えた整数の組を元の順番を保ったまま並べて列にする。

制約

  • 1 \leq N \leq 10^9
  • 1 \leq M \leq \min(N, 200)
  • 1 \leq K \leq \min(N,5 \times 10^5)
  • 0 \leq a_i \leq 6
  • 1 \leq c_i \leq N
  • \sum_{i=1}^M c_i = N
  • 入力される値はすべて整数

入力

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

N M K
a_1 c_1
a_2 c_2
\vdots
a_M c_M

出力

答えを以下の形式で出力せよ。なお、制約下において答えは一意に定まることが保証される。

B_{K,1} B_{K,2} \dots B_{K,K}

入力例 1

6 3 4
2 3
5 2
1 1

出力例 1

1 4 3 2

A = (2,2,2,5,5,1) です。また、ブロックに書かれる数は次のようになります。

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

入力例 2

1 1 1
6 1

出力例 2

6

入力例 3

111111111 9 9
0 1
1 10
2 100
3 1000
4 10000
5 100000
6 1000000
0 10000000
1 100000000

出力例 3

1 0 4 2 5 5 5 6 3

Score : 600 points

Problem Statement

Blocks are stacked in a triangle. The i-th column from the top has i blocks.

You are given a sequence P = ((a_1, c_1), (a_2, c_2), ..., (a_M, c_M)) which is a result of the run-length compression of a sequence A = (A_1, A_2, ..., A_N) consisting of non-negative integers less than or equal to 6.

  • For example, when A = (2, 2, 2, 5, 5, 1), you are given P = ((2, 3), (5, 2), (1, 1)).

You will write a number on each block so that the following conditions are satisfied, where B_{i,j} denotes the number to write on the j-th block from the left in the i-th column from the top:

  • For all integers i such that 1 \leq i \leq N, it holds that B_{N,i} = A_{i}.
  • For all pairs of integers (i, j) such that 1 \leq j \leq i \leq N-1, it holds that B_{i,j}= (B_{i+1,j}+B_{i+1,j+1})\bmod 7.

Enumerate the numbers written on the blocks in the K-th column from the top.

What is run-length compression? The run-length compression is a conversion from a given sequence A to a sequence of pairs of integers obtained by the following procedure.
  1. Split A off at the positions where two different elements are adjacent to each other.
  2. For each subsequence B that has been split off, replace B with a integer pair of "the number which B consists of" and "the length of B".
  3. Construct a sequence consisting of the integer pairs after the replacement without changing the order.

Constraints

  • 1 \leq N \leq 10^9
  • 1 \leq M \leq \min(N, 200)
  • 1 \leq K \leq \min(N,5 \times 10^5)
  • 0 \leq a_i \leq 6
  • 1 \leq c_i \leq N
  • \sum_{i=1}^M c_i = N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
a_1 c_1
a_2 c_2
\vdots
a_M c_M

Output

Print the answer in the following format. It is guaranteed that the answer is unique under the Constraint of the problem.

B_{K,1} B_{K,2} \dots B_{K,K}

Sample Input 1

6 3 4
2 3
5 2
1 1

Sample Output 1

1 4 3 2

We have A = (2,2,2,5,5,1). The number written on the blocks are as follows.

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

Sample Input 2

1 1 1
6 1

Sample Output 2

6

Sample Input 3

111111111 9 9
0 1
1 10
2 100
3 1000
4 10000
5 100000
6 1000000
0 10000000
1 100000000

Sample Output 3

1 0 4 2 5 5 5 6 3