Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英大文字・英小文字からなる空でない文字列 S が与えられます。以下の条件が満たされているか判定してください。
- S の先頭の文字は大文字であり、それ以外の文字はすべて小文字である。
制約
- 1 \leq |S| \leq 100(|S| は文字列 S の長さ)
- S の各文字は英大文字または英小文字である。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
条件が満たされていれば Yes、そうでなければ No を出力せよ。
入力例 1
Capitalized
出力例 1
Yes
Capitalized の先頭の文字 C は大文字であり、それ以外の文字 apitalized はすべて小文字であるため、Yes を出力します。
入力例 2
AtCoder
出力例 2
No
AtCoder は先頭以外にも大文字 C を含むため、No を出力します。
入力例 3
yes
出力例 3
No
yes の先頭の文字 y は大文字でないため、No を出力します。
入力例 4
A
出力例 4
Yes
Score: 100 points
Problem Statement
You are given a non-empty string S consisting of uppercase and lowercase English letters. Determine whether the following condition is satisfied:
- The first character of S is uppercase, and all other characters are lowercase.
Constraints
- 1 \leq |S| \leq 100 (|S| is the length of the string S.)
- Each character of S is an uppercase or lowercase English letter.
Input
The input is given from Standard Input in the following format:
S
Output
If the condition is satisfied, print Yes; otherwise, print No.
Sample Input 1
Capitalized
Sample Output 1
Yes
The first character C of Capitalized is uppercase, and all other characters apitalized are lowercase, so you should print Yes.
Sample Input 2
AtCoder
Sample Output 2
No
AtCoder contains an uppercase letter C that is not at the beginning, so you should print No.
Sample Input 3
yes
Sample Output 3
No
The first character y of yes is not uppercase, so you should print No.
Sample Input 4
A
Sample Output 4
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英大文字からなる長さ 3 の文字列 S が与えられます。S が ACE、BDF、CEG、DFA、EGB、FAC、GBD のいずれかと等しいとき Yes を、そうでないとき No を出力してください。
制約
- S は英大文字からなる長さ 3 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が ACE、BDF、CEG、DFA、EGB、FAC、GBD のいずれかと等しいとき Yes を、そうでないとき No を出力せよ。
入力例 1
ABC
出力例 1
No
S = ABC のとき、S は ACE、BDF、CEG、DFA、EGB、FAC、GBD のいずれとも等しくないので No を出力します。
入力例 2
FAC
出力例 2
Yes
入力例 3
XYX
出力例 3
No
Score : 100 points
Problem Statement
Given a length-3 string S consisting of uppercase English letters, print Yes if S equals one of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.
Constraints
- S is a length-3 string consisting of uppercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes if S equals one of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.
Sample Input 1
ABC
Sample Output 1
No
When S = ABC, S does not equal any of ACE, BDF, CEG, DFA, EGB, FAC, and GBD, so No should be printed.
Sample Input 2
FAC
Sample Output 2
Yes
Sample Input 3
XYX
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
xy 平面を考えます。x 軸の正の向きを東向き、y 軸の正の向きを北向きとします。
高橋君ははじめ、点 (x, y) = (0, 0) にいて東( x 軸の正の向き)を向いています。
S と R のみからなる長さ 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 は整数
- T は
SとRのみからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N T
出力
問題文中の手順を終えた後に高橋君がいる点の座標 (x, y) を、下記の形式にしたがって空白区切りで出力せよ。
x y
入力例 1
4 SSRS
出力例 1
2 -1
高橋君ははじめ (0, 0) にいて東を向いています。その後、高橋君は下記の通りに行動します。
- t_1 =
Sであるので、高橋君は東に距離 1 だけ進んだ (1, 0) に移動します。 - t_2 =
Sであるので、高橋君は東に距離 1 だけ進んだ (2, 0) に移動します。 - t_3 =
Rであるので、高橋君は右に 90 度回転し、高橋君は南を向きます。 - 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
SandR.
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.
- t_1 =
S, so he advances in the direction of east by distance 1, arriving at (1, 0). - t_2 =
S, so he advances in the direction of east by distance 1, arriving at (2, 0). - t_3 =
R, so he turns 90 degrees clockwise, resulting in facing south. - 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
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
縦 H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と表します。
各マスの状態は文字 C_{i,j} で表されます。C_{i,j} が . ならば (i, j) には何も置かれておらず、 # ならば箱が 1 個置かれています。
1 \leq j \leq W を満たす整数 j に対して、整数 X_j を次のように定義します。
- j 列目に置かれている箱の個数。言い換えると、C_{i,j} が
#であるような整数 i の個数。
X_1, X_2, \dots, X_W をすべて求めてください。
制約
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- H, W は整数
- C_{i, j} は
.または#
入力
入力は以下の形式で標準入力から与えられる。
H W
C_{1,1}C_{1,2}\dots C_{1,W}
C_{2,1}C_{2,2}\dots C_{2,W}
\vdots
C_{H,1}C_{H,2}\dots C_{H,W}
出力
X_1, X_2, \dots, X_W を以下の形式に従って出力せよ。
X_1 X_2 \dots X_W
入力例 1
3 4 #..# .#.# .#.#
出力例 1
1 2 0 3
1 列目の箱が置かれているマスは (1, 1) の 1 ヵ所です。よって X_1 = 1 です。
2 列目の箱が置かれているマスは (2, 2), (3, 2) の 2 ヵ所です。よって X_2 = 2 です。
3 列目の箱が置かれているマスは存在しません。よって X_3 = 0 です。
4 列目の箱が置かれているマスは (1, 4), (2, 4), (3, 4) の 3 ヵ所です。よって X_4 = 3 です。
よって (X_1, X_2, X_3, X_4) = (1, 2, 0, 3) が答えとなります。
入力例 2
3 7 ....... ....... .......
出力例 2
0 0 0 0 0 0 0
箱が置かれているマスが存在しない場合もあります。
入力例 3
8 3 .#. ### .#. .#. .## ..# ##. .##
出力例 3
2 7 4
入力例 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
出力例 4
0 5 1 2 2 0 0 5 3 3 3 3 0 0 1 1 3 1 1 0 0 5 3 3 3 3 0 0 5 1 1 1 5 0 0 3 2 2 2 2 0 0 5 3 3 3 3
Score : 200 points
Problem Statement
There is a grid with H rows from top to bottom and W columns from left to right. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
The squares are described by characters C_{i,j}. If C_{i,j} is ., (i, j) is empty; if it is #, (i, j) contains a box.
For integers j satisfying 1 \leq j \leq W, let the integer X_j defined as follows.
- X_j is the number of boxes in the j-th column. In other words, X_j is the number of integers i such that C_{i,j} is
#.
Find all of X_1, X_2, \dots, X_W.
Constraints
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- H and W are integers.
- C_{i, j} is
.or#.
Input
The input is given from Standard Input in the following format:
H W
C_{1,1}C_{1,2}\dots C_{1,W}
C_{2,1}C_{2,2}\dots C_{2,W}
\vdots
C_{H,1}C_{H,2}\dots C_{H,W}
Output
Print X_1, X_2, \dots, X_W in the following format:
X_1 X_2 \dots X_W
Sample Input 1
3 4 #..# .#.# .#.#
Sample Output 1
1 2 0 3
In the 1-st column, one square, (1, 1), contains a box. Thus, X_1 = 1.
In the 2-nd column, two squares, (2, 2) and (3, 2), contain a box. Thus, X_2 = 2.
In the 3-rd column, no squares contain a box. Thus, X_3 = 0.
In the 4-th column, three squares, (1, 4), (2, 4), and (3, 4), contain a box. Thus, X_4 = 3.
Therefore, the answer is (X_1, X_2, X_3, X_4) = (1, 2, 0, 3).
Sample Input 2
3 7 ....... ....... .......
Sample Output 2
0 0 0 0 0 0 0
There may be no square that contains a box.
Sample Input 3
8 3 .#. ### .#. .#. .## ..# ##. .##
Sample Output 3
2 7 4
Sample Input 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
Sample Output 4
0 5 1 2 2 0 0 5 3 3 3 3 0 0 1 1 3 1 1 0 0 5 3 3 3 3 0 0 5 1 1 1 5 0 0 3 2 2 2 2 0 0 5 3 3 3 3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1 から N までの番号がついた N 個の空の箱と、何も書かれていない無数のカードがあります。
Q 個のクエリが与えられるので、順番に処理してください。クエリは次の 3 種類のいずれかです。
1 i j\colon カードを 1 枚選んで数 i を書き込み、そのカードを箱 j に入れる2 i\colon 箱 i に入っているカードに書かれた数を昇順ですべて答える3 i\colon 数 i が書かれたカードが入っている箱の番号を昇順ですべて答える
ただし、以下の点に注意してください。
- 2 番目の形式のクエリにおいては、箱 i の中に同じ数が書かれたカードが複数枚あるときは、入っている枚数と同じ回数だけその数を出力する
- 3 番目の形式のクエリにおいては、数 i が書かれたカードが同じ箱に複数枚入っている場合でも、その箱の番号は 1 度だけ出力する
制約
- 1 \leq N, Q \leq 2 \times 10^5
- 1 番目の形式のクエリについて、
- 1 \leq i \leq 2 \times 10^5
- 1 \leq j \leq N
- 2 番目の形式のクエリについて、
- 1 \leq i \leq N
- このクエリが与えられる時点で箱 i にカードが入っている
- 3 番目の形式のクエリについて、
- 1 \leq i \leq 2 \times 10^5
- このクエリが与えられる時点で数 i が書かれたカードが入っている箱が存在する
- 出力するべき数は合計で 2 \times 10^5 個以下
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
ただし、\mathrm{query}_q は q 個目のクエリを表しており、次のいずれかの形式で与えられる。
1 i j
2 i
3 i
出力
2 番目の形式のクエリと 3 番目の形式のクエリに対し、順番に答えを出力せよ。
各クエリでは出力するべき要素を昇順に空白区切りで出力し、クエリごとに改行せよ。
入力例 1
5 8 1 1 1 1 2 4 1 1 4 2 4 1 1 4 2 4 3 1 3 2
出力例 1
1 2 1 1 2 1 4 4
クエリを順に処理していきます。
- カードに 1 を書き込み、箱 1 に入れます。
- カードに 2 を書き込み、箱 4 に入れます。
- カードに 1 を書き込み、箱 4 に入れます。
- 箱 4 に入っているカードに書かれた数は 1, 2 です。
- 1, 2 の順に出力してください。
- カードに 1 を書き込み、箱 4 に入れます。
- 箱 4 に入っているカードに書かれた数は 1, 1, 2 です。
- 1 を 2 度出力することに注意してください。
- 数 1 が書かれたカードが入っている箱は箱 1, 4 です。
- 箱 4 には数 1 が書かれたカードが 2 枚入っていますが、4 は 1 回しか出力しないことに注意してください。
- 数 2 が書かれたカードが入っている箱は箱 4 です。
入力例 2
1 5 1 1 1 1 2 1 1 200000 1 2 1 3 200000
出力例 2
1 2 200000 1
Score : 300 points
Problem Statement
We have N boxes numbered 1 to N that are initially empty, and an unlimited number of blank cards.
Process Q queries in order. There are three kinds of queries as follows.
1 i j\colon Write the number i on a blank card and put it into box j.2 i\colon Report all numbers written on the cards in box i, in ascending order.3 i\colon Report all box numbers of the boxes that contain a card with the number i, in ascending order.
Here, note the following.
- In a query of the second kind, if box i contains multiple cards with the same number, that number should be printed the number of times equal to the number of those cards.
- In a query of the third kind, even if a box contains multiple cards with the number i, the box number of that box should be printed only once.
Constraints
- 1 \leq N, Q \leq 2 \times 10^5
- For a query of the first kind:
- 1 \leq i \leq 2 \times 10^5
- 1 \leq j \leq N
- For a query of the second kind:
- 1 \leq i \leq N
- Box i contains some cards when this query is given.
- For a query of the third kind:
- 1 \leq i \leq 2 \times 10^5
- Some boxes contain a card with the number i when this query is given.
- At most 2 \times 10^5 numbers are to be reported.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Here, \mathrm{query}_q denotes the q-th query, which is in one of the following formats:
1 i j
2 i
3 i
Output
Respond to the queries of the second and third kinds in order.
For each of those queries, print one line containing the elements to be reported in ascending order, with spaces in between.
Sample Input 1
5 8 1 1 1 1 2 4 1 1 4 2 4 1 1 4 2 4 3 1 3 2
Sample Output 1
1 2 1 1 2 1 4 4
Let us process the queries in order.
- Write 1 on a card and put it into box 1.
- Write 2 on a card and put it into box 4.
- Write 1 on a card and put it into box 4.
- Box 4 contains cards with the numbers 1 and 2.
- Print 1 and 2 in this order.
- Write 1 on a card and put it into box 4.
- Box 4 contains cards with the numbers 1, 1, and 2.
- Note that you should print 1 twice.
- Boxes 1 and 4 contain a card with the number 1.
- Note that you should print 4 only once, even though box 4 contains two cards with the number 1.
- Boxes 4 contains a card with the number 2.
Sample Input 2
1 5 1 1 1 1 2 1 1 200000 1 2 1 3 200000
Sample Output 2
1 2 200000 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N かつ全ての要素が 1 以上 M 以下である整数列のうち、狭義単調増加であるものを全て辞書順に出力してください。
注記
ある 2 個の異なる長さの等しい整数列 A_1,A_2,\dots,A_N と B_1,B_2,\dots,B_N が以下を満たすとき、またその時に限り辞書順で A が B より早いと定義されます。
- ある整数 i(1 \le i \le N) が存在し、1 \le j < i である全ての整数 j に対し A_j=B_j が成り立ち、かつ A_i < B_i が成り立つ。
ある整数列 A_1,A_2,\dots,A_N は以下を満たすとき、またその時に限り狭義単調増加です。
- 全ての整数 i(1 \le i \le N-1) に対し A_i < A_{i+1} が成り立つ。
制約
- 1 \le N \le M \le 10
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
条件を満たす整数列を一行に一つずつ、辞書順に出力せよ(出力例を参考にせよ)。
入力例 1
2 3
出力例 1
1 2 1 3 2 3
条件を満たす数列は (1,2),(1,3),(2,3) の 3 個です。これらを辞書順で早い方から出力します。
入力例 2
3 5
出力例 2
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
Score : 300 points
Problem Statement
Print all strictly increasing integer sequences of length N where all elements are between 1 and M (inclusive), in lexicographically ascending order.
Notes
For two integer sequences of the same length A_1,A_2,\dots,A_N and B_1,B_2,\dots,B_N, A is said to be lexicographically earlier than B if and only if:
- there is an integer i (1 \le i \le N) such that A_j=B_j for all integers j satisfying 1 \le j < i, and A_i < B_i.
An integer sequence A_1,A_2,\dots,A_N is said to be strictly increasing if and only if:
- A_i < A_{i+1} for all integers i (1 \le i \le N-1).
Constraints
- 1 \le N \le M \le 10
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the sought sequences in lexicographically ascending order, each in its own line (see Sample Outputs).
Sample Input 1
2 3
Sample Output 1
1 2 1 3 2 3
The sought sequences are (1,2),(1,3),(2,3), which should be printed in lexicographically ascending order.
Sample Input 2
3 5
Sample Output 2
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
AtCoder 社の壁紙の模様を xy 平面上に表現すると、以下のようになります。
-
以下の 3 種類の直線で領域が分割されている。
- x = n (n は整数)
- y = n (n は偶数)
- x + y = n (n は偶数)
- 各領域は白もしくは黒で塗られている。いずれかの直線で隣接する 2 領域は異なる色で塗られている。
- (0.5, 0.5) を含む領域は黒で塗られている。
下の図は、模様の一部を表したものです。

整数 A, B, C, D が与えられます。各辺が x, y 軸に平行で、左下の頂点が (A, B) にあり右上の頂点が (C, D) にあるような長方形を考えます。この長方形の内側に存在する黒で塗られた領域の面積を求め、それを 2 倍したものを出力してください。
出力する値は整数になることが証明できます。
制約
- -10^9 \leq A, B, C, D \leq 10^9
- A < C かつ B < D
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
A B C D
出力
答えを一行に出力せよ。
入力例 1
0 0 3 3
出力例 1
10
求めるのは、以下の正方形で囲われた領域内の黒く塗られた領域の面積です。

これは 5 なので、 2 倍した 10 を出力します。
入力例 2
-1 -2 1 3
出力例 2
11
面積は 5.5 と小数になりますが、出力するべき値は整数になります。
入力例 3
-1000000000 -1000000000 1000000000 1000000000
出力例 3
4000000000000000000
これは長方形が最大のケースですが、出力は 64bit 符号付き整数の範囲に収まります。
Score : 450 points
Problem Statement
The pattern of AtCoder's wallpaper can be represented on the xy-plane as follows:
-
The plane is divided by the following three types of lines:
- x = n (where n is an integer)
- y = n (where n is an even number)
- x + y = n (where n is an even number)
- Each region is painted black or white. Any two regions adjacent along one of these lines are painted in different colors.
- The region containing (0.5, 0.5) is painted black.
The following figure shows a part of the pattern.

You are given integers A, B, C, D. Consider a rectangle whose sides are parallel to the x- and y-axes, with its bottom-left vertex at (A, B) and its top-right vertex at (C, D). Calculate the area of the regions painted black inside this rectangle, and print twice that area.
It can be proved that the output value will be an integer.
Constraints
- -10^9 \leq A, B, C, D \leq 10^9
- A < C and B < D.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A B C D
Output
Print the answer on a single line.
Sample Input 1
0 0 3 3
Sample Output 1
10
We are to find the area of the black-painted region inside the following square:

The area is 5, so print twice that value: 10.
Sample Input 2
-1 -2 1 3
Sample Output 2
11
The area is 5.5, which is not an integer, but the output value is an integer.
Sample Input 3
-1000000000 -1000000000 1000000000 1000000000
Sample Output 3
4000000000000000000
This is the case with the largest rectangle, where the output still fits into a 64-bit signed integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点の根付き木が与えられます。頂点 1 が根です。
i = 1, 2, \ldots, N-1 について、i 番目の辺は頂点 u_i と頂点 v_i を結んでいます。
i = 1, 2, \ldots, N について、頂点 i を根とする部分木に含まれる頂点全体からなる集合を S_i で表します。(各頂点は自身を根とする部分木に含まれます。すなわち、i \in S_i です。)
また、整数 l, r について、l 以上 r 以下の整数全体からなる集合を [l, r] で表します。 すなわち、[l, r] = \lbrace l, l+1, l+2, \ldots, r \rbrace です。
整数の 2 つ組を N 個並べた列 \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big) であって以下の条件を満たすものを考えます。
- 1 \leq i \leq N を満たすすべての整数 i について、1 \leq L_i \leq R_i
- 1 \leq i, j \leq N を満たすすべての整数の組 (i, j) について次が成り立つ
- S_i \subseteq S_j ならば、[L_i, R_i] \subseteq [L_j, R_j]
- S_i \cap S_j = \emptyset ならば、[L_i, R_i] \cap [L_j, R_j] = \emptyset
そのような \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big) が少なくとも 1 つ存在することが示せます。 それらのうち、登場する整数の最大値 \max \lbrace L_1, L_2, \ldots, L_N, R_1, R_2, \ldots, R_N \rbrace が最小のものを 1 つ出力してください。(複数ある場合はどれを出力しても正解となります。)
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- 入力はすべて整数
- 与えられるグラフは木である
入力
入力は以下の形式で標準入力から与えられる。
N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
出力
下記の形式で N 行出力せよ。すなわち、i = 1, 2, \ldots, N について、i 行目に L_i と R_i を空白区切りで出力せよ。
L_1 R_1 L_2 R_2 \vdots L_N R_N
入力例 1
3 2 1 3 1
出力例 1
1 2 2 2 1 1
(L_1, R_1) = (1, 2), (L_2, R_2) = (2, 2), (L_3, R_3) = (1, 1) が問題文中の条件を満たします。
実際、[L_2, R_2] \subseteq [L_1, R_1], [L_3, R_3] \subseteq [L_1, R_1], [L_2, R_2] \cap [L_3, R_3] = \emptyset が成り立ちます。
また、\max \lbrace L_1, L_2, L_3, R_1, R_2, R_3 \rbrace = 2 であり、これが最小です。
入力例 2
5 3 4 5 4 1 2 1 4
出力例 2
1 3 3 3 2 2 1 2 1 1
入力例 3
5 4 5 3 2 5 2 3 1
出力例 3
1 1 1 1 1 1 1 1 1 1
Score : 500 points
Problem Statement
You are given a rooted tree with N vertices. The root is Vertex 1.
For each i = 1, 2, \ldots, N-1, the i-th edge connects Vertex u_i and Vertex v_i.
For each i = 1, 2, \ldots, N, let S_i denote the set of all vertices in the subtree rooted at Vertex i. (Each vertex is in the subtree rooted at itself, that is, i \in S_i.)
Additionally, for integers l and r, let [l, r] denote the set of all integers between l and r, that is, [l, r] = \lbrace l, l+1, l+2, \ldots, r \rbrace.
Consider a sequence of N pairs of integers \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big) that satisfies the conditions below.
- 1 \leq L_i \leq R_i for every integer i such that 1 \leq i \leq N.
- The following holds for every pair of integers (i, j) such that 1 \leq i, j \leq N.
- [L_i, R_i] \subseteq [L_j, R_j] if S_i \subseteq S_j
- [L_i, R_i] \cap [L_j, R_j] = \emptyset if S_i \cap S_j = \emptyset
It can be shown that there is at least one sequence \big((L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N)\big). Among those sequences, print one that minimizes \max \lbrace L_1, L_2, \ldots, L_N, R_1, R_2, \ldots, R_N \rbrace, the maximum integer used. (If there are multiple such sequences, you may print any of them.)
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- All values in input are integers.
- The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
Output
Print N lines in the format below. That is, for each i = 1, 2, \ldots, N, the i-th line should contain L_i and R_i separated by a space.
L_1 R_1 L_2 R_2 \vdots L_N R_N
Sample Input 1
3 2 1 3 1
Sample Output 1
1 2 2 2 1 1
(L_1, R_1) = (1, 2), (L_2, R_2) = (2, 2), (L_3, R_3) = (1, 1) satisfies the conditions.
Indeed, we have [L_2, R_2] \subseteq [L_1, R_1], [L_3, R_3] \subseteq [L_1, R_1], [L_2, R_2] \cap [L_3, R_3] = \emptyset.
Additionally, \max \lbrace L_1, L_2, L_3, R_1, R_2, R_3 \rbrace = 2 is the minimum possible value.
Sample Input 2
5 3 4 5 4 1 2 1 4
Sample Output 2
1 3 3 3 2 2 1 2 1 1
Sample Input 3
5 4 5 3 2 5 2 3 1
Sample Output 3
1 1 1 1 1 1 1 1 1 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
N 頂点の木 T が与えられます。T の頂点には 1 から N の番号がついており、 i\,(1\leq i \leq N-1) 番目の辺は頂点 u_i と頂点 v_i を双方向に結んでいます。
T を用いて、N 頂点の完全グラフ G を次のように定めます。
- G の頂点 x と頂点 y の間の辺の重み w(x,y) を、T における頂点 x と頂点 y の間の最短距離とする
G の最大重み最大マッチングを一つ求めてください。すなわち、\lfloor N/2 \rfloor 個の頂点のペアの集合 M=\{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} であって、各頂点 1,2,\dots, N が M に現れる回数がたかだか 1 回であるようなもののうち、 \displaystyle \sum_{i=1}^{\lfloor N/2 \rfloor} w(x_i,y_i) が最大であるものを一つ求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq u_i < v_i \leq N
- 入力されるグラフは木である
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
出力
答えを \{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} として、以下の形式で出力せよ。答えが複数あり得る場合、そのうちどれを出力しても良い。
x_1 y_1
x_2 y_2
\vdots
x_{\lfloor N/2 \rfloor} y_{\lfloor N/2 \rfloor}
入力例 1
4 1 2 2 3 3 4
出力例 1
2 4 1 3
T において、頂点 2,4 間の距離は 2、頂点 1,3 間の距離は 2 なので、マッチング \{(2,4),(1,3)\} の重みは 4 です。重みが 4 より大きいマッチングは存在しないので、これが最大重み最大マッチングの一つです。他にも、
2 3 1 4
などを出力しても正解になります。
入力例 2
3 1 2 2 3
出力例 2
1 3
T において、頂点 1,3 間の距離は 2 なので、マッチング \{(1,3)\} の重みは 2 です。重みが 2 より大きいマッチングは存在しないので、これが最大重み最大マッチングの一つです。他にも、
3 1
を出力しても正解になります。
Score : 550 points
Problem Statement
You are given a tree T with N vertices. The vertices are numbered 1 to N, and the i-th edge (1 \leq i \leq N-1) connects vertices u_i and v_i bidirectionally.
Using T, define a complete graph G with N vertices as follows:
- The weight w(x,y) of the edge between vertices x and y in G is the shortest distance between vertices x and y in T.
Find one maximum weight maximum matching in G. That is, find a set of \lfloor N/2 \rfloor pairs of vertices M=\{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} such that each vertex 1,2,\dots, N appears in M at most once, and \displaystyle \sum_{i=1}^{\lfloor N/2 \rfloor} w(x_i,y_i) is maximized.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq u_i < v_i \leq N
- The input graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
Output
Print a solution as \{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} in the following format. If multiple solutions exist, any of them is acceptable.
x_1 y_1
x_2 y_2
\vdots
x_{\lfloor N/2 \rfloor} y_{\lfloor N/2 \rfloor}
Sample Input 1
4 1 2 2 3 3 4
Sample Output 1
2 4 1 3
In T, the distance between vertices 2 and 4 is 2, and the distance between vertices 1 and 3 is 2, so the weight of the matching \{(2,4),(1,3)\} is 4. There is no matching with a weight greater than 4, so this is a maximum weight maximum matching. Other acceptable outputs include:
2 3 1 4
Sample Input 2
3 1 2 2 3
Sample Output 2
1 3
In T, the distance between vertices 1 and 3 is 2, so the weight of the matching \{(1,3)\} is 2. There is no matching with a weight greater than 2, so this is a maximum weight maximum matching. Another acceptable output is:
3 1