実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
高橋君のケーキが誰かに食べられてしまいました。ケーキを食べた犯人の候補として、人 1、人 2、人 3 の三人が挙げられています。
犯人の目撃者はりんごさんとすぬけくんの二人がいます。りんごさんは人 A が犯人でないことを覚えており、すぬけくんは人 B が犯人でないことを覚えています。
二人の記憶から犯人を一人に特定できるかどうか判定し、特定できるならばその人の番号を出力してください。
制約
- 1\leq A,B\leq 3
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
二人の記憶から犯人を一人に特定できるならばその人の番号を出力し、特定できないならば -1 を出力せよ。
入力例 1
1 2
出力例 1
3
二人の記憶から、人 3 が犯人であることが特定できます。
入力例 2
1 1
出力例 2
-1
二人の記憶からでは、人 2,3 のどちらが犯人か特定することができません。よって -1 を出力します。
入力例 3
3 1
出力例 3
2
Score : 100 points
Problem Statement
Takahashi's cake has been eaten by someone. There are three suspects: person 1, person 2, and person 3.
There are two witnesses, Ringo and Snuke. Ringo remembers that person A is not the culprit, and Snuke remembers that person B is not the culprit.
Determine if the culprit can be uniquely identified based on the memories of the two witnesses. If the culprit can be identified, print the person's number.
Constraints
- 1 \leq A, B \leq 3
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
If the culprit can be uniquely identified based on the memories of the two witnesses, print the person's number; otherwise, print -1.
Sample Input 1
1 2
Sample Output 1
3
From the memories of the two witnesses, it can be determined that person 3 is the culprit.
Sample Input 2
1 1
Sample Output 2
-1
From the memories of the two witnesses, it cannot be determined whether person 2 or person 3 is the culprit. Therefore, print -1.
Sample Input 3
3 1
Sample Output 3
2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
初項が A、末項が B、公差が D であるような等差数列を出力してください。
なお、そのような等差数列が存在する入力のみが与えられます。
制約
- 1 \leq A \leq B \leq 100
- 1\leq D \leq 100
- 初項が A、末項が B、公差が D であるような等差数列が存在する
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
A B D
出力
初項が A、末項が B、公差が D であるような等差数列の項を順に空白区切りで出力せよ。
入力例 1
3 9 2
出力例 1
3 5 7 9
初項が 3、末項が 9、公差が 2 であるような等差数列は (3,5,7,9) です。
入力例 2
10 10 1
出力例 2
10
初項が 10、末項が 10、公差が 1 であるような等差数列は (10) です。
Score: 100 points
Problem Statement
Print an arithmetic sequence with first term A, last term B, and common difference D.
You are only given inputs for which such an arithmetic sequence exists.
Constraints
- 1 \leq A \leq B \leq 100
- 1 \leq D \leq 100
- There is an arithmetic sequence with first term A, last term B, and common difference D.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A B D
Output
Print the terms of the arithmetic sequence with first term A, last term B, and common difference D, in order, separated by spaces.
Sample Input 1
3 9 2
Sample Output 1
3 5 7 9
The arithmetic sequence with first term 3, last term 9, and common difference 2 is (3,5,7,9).
Sample Input 2
10 10 1
Sample Output 2
10
The arithmetic sequence with first term 10, last term 10, and common difference 1 is (10).
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
あなたは3Dゲームの当たり判定を実装しようとしています。
3 次元空間内の直方体であって、2 点 (a,b,c),(d,e,f) を結ぶ線分を対角線とし、全ての面が xy 平面、yz 平面、zx 平面のいずれかに平行なものを C(a,b,c,d,e,f) と表します。
(この定義により C(a,b,c,d,e,f) は一意に定まります)
2 つの直方体 C(a,b,c,d,e,f) と C(g,h,i,j,k,l) が与えられるので、これらの共通部分の体積が正かどうか判定してください。
制約
- 0 \leq a < d \leq 1000
- 0 \leq b < e \leq 1000
- 0 \leq c < f \leq 1000
- 0 \leq g < j \leq 1000
- 0 \leq h < k \leq 1000
- 0 \leq i < l \leq 1000
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
a b c d e f g h i j k l
出力
2 つの直方体の共通部分の体積が正なら Yes、そうでなければ No を出力せよ。
入力例 1
0 0 0 4 5 6 2 3 4 5 6 7
出力例 1
Yes
2 つの直方体の位置関係は下図のようになっており、共通部分の体積は 8 です。

入力例 2
0 0 0 2 2 2 0 0 2 2 2 4
出力例 2
No
2 つの直方体は面で接していますが、共通部分の体積は 0 です。
入力例 3
0 0 0 1000 1000 1000 10 10 10 100 100 100
出力例 3
Yes
Score : 250 points
Problem Statement
You are trying to implement collision detection in a 3D game.
In a 3-dimensional space, let C(a,b,c,d,e,f) denote the cuboid with a diagonal connecting (a,b,c) and (d,e,f), and with all faces parallel to the xy-plane, yz-plane, or zx-plane.
(This definition uniquely determines C(a,b,c,d,e,f).)
Given two cuboids C(a,b,c,d,e,f) and C(g,h,i,j,k,l), determine whether their intersection has a positive volume.
Constraints
- 0 \leq a < d \leq 1000
- 0 \leq b < e \leq 1000
- 0 \leq c < f \leq 1000
- 0 \leq g < j \leq 1000
- 0 \leq h < k \leq 1000
- 0 \leq i < l \leq 1000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
a b c d e f g h i j k l
Output
Print Yes if the intersection of the two cuboids has a positive volume, and No otherwise.
Sample Input 1
0 0 0 4 5 6 2 3 4 5 6 7
Sample Output 1
Yes
The positional relationship of the two cuboids is shown in the figure below, and their intersection has a volume of 8.

Sample Input 2
0 0 0 2 2 2 0 0 2 2 2 4
Sample Output 2
No
The two cuboids touch at a face, where the volume of the intersection is 0.
Sample Input 3
0 0 0 1000 1000 1000 10 10 10 100 100 100
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。
長さ K の文字列 t の出現回数を、以下を満たす整数 i の個数として定義します。
- 1 \leq i \leq N-K+1
- S の i 文字目から i+K-1 文字目までからなる部分文字列が t に一致する。
長さ K の文字列に対する出現回数の最大値 x を求めてください。 また、出現回数が x となるような長さ K の文字列をすべて辞書順昇順に出力してください。
制約
- N, K は整数
- S は英小文字からなる長さ N の文字列
- 1 \leq K \leq N \leq 100
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
2 行出力せよ。
1 行目には、長さ K の文字列に対する出現回数の最大値 x を出力せよ。
2 行目には、出現回数が x となるような長さ K の文字列を辞書順昇順に、空白区切りで出力せよ。
入力例 1
9 3 ovowowovo
出力例 1
2 ovo owo
出現回数 2 の文字列として、以下が挙げられます。
- 文字列
ovoについて、i=1,7 が条件を満たすことから、ovoの出現回数は 2 です。 - 文字列
owoについて、i=3,5 が条件を満たすことから、owoの出現回数は 2 です。
出現回数が 2 よりも大きいような長さ 3 の文字列は存在しないので、求める最大値は 2 です。
一方で、出現回数が 2 よりも小さい文字列として、以下が挙げられます。
- 文字列
vowについて、i=2 が条件を満たすことから、vowの出現回数は 1 です。 - 文字列
oooについて、条件を満たす i が存在しないことから、oooの出現回数は 0 です。
入力例 2
9 1 ovowowovo
出力例 2
5 o
入力例 3
35 3 thequickbrownfoxjumpsoverthelazydog
出力例 3
2 the
Score : 200 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters.
Define the number of occurrences of a string t of length K as the number of integers i that satisfy the following condition:
- 1 \leq i \leq N-K+1
- The substring of S from the i-th character to the (i+K-1)-th character matches t.
Find the maximum value x of the number of occurrences of a string of length K. Also, output all strings of length K with x occurrences in ascending lexicographical order.
Constraints
- N, K are integers.
- S is a string of length N consisting of lowercase English letters.
- 1 \leq K \leq N \leq 100
Input
The input is given from Standard Input in the following format:
N K S
Output
Output two lines.
The first line should contain the maximum value x of the number of occurrences of a string of length K.
The second line should contain all strings of length K with x occurrences in ascending lexicographical order, separated by spaces.
Sample Input 1
9 3 ovowowovo
Sample Output 1
2 ovo owo
The following strings have two occurrences:
- For the string
ovo, i=1,7 satisfy the condition, so the number of occurrences ofovois 2. - For the string
owo, i=3,5 satisfy the condition, so the number of occurrences ofowois 2.
There is no string of length 3 with more than two occurrences, so the maximum value is 2.
On the other hand, the following are examples of strings with fewer than two occurrences:
- For the string
vow, i=2 satisfies the condition, so the number of occurrences ofvowis 1. - For the string
ooo, there is no i that satisfies the condition, so the number of occurrences ofooois 0.
Sample Input 2
9 1 ovowowovo
Sample Output 2
5 o
Sample Input 3
35 3 thequickbrownfoxjumpsoverthelazydog
Sample Output 3
2 the
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋くんは、文字をいくつか持っています。
高橋くんが持っている文字は A, B, C のいずれかです。
はじめ、高橋くんは A を n _ A 個、B を n _ B 個、C を n _ C 個持っています。
高橋くんは、A を 1 つ、C を 1 つ、さらに追加で任意の 1 つの文字の合計 3 文字を使うことで、コンテストを 1 つ開催することができます。
具体的には、A を 2 つ、C を 1 つ使うと AAC を、A, B, C を 1 つずつ使うと ABC を、A を 1 つ、C を 2 つ使うと ACC を開催することができます。
高橋くんは、現在持っている文字を使ってコンテストをなるべく多く開催したいです。 高橋くんが最大でいくつのコンテストを開催することができるか求めてください。
T 個のテストケースが与えられるので、それぞれについて答えてください。
制約
- 1\le T\le2\times10 ^ 5
- それぞれのテストケースについて、
- 0\le n _ A\le10 ^ 9
- 0\le n _ B\le10 ^ 9
- 0\le n _ C\le10 ^ 9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{testcase} _ 1
\mathrm{testcase} _ 2
\vdots
\mathrm{testcase} _ T
\mathrm{testcase} _ i\ (1\le i\le T) は i 番目のテストケースを表しており、次の形式で与えられる。
n _ A n _ B n _ C
出力
T 行にわたって出力せよ。 i 行目 (1\le i\le T) には、i 番目のテストケースの答えを出力せよ。
入力例 1
5 3 1 2 100 0 0 1000000 1000000 1000000 31 41 59 1000000000 10000 1
出力例 1
2 0 1000000 31 1
1 つめのテストケースでは、AAC を 1 回、ABC を 1 回の計 2 回のコンテストを開催することができます。
よって、1 行目には 2 を出力してください。
Score : 300 points
Problem Statement
Takahashi has some letters.
Each letter he has is A, B, or C.
Initially, he has n _ A letters A, n _ B letters B, and n _ C letters C.
He can hold one contest by using one letter A, one letter C, and additionally any one letter, for a total of three letters.
Specifically, he can hold AAC by using two letters A and one letter C, ABC by using one letter each of A, B, and C, and ACC by using one letter A and two letters C.
He wants to hold as many contests as possible using the letters he currently has. Find the maximum number of contests he can hold.
T test cases are given, so report the answer for each of them.
Constraints
- 1\le T\le2\times10 ^ 5
- For each test case,
- 0\le n _ A\le10 ^ 9
- 0\le n _ B\le10 ^ 9
- 0\le n _ C\le10 ^ 9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\mathrm{testcase} _ 1
\mathrm{testcase} _ 2
\vdots
\mathrm{testcase} _ T
\mathrm{testcase} _ i\ (1\le i\le T) represents the i-th test case and is given in the following format:
n _ A n _ B n _ C
Output
Output over T lines. On the i-th line (1\le i\le T), output the answer to the i-th test case.
Sample Input 1
5 3 1 2 100 0 0 1000000 1000000 1000000 31 41 59 1000000000 10000 1
Sample Output 1
2 0 1000000 31 1
In the first test case, he can hold AAC once and ABC once, for a total of two contests.
Therefore, output 2 on the first line.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英小文字からなる文字列 S が与えられます。
S の先頭に a をいくつか( 0 個でも良い)つけ加えて回文にすることができるか判定してください。
ただし、長さ N の文字列 A=A_1A_2\ldots A_N が回文であるとは、すべての 1\leq i\leq N について A_i=A_{N+1-i} が成り立っていることをいいます。
制約
- 1 \leq \lvert S \rvert \leq 10^6
- S は英小文字のみからなる。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S の先頭に a をいくつかつけ加えて回文にすることができるならば Yes を、そうでないならば No を出力せよ。
入力例 1
kasaka
出力例 1
Yes
kasaka の先頭に a を 1 つ付け加えることによって、akasaka となり回文となるため Yes を出力します。
入力例 2
atcoder
出力例 2
No
atcoder の先頭に a をいくつ付け加えても回文となる事はありません。
入力例 3
php
出力例 3
Yes
php はそれ自体回文です。S の先頭に付け加える a は 0 個でも許されるため、Yes を出力します。
Score : 300 points
Problem Statement
Given is a string S consisting of lowercase English letters.
Determine whether adding some number of a's (possibly zero) at the beginning of S can make it a palindrome.
Here, a string of length N, A=A_1A_2\ldots A_N, is said to be a palindrome when A_i=A_{N+1-i} for every 1\leq i\leq N.
Constraints
- 1 \leq \lvert S \rvert \leq 10^6
- S consists of lowercase English letters.
Input
Input is given from Standard Input in the following format:
S
Output
If adding some number of a's (possibly zero) at the beginning of S can make it a palindrome, print Yes; otherwise, print No.
Sample Input 1
kasaka
Sample Output 1
Yes
By adding one a at the beginning of kasaka, we have akasaka, which is a palindrome, so Yes should be printed.
Sample Input 2
atcoder
Sample Output 2
No
Adding any number of a's at the beginning of atcoder does not make it a palindrome.
Sample Input 3
php
Sample Output 3
Yes
php itself is a palindrome. Adding zero a's at the beginning of S is allowed, so Yes should be printed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 種類の弁当が、それぞれ 1 個ずつ売られています。
i = 1, 2, \ldots, N について、i 種類目の弁当には A_i 個のたこ焼きと B_i 個のたい焼きが入っています。
高橋君は、 X 個以上のたこ焼きと Y 個以上のたい焼きを食べたいです。
高橋君がいくつかの弁当を選んで買うことで、 X 個以上のたこ焼きと Y 個以上のたい焼きを手に入れることが可能かどうか判定して下さい。また、可能な場合はそのために高橋君が購入しなければならない弁当の個数の最小値を求めて下さい。
各種類の弁当は 1 個しか売られていないため、同じ種類の弁当を 2 個以上購入することは出来ないことに注意して下さい。
制約
- 1 \leq N \leq 300
- 1 \leq X, Y \leq 300
- 1 \leq A_i, B_i \leq 300
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X Y A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
高橋君が X 個以上のたこ焼きと Y 個以上のたい焼きを手に入れることが不可能な場合は -1 を出力し、 可能な場合はそのために高橋君が購入しなければならない弁当の個数の最小値を出力せよ。
入力例 1
3 5 6 2 1 3 4 2 3
出力例 1
2
高橋君は、5 個以上のたこ焼きと 6 個以上のたい焼きを食べたいです。
高橋君は 2 種類目の弁当と 3 種類目の弁当を買うことで、
たこ焼きを 3 + 2 = 5 個、たい焼きを 4 + 3 = 7 個手に入れることができます。
入力例 2
3 8 8 3 4 2 3 2 1
出力例 2
-1
高橋君がたとえすべての弁当を買ったとしても、高橋君は 8 個以上のたこ焼きと 8 個以上のたい焼きを手に入れることが出来ません。
よって、-1 を出力します。
Score : 400 points
Problem Statement
A shop sells N kinds of lunchboxes, one for each kind.
For each i = 1, 2, \ldots, N, the i-th kind of lunchbox contains A_i takoyaki (octopus balls) and B_i taiyaki (fish-shaped cakes).
Takahashi wants to eat X or more takoyaki and Y or more taiyaki.
Determine whether it is possible to buy some number of lunchboxes to get at least X takoyaki and at least Y taiyaki. If it is possible, find the minimum number of lunchboxes that Takahashi must buy to get them.
Note that just one lunchbox is in stock for each kind; you cannot buy two or more lunchboxes of the same kind.
Constraints
- 1 \leq N \leq 300
- 1 \leq X, Y \leq 300
- 1 \leq A_i, B_i \leq 300
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X Y A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
If Takahashi cannot get at least X takoyaki and at least Y taiyaki, print -1; otherwise, print the minimum number of lunchboxes that he must buy to get them.
Sample Input 1
3 5 6 2 1 3 4 2 3
Sample Output 1
2
He wants to eat 5 or more takoyaki and 6 or more taiyaki.
Buying the second and third lunchboxes will get him 3 + 2 = 5 taiyaki and 4 + 3 = 7 taiyaki.
Sample Input 2
3 8 8 3 4 2 3 2 1
Sample Output 2
-1
Even if he is to buy every lunchbox, it is impossible to get at least 8 takoyaki and at least 8 taiyaki.
Thus, print -1.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
整数 N が与えられます。あなたは次の 2 種類の操作を行うことができます。
- X 円払う。N を \displaystyle\left\lfloor\frac{N}{A}\right\rfloor に置き換える。
- Y 円払う。1 以上 6 以下の整数が等確率で出るサイコロを振る。その出目を b としたとき、N を \displaystyle\left\lfloor\frac{N}{b}\right\rfloor に置き換える。
ここで \lfloor s \rfloor は s 以下の最大の整数を表します。例えば \lfloor 3 \rfloor=3、\lfloor 2.5 \rfloor=2 です。
適切に操作を選択したとき、N を 0 にするまでに払う必要がある金額の期待値の最小値を求めてください。
なお、サイコロの出目は操作ごとに独立であり、操作の選択はそれまでの操作の結果を確認してから行うことができます。
制約
- 1 \leq N \leq 10^{18}
- 2 \leq A \leq 6
- 1 \leq X,Y \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N A X Y
出力
答えを出力せよ。
真の解との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。
入力例 1
3 2 10 20
出力例 1
20.000000000000000
行える操作は 次の 2 種類です。
- 10 円払う。N を \displaystyle\left\lfloor\frac{N}{2}\right\rfloor に置き換える。
- 20 円払う。1 以上 6 以下の整数が等確率で出るサイコロを振る。その出目を b としたとき、N を \displaystyle\left\lfloor\frac{N}{b}\right\rfloor に置き換える。
前者の操作を 2 回行うのが最適です。
入力例 2
3 2 20 20
出力例 2
32.000000000000000
行える操作は 次の 2 種類です。
- 20 円払う。N を \displaystyle\left\lfloor\frac{N}{2}\right\rfloor に置き換える。
- 20 円払う。1 以上 6 以下の整数が等確率で出るサイコロを振る。その出目を b としたとき、N を \displaystyle\left\lfloor\frac{N}{b}\right\rfloor に置き換える。
最適な操作は以下のようになります。
- まず後者の操作でサイコロを振ります。
- 4 以上が出た場合 N=0 となります。
- 2 または 3 が出た場合 N=1 となります。続けて前者の操作を行うことで N=0 となります。
- 1 が出た場合最初からやり直します。
入力例 3
314159265358979323 4 223606797 173205080
出力例 3
6418410657.7408381
Score: 450 points
Problem Statement
You are given an integer N. You can perform the following two types of operations:
- Pay X yen to replace N with \displaystyle\left\lfloor\frac{N}{A}\right\rfloor.
- Pay Y yen to roll a die (dice) that shows an integer between 1 and 6, inclusive, with equal probability. Let b be the outcome of the die, and replace N with \displaystyle\left\lfloor\frac{N}{b}\right\rfloor.
Here, \lfloor s \rfloor denotes the greatest integer less than or equal to s. For example, \lfloor 3 \rfloor=3 and \lfloor 2.5 \rfloor=2.
Determine the minimum expected cost paid before N becomes 0 when optimally choosing operations.
The outcome of the die in each operation is independent of other rolls, and the choice of operation can be made after observing the results of the previous operations.
Constraints
- 1 \leq N \leq 10^{18}
- 2 \leq A \leq 6
- 1 \leq X, Y \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A X Y
Output
Print the answer.
Your output will be considered correct if the absolute or relative error from the true answer is at most 10^{-6}.
Sample Input 1
3 2 10 20
Sample Output 1
20.000000000000000
The available operations are as follows:
- Pay 10 yen. Replace N with \displaystyle\left\lfloor\frac{N}{2}\right\rfloor.
- Pay 20 yen. Roll a die. Let b be the outcome, and replace N with \displaystyle\left\lfloor\frac{N}{b}\right\rfloor.
The optimal strategy is to perform the first operation twice.
Sample Input 2
3 2 20 20
Sample Output 2
32.000000000000000
The available operations are as follows:
- Pay 20 yen. Replace N with \displaystyle\left\lfloor\frac{N}{2}\right\rfloor.
- Pay 20 yen. Roll a die. Let b be the outcome, and replace N with \displaystyle\left\lfloor\frac{N}{b}\right\rfloor.
The optimal strategy is as follows:
- First, perform the second operation to roll the die.
- If the outcome is 4 or greater, then N becomes 0.
- If the outcome is 2 or 3, then N becomes 1. Now, perform the first operation to make N = 0.
- If the outcome is 1, restart from the beginning.
Sample Input 3
314159265358979323 4 223606797 173205080
Sample Output 3
6418410657.7408381
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 頂点の木が与えられます。頂点には 1,2,\ldots ,N の番号がついており、i 番目の辺は頂点 u_i,v_i を結ぶ無向辺です。
各整数 i\,(1 \leq i \leq N) に対して、\sum_{j=1}^{N}dis(i,j) を求めてください。
ただし、dis(i,j) は頂点 i から頂点 j に到達する際にたどる必要のある最小の辺数です。
制約
- 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 行目には \sum_{j=1}^{N}dis(i,j) を出力せよ。
入力例 1
3 1 2 2 3
出力例 1
3 2 3
dis(1,1)+dis(1,2)+dis(1,3)=0+1+2=3、
dis(2,1)+dis(2,2)+dis(2,3)=1+0+1=2、
dis(3,1)+dis(3,2)+dis(3,3)=2+1+0=3、
です。
入力例 2
2 1 2
出力例 2
1 1
入力例 3
6 1 6 1 5 1 3 1 4 1 2
出力例 3
5 9 9 9 9 9
Score : 500 points
Problem Statement
Given is a tree with N vertices. The vertices are numbered 1,2,\ldots ,N, and the i-th edge is an undirected edge connecting Vertices u_i and v_i.
For each integer i\,(1 \leq i \leq N), find \sum_{j=1}^{N}dis(i,j).
Here, dis(i,j) denotes the minimum number of edges that must be traversed to go from Vertex i to Vertex j.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq u_i < v_i \leq N
- The given graph is a tree.
- All values in input are integers.
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.
The i-th line should contain \sum_{j=1}^{N}dis(i,j).
Sample Input 1
3 1 2 2 3
Sample Output 1
3 2 3
We have:
dis(1,1)+dis(1,2)+dis(1,3)=0+1+2=3,
dis(2,1)+dis(2,2)+dis(2,3)=1+0+1=2,
dis(3,1)+dis(3,2)+dis(3,3)=2+1+0=3.
Sample Input 2
2 1 2
Sample Output 2
1 1
Sample Input 3
6 1 6 1 5 1 3 1 4 1 2
Sample Output 3
5 9 9 9 9 9