Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋君は野球ゲームを作っています。
高橋君はバッターの打率を指定された桁数の分だけ表示するプログラムを作ることにしました。
整数 A, B があります。ここで A, B は 1 \leq A \leq 10, 0 \leq B \leq A を満たします。
このとき、文字列 S を次のように定義します。
- \dfrac{B}{A} を小数点第 4 位で四捨五入した値を「整数部 1 桁」「
.」「小数部 3 桁」の順に末尾ゼロを省略せずに表記した文字列。
例えば A=7, B = 4 の場合は、\dfrac{B}{A} = \dfrac{4}{7} = 0.571428\dots で、これを小数点第 4 位で四捨五入した値は 0.571 です。よって S は 0.571 になります。
A, B が入力として与えられるので S を出力してください。
制約
- 1 \leq A \leq 10
- 0 \leq B \leq A
- A, B は整数
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
S を問題文の指示に従った形式で出力せよ。問題文の指示と異なる形式で出力した場合は誤答となることに注意せよ。
入力例 1
7 4
出力例 1
0.571
問題文本文でも説明した通り、\dfrac{B}{A} = \dfrac{4}{7} = 0.571428\dots で、これを小数点第 4 位で四捨五入した値は 0.571 です。よって S は 0.571 になります。
入力例 2
7 3
出力例 2
0.429
\dfrac{B}{A} = \dfrac{3}{7} = 0.428571\dots で、これを小数点第 4 位で四捨五入した値は 0.429 です。(繰り上がりが発生するのに注意してください。)
よって S は 0.429 となります。
入力例 3
2 1
出力例 3
0.500
\dfrac{B}{A} = \dfrac{1}{2} = 0.5 で、これを小数点第 4 位で四捨五入した値も同様に 0.5 です。
よって S は 0.500 となります。小数部を 3 桁並べる必要があるのに注意してください。
入力例 4
10 10
出力例 4
1.000
入力例 5
1 0
出力例 5
0.000
Score : 100 points
Problem Statement
Takahashi is making a computer baseball game.
He will write a program that shows a batter's batting average with a specified number of digits.
There are integers A and B, which satisfy 1 \leq A \leq 10 and 0 \leq B \leq A.
Let S be the string obtained as follows.
- Round off \dfrac{B}{A} to three decimal digits, then write the integer part (1 digit),
.(the decimal point), and the decimal part (3 digits) in this order, with trailing zeros.
For example, if A=7 and B=4, then \dfrac{B}{A} = \dfrac{4}{7} = 0.571428\dots rounded off to three decimal digits is 0.571. Thus, S is 0.571.
You are given A and B as the input and asked to print S.
Constraints
- 1 \leq A \leq 10
- 0 \leq B \leq A
- A and B are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
Print S in the format specified in the Problem Statement. Note that answers in different formats will be considered wrong.
Sample Input 1
7 4
Sample Output 1
0.571
As explained in the Problem Statement, \dfrac{B}{A} = \dfrac{4}{7} = 0.571428\dots rounded off to three decimal digits is 0.571. Thus, S is 0.571.
Sample Input 2
7 3
Sample Output 2
0.429
\dfrac{B}{A} = \dfrac{3}{7} = 0.428571\dots rounded off to three decimal digits is 0.429. (Note that it got rounded up.)
Thus, S is 0.429.
Sample Input 3
2 1
Sample Output 3
0.500
\dfrac{B}{A} = \dfrac{1}{2} = 0.5 rounded off to three decimal digits is again 0.5.
Thus, S is 0.500. Note that it must have three decimal places.
Sample Input 4
10 10
Sample Output 4
1.000
Sample Input 5
1 0
Sample Output 5
0.000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
3 桁の正整数 N が与えられます。 N を十進法で表したとき、すべての桁の数字が同じであるかどうかを判定してください。
制約
- 100 \leq N \leq 999
- 入力される値は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N を十進法で表したとき、すべての桁の数字が同じであるならば Yes を、同じでないならば No を 1 行で出力せよ。
入力例 1
444
出力例 1
Yes
444 の各桁の数字は 4,4,4 で同じなので Yes を出力してください。
入力例 2
160
出力例 2
No
160 の各桁の数字は 1,6,0 で同じではないので、No を出力してください。
入力例 3
999
出力例 3
Yes
Score : 100 points
Problem Statement
You are given a 3-digit positive integer N. Determine whether all digits are the same when N is represented in decimal.
Constraints
- 100 \leq N \leq 999
- The input value is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
If all digits are the same when N is represented in decimal, output Yes in one line; otherwise, output No.
Sample Input 1
444
Sample Output 1
Yes
The digits of 444 are 4,4,4, which are the same, so output Yes.
Sample Input 2
160
Sample Output 2
No
The digits of 160 are 1,6,0, which are not the same, so output No.
Sample Input 3
999
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
N 人の学生が試験を受けました。学生には学生 1, 学生 2, \dots, 学生 N と番号がついていて、学生 i は a_i 点を取りました。
P 点未満の点数を取った学生は "不可" となり単位を取得できません。 "不可" となった学生の人数を答えてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq P \leq 100
- 0 \leq a_i \leq 100 (1 \leq i \leq N)
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N P a_1 a_2 \dots a_N
出力
"不可" となった学生の人数を出力せよ。
入力例 1
4 50 80 60 40 0
出力例 1
2
学生 1 は 80 点、学生 2 は 60 点と、 50 点以上の点数を取っているので "不可" とならず単位を取得できています。
一方、学生 3 は 40 点、学生 4 は 0 点で、 50 点を下回る点数を取っているので "不可" となります。よって答えは 2 人です。
入力例 2
3 90 89 89 89
出力例 2
3
入力例 3
2 22 6 37
出力例 3
1
Score : 200 points
Problem Statement
N students took an exam. The students are labeled as Student 1, Student 2, \dots, Student N, and Student i scored a_i points.
A student who scored less than P points are considered to have failed the exam and cannot earn the credit. Find the number of students who failed the exam.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq P \leq 100
- 0 \leq a_i \leq 100 (1 \leq i \leq N)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N P a_1 a_2 \dots a_N
Output
Print the number of students who failed the exam.
Sample Input 1
4 50 80 60 40 0
Sample Output 1
2
Students 1 and 2, who scored 80 and 60 points, respectively, succeeded in scoring at least 50 points to earn the credit.
On the other hand, Students 3 and 4, who scored 40 and 0 points, respectively, fell below 50 points and failed the exam. Thus, the answer is 2.
Sample Input 2
3 90 89 89 89
Sample Output 2
3
Sample Input 3
2 22 6 37
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
空の数列 A があります。クエリが Q 個与えられるので、与えられた順に処理してください。
クエリは次の 2 種類のいずれかです。
1 x: A の末尾に x を追加する。2 k: A の後ろから k 番目の値を求める。このクエリが与えられるとき、A の長さは k 以上であることが保証される。
制約
- 1 \leq Q \leq 100
- 1 種類目のクエリにおいて x は 1 \leq x \leq 10^9 を満たす整数
- 2 種類目のクエリにおいて k はその時点の数列 A の長さ以下の正の整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
クエリは以下の 2 つのいずれかの形式である。
1 x
2 k
出力
2 種類目のクエリの個数を q として q 行出力せよ。
i 行目にはそのような i 回目のクエリに対する答えを出力せよ。
入力例 1
5 1 20 1 30 2 1 1 40 2 3
出力例 1
30 20
- 最初 A は空である。
- 1 番目のクエリにより A の末尾に 20 が追加され A=(20) となる。
- 2 番目のクエリにより A の末尾に 30 が追加され A=(20,30) となる。
- 3 番目のクエリの答えは A=(20,30) の後ろから 1 番目の値の 30 である。
- 4 番目のクエリにより A の末尾に 40 が追加され A=(20,30,40) となる。
- 5 番目のクエリの答えは A=(20,30,40) の後ろから 3 番目の値の 20 である。
Score: 200 points
Problem Statement
You have an empty sequence A. There are Q queries given, and you need to process them in the order they are given.
The queries are of the following two types:
1 x: Append x to the end of A.2 k: Find the k-th value from the end of A. It is guaranteed that the length of A is at least k when this query is given.
Constraints
- 1 \leq Q \leq 100
- In the first type of query, x is an integer satisfying 1 \leq x \leq 10^9.
- In the second type of query, k is a positive integer not greater than the current length of sequence A.
Input
The input is given from Standard Input in the following format:
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is in one of the following two formats:
1 x
2 k
Output
Print q lines, where q is the number of queries of the second type.
The i-th line should contain the answer to the i-th such query.
Sample Input 1
5 1 20 1 30 2 1 1 40 2 3
Sample Output 1
30 20
- Initially, A is empty.
- The first query appends 20 to the end of A, making A=(20).
- The second query appends 30 to the end of A, making A=(20,30).
- The answer to the third query is 30, which is the 1-st value from the end of A=(20,30).
- The fourth query appends 40 to the end of A, making A=(20,30,40).
- The answer to the fifth query is 20, which is the 3-rd value from the end of A=(20,30,40).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
N 個の料理があり、i 個目の料理の甘さは A_i、しょっぱさは B_i です。
高橋君はこれらの N 個の料理を好きな順番で並べ、その順に食べようとします。
高橋君は並べた順番の通りに料理を食べていきますが、食べた料理の甘さの合計が X より大きくなるかしょっぱさの合計が Y より大きくなるとその時点で食べるのをやめます。
高橋君が食べることになる料理の個数としてあり得る最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq X, Y \leq 2 \times 10^{14}
- 1 \leq A_i, B_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X Y A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
出力
答えを出力せよ。
入力例 1
4 7 18 2 3 5 1 8 8 1 4
出力例 1
2
i 個目の料理のことを料理 i と書きます。
高橋君が 4 個の料理を料理 2, 3, 1, 4 の順に並べ替えたとき、料理 2, 3 を食べた時点での食べた料理の甘さの合計が 8 となり 7 より大きくなります。したがってこの場合は高橋君が食べることになる料理の個数は 2 個です。
高橋君が食べる料理の個数が 1 個以下になることはないため、2 を出力します。
入力例 2
5 200000000000000 200000000000000 1 1 1 1 1 2 2 2 2 2
出力例 2
5
入力例 3
8 30 30 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1
出力例 3
6
Score : 250 points
Problem Statement
There are N dishes, and the i-th dish has a sweetness of A_i and a saltiness of B_i.
Takahashi plans to arrange these N dishes in any order he likes and eat them in that order.
He will eat the dishes in the arranged order, but he will stop eating as soon as the total sweetness of the dishes he has eaten exceeds X or the total saltiness exceeds Y.
Find the minimum possible number of dishes that he will end up eating.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq X, Y \leq 2 \times 10^{14}
- 1 \leq A_i, B_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X Y A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
Output
Print the answer.
Sample Input 1
4 7 18 2 3 5 1 8 8 1 4
Sample Output 1
2
The i-th dish will be denoted as dish i.
If he arranges the four dishes in the order 2, 3, 1, 4, as soon as he eats dishes 2 and 3, their total sweetness is 8, which is greater than 7. Therefore, in this case, he will end up eating two dishes.
The number of dishes he will eat cannot be 1 or less, so print 2.
Sample Input 2
5 200000000000000 200000000000000 1 1 1 1 1 2 2 2 2 2
Sample Output 2
5
Sample Input 3
8 30 30 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1
Sample Output 3
6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
長さ N の文字列 S が与えられます。クエリが Q 個与えられるので、順番に処理してください。
i 番目のクエリは以下の通りです。
- 整数 X_i と文字 C_i が与えられるので、 S の X_i 番目の文字を C_i に置き換える。その後、 S に文字列
ABCが部分文字列として何箇所含まれるかを出力する。
ここで、 S の 部分文字列 とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。
例えば、ab は abc の部分文字列ですが、ac は abc の部分文字列ではありません。
制約
- 3\le N\le 2\times 10^5
- 1\le Q\le 2\times 10^5
- S は英大文字からなる長さ N の文字列
- 1\le X_i\le N
- C_i は英大文字
入力
入力は以下の形式で標準入力から与えられる。
N Q S X_1 C_1 X_2 C_2 \vdots X_Q C_Q
出力
Q 行出力せよ。 i 行目 (1\le i\le Q) には i 番目のクエリに対する答えを出力せよ。
入力例 1
7 4 ABCDABC 4 B 3 A 5 C 4 G
出力例 1
2 1 1 0
各クエリを処理した後の S は次のようになります。
- 1 つ目のクエリを処理後: S=
ABCBABCとなる。この中にABCは部分文字列として 2 回登場する。 - 2 つ目のクエリを処理後: S=
ABABABCとなる。この中にABCは部分文字列として 1 回登場する。 - 3 つ目のクエリを処理後: S=
ABABCBCとなる。この中にABCは部分文字列として 1 回登場する。 - 4 つ目のクエリを処理後: S=
ABAGCBCとなる。この中にABCは部分文字列として 0 回登場する。
入力例 2
3 3 ABC 1 A 2 B 3 C
出力例 2
1 1 1
クエリの処理前と処理後で S が変わらない場合もあります。
入力例 3
15 10 BBCCBCACCBACACA 9 C 11 B 5 B 11 B 4 A 8 C 8 B 5 B 7 B 14 B
出力例 3
0 0 0 0 1 1 2 2 1 1
Score : 350 points
Problem Statement
You are given a string S of length N. You are also given Q queries, which you should process in order.
The i-th query is as follows:
- Given an integer X_i and a character C_i, replace the X_i-th character of S with C_i. Then, print the number of times the string
ABCappears as a substring in S.
Here, a substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.
For example, ab is a substring of abc, but ac is not a substring of abc.
Constraints
- 3 \le N \le 2 \times 10^5
- 1 \le Q \le 2 \times 10^5
- S is a string of length N consisting of uppercase English letters.
- 1 \le X_i \le N
- C_i is an uppercase English letter.
Input
The input is given from Standard Input in the following format:
N Q S X_1 C_1 X_2 C_2 \vdots X_Q C_Q
Output
Print Q lines. The i-th line (1 \le i \le Q) should contain the answer to the i-th query.
Sample Input 1
7 4 ABCDABC 4 B 3 A 5 C 4 G
Sample Output 1
2 1 1 0
After processing each query, S becomes as follows.
- After the first query: S=
ABCBABC. In this string,ABCappears twice as a substring. - After the second query: S=
ABABABC. In this string,ABCappears once as a substring. - After the third query: S=
ABABCBC. In this string,ABCappears once as a substring. - After the fourth query: S=
ABAGCBC. In this string,ABCappears zero times as a substring.
Sample Input 2
3 3 ABC 1 A 2 B 3 C
Sample Output 2
1 1 1
There are cases where S does not change through processing a query.
Sample Input 3
15 10 BBCCBCACCBACACA 9 C 11 B 5 B 11 B 4 A 8 C 8 B 5 B 7 B 14 B
Sample Output 3
0 0 0 0 1 1 2 2 1 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
黒板に整数 X が 1 つ書かれています。
Q 個のクエリが与えられるので、順に処理してください。i 個目 (1\le i\le Q) のクエリは以下の通りです。
2 つの整数 A_i,B_i が与えられる。黒板に新たに 2 つの整数 A_i,B_i を書く。
その後、黒板に書かれた 2i+1 個の整数の中央値を出力する。
制約
- 1\le X\le 10^9
- 1\le Q\le 2\times 10^5
- 1\le A_i,B_i\le 10^9
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
X Q A_1 B_1 A_2 B_2 \vdots A_Q B_Q
出力
Q 行出力せよ。
i 行目には、i 番目のクエリに対する答えを出力せよ。
入力例 1
5 3 2 3 1 2 8 9
出力例 1
3 2 3
1 つ目のクエリでは黒板に書かれた整数は 5,2,3 となり、これらの整数の中央値は 3 です。
2 つ目のクエリでは黒板に書かれた整数は 5,2,3,1,2 となり、これらの整数の中央値は 2 です。
3 つ目のクエリでは黒板に書かれた整数は 5,2,3,1,2,8,9 となり、これらの整数の中央値は 3 です。
入力例 2
1 4 2 3 4 5 6 7 8 9
出力例 2
2 3 4 5
入力例 3
278117031 7 167642909 517897721 148434323 567739597 319926999 481642530 659199879 252516557 49913403 798318034 89701408 892537201 199166668 742285869
出力例 3
278117031 278117031 319926999 319926999 319926999 319926999 319926999
Score : 400 points
Problem Statement
There is one integer X written on a blackboard.
You are given Q queries to process in order. The i-th query (1 \le i \le Q) is as follows.
Two integers A_i and B_i are given. Write two new integers A_i and B_i on the blackboard.
Then, output the median of the 2i+1 integers written on the blackboard.
Constraints
- 1 \le X \le 10^9
- 1 \le Q \le 2 \times 10^5
- 1 \le A_i, B_i \le 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
X Q A_1 B_1 A_2 B_2 \vdots A_Q B_Q
Output
Output Q lines.
The i-th line should contain the answer to the i-th query.
Sample Input 1
5 3 2 3 1 2 8 9
Sample Output 1
3 2 3
In the first query, the integers written on the blackboard become 5, 2, 3, and their median is 3.
In the second query, the integers written on the blackboard become 5, 2, 3, 1, 2, and their median is 2.
In the third query, the integers written on the blackboard become 5, 2, 3, 1, 2, 8, 9, and their median is 3.
Sample Input 2
1 4 2 3 4 5 6 7 8 9
Sample Output 2
2 3 4 5
Sample Input 3
278117031 7 167642909 517897721 148434323 567739597 319926999 481642530 659199879 252516557 49913403 798318034 89701408 892537201 199166668 742285869
Sample Output 3
278117031 278117031 319926999 319926999 319926999 319926999 319926999
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
長さ N の数列 A=(A_1,\ldots,A_N) が与えられます。
\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^{N}\left\lfloor\frac{\max(A_i,A_j)}{\min(A_i,A_j)}\right\rfloor を求めてください。
ただし、\lfloor x \rfloor は x 以下の最大の整数を表します。例えば、\lfloor 3.14 \rfloor=3、\lfloor 2 \rfloor=2 です。
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq A_i \leq 10^6
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
答えを出力せよ。
入力例 1
3 3 1 4
出力例 1
8
求める値は
\left\lfloor\frac{\max(3,1)}{\min(3,1)}\right\rfloor + \left\lfloor\frac{\max(3,4)}{\min(3,4)}\right\rfloor + \left\lfloor\frac{\max(1,4)}{\min(1,4)}\right\rfloor\\ =\left\lfloor\frac{3}{1}\right\rfloor + \left\lfloor\frac{4}{3}\right\rfloor + \left\lfloor\frac{4}{1}\right\rfloor\\ =3+1+4\\ =8
となります。
入力例 2
6 2 7 1 8 2 8
出力例 2
53
入力例 3
12 3 31 314 3141 31415 314159 2 27 271 2718 27182 271828
出力例 3
592622
Score : 475 points
Problem Statement
You are given a sequence A=(A_1,\ldots,A_N) of length N.
Find \displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^{N}\left\lfloor\frac{\max(A_i,A_j)}{\min(A_i,A_j)}\right\rfloor.
Here, \lfloor x \rfloor represents the greatest integer not greater than x. For example, \lfloor 3.14 \rfloor=3 and \lfloor 2 \rfloor=2.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1 \leq A_i \leq 10^6
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Print the answer.
Sample Input 1
3 3 1 4
Sample Output 1
8
The sought value is
\left\lfloor\frac{\max(3,1)}{\min(3,1)}\right\rfloor + \left\lfloor\frac{\max(3,4)}{\min(3,4)}\right\rfloor + \left\lfloor\frac{\max(1,4)}{\min(1,4)}\right\rfloor\\ =\left\lfloor\frac{3}{1}\right\rfloor + \left\lfloor\frac{4}{3}\right\rfloor + \left\lfloor\frac{4}{1}\right\rfloor\\ =3+1+4\\ =8.
Sample Input 2
6 2 7 1 8 2 8
Sample Output 2
53
Sample Input 3
12 3 31 314 3141 31415 314159 2 27 271 2718 27182 271828
Sample Output 3
592622
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
N 頂点の木が与えられます。
頂点は頂点 1、頂点 2、\ldots、頂点 N と番号づけられており、i 番目 (1\leq i\leq N-1) の辺は頂点 U_i と頂点 V_i を結んでいます。
高橋君は各頂点を色 1、色 2、\ldots、色 K のうちいずれか一色で塗ります。
色 i は最大で C_i 個の頂点を塗るのに使うことができます。
次の条件をみたすように頂点を塗ることが可能か判定し、可能な場合は条件をみたす塗り方を 1 つ出力してください。
- どの辺についても、ある i (1\leq i\leq K) が存在して、その辺で木を切って得られる 2 つの部分木の両方に色 i で塗られた頂点が存在する。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T\leq 10^5
- 2\leq N\leq 3\times 10^5
- 1\leq K\leq N
- 1\leq U_i,V_i\leq N
- 与えられるグラフは木である。
- 1\leq C_i\leq N
- C_1+C_2+\cdots+C_K\geq N
- 入力はすべて整数
- 各入力のテストケースにわたる N の総和は 3\times 10^5 を超えない。
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i は i 個目のテストケースを表す。
各テストケースは以下の形式で与えられる。
N K
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
C_1 C_2 \ldots C_K
出力
T 行出力せよ。
i 行目 (1\leq i\leq T) には、i 個目のテストケースに対する答えを以下のように出力せよ。
条件をみたすように頂点を塗ることが不可能ならば、-1 を 1 つだけ出力せよ。
そうでないならば、N 個の整数 X_1,X_2,\ldots,X_N (1\leq X_i\leq K) を空白区切りで出力せよ。 ここで、j=1,2,\ldots,N について頂点 i を色 X_i で塗った方法が条件をみたすようにせよ。
入力例 1
2 5 3 1 2 1 3 2 4 2 5 2 2 2 3 3 1 2 2 3 1 1 1
出力例 1
3 2 2 1 1 -1
1 個目のテストケースについて、出力例のように頂点を塗ったとします。
すなわち、頂点 1 に色 3 を、頂点 2 に色 2 を、頂点 3 に色 2 を、頂点 4 に色 1 を、頂点 5 に色 1 を塗ったとします。
このとき、頂点 1 と頂点 2 を結ぶ辺を切ったとすると、木は頂点 1,3 からなる部分木と頂点 2,4,5 からなる部分木に分かれますが、このときどちらにも色 2 で塗られた頂点(頂点 3 または頂点 2)が存在します。
与えられた木を他のどの辺で切ってもそのような色が存在するため、出力例の塗り方は条件をみたしています。
2 個目のテストケースについて、条件をみたすように頂点を塗る方法は存在しません。
Score : 550 points
Problem Statement
You are given a tree with N vertices.
The vertices are numbered vertex 1, vertex 2, \ldots, vertex N, and the i-th edge (1\leq i\leq N-1) connects vertices U_i and V_i.
Takahashi will color each vertex with one of colors 1, 2, \ldots, K.
Color i can be used to color at most C_i vertices.
Determine whether it is possible to color the vertices satisfying the following condition, and if so, output one valid coloring.
- For every edge, there exists some i (1\leq i\leq K) such that both of the two subtrees obtained by cutting the tree at that edge contain at least one vertex colored with color i.
You are given T test cases; solve each of them.
Constraints
- 1\leq T\leq 10^5
- 2\leq N\leq 3\times 10^5
- 1\leq K\leq N
- 1\leq U_i,V_i\leq N
- The given graph is a tree.
- 1\leq C_i\leq N
- C_1+C_2+\cdots+C_K\geq N
- All input values are integers.
- The sum of N over all test cases does not exceed 3\times 10^5.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i represents the i-th test case. Each test case is given in the following format:
N K
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
C_1 C_2 \ldots C_K
Output
Output T lines.
On the i-th line (1\leq i\leq T), output the answer for the i-th test case as follows.
If it is impossible to color the vertices satisfying the condition, output -1 alone.
Otherwise, output N integers X_1,X_2,\ldots,X_N (1\leq X_i\leq K) separated by spaces, such that coloring vertex i with color X_i for i=1,2,\ldots,N satisfies the condition.
Sample Input 1
2 5 3 1 2 1 3 2 4 2 5 2 2 2 3 3 1 2 2 3 1 1 1
Sample Output 1
3 2 2 1 1 -1
For the first test case, suppose the vertices are colored as in the sample output.
That is, vertex 1 is colored with color 3, vertex 2 with color 2, vertex 3 with color 2, vertex 4 with color 1, and vertex 5 with color 1.
If we cut the edge connecting vertices 1 and 2, the tree splits into the subtree consisting of vertices 1,3 and the subtree consisting of vertices 2,4,5; both subtrees contain a vertex colored with color 2 (vertex 3 or vertex 2, respectively).
Such a color exists no matter which edge of the given tree is cut, so the coloring in the sample output satisfies the condition.
For the second test case, there is no valid coloring satisfying the condition.