Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
1 フィートは 12 インチです。
A フィート B インチは、インチ換算で何インチですか?
制約
- 1 \leq A \leq 8
- 0 \leq B \leq 11
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
答えを 1 行に出力せよ。単位 (インチ) は省いて出力すること。
入力例 1
6 7
出力例 1
79
6 フィート 7 インチは、インチ換算で 6 \times 12 + 7 = 79 インチです。
入力例 2
4 11
出力例 2
59
4 フィート 11 インチは、インチ換算で 4 \times 12 + 11 = 59 インチです。
入力例 3
8 0
出力例 3
96
8 フィート 0 インチは、インチ換算で 8 \times 12 + 0 = 96 インチです。
Score : 100 points
Problem Statement
1 foot is 12 inches.
How many inches is A feet B inches?
Constraints
- 1 \leq A \leq 8
- 0 \leq B \leq 11
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
Output the answer in one line. Omit the unit (inches).
Sample Input 1
6 7
Sample Output 1
79
6 feet 7 inches is 6 \times 12 + 7 = 79 inches.
Sample Input 2
4 11
Sample Output 2
59
4 feet 11 inches is 4 \times 12 + 11 = 59 inches.
Sample Input 3
8 0
Sample Output 3
96
8 feet 0 inches is 8 \times 12 + 0 = 96 inches.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
H 行 W 列からなるグリッドがあります。各マスには整数が 1 つずつ書かれており、これらの整数は相異なります。グリッドの上から i 行目・左から j 行目のマスには整数 A_{i,j} が書かれています。
いま、司会が N 個の相異なる整数 B_1, \dots, B_N を叫びました。
それぞれの行に対して、司会の叫んだ整数がその行に何個含まれるかを求めたとき、それらの最大値はいくつになりますか?
制約
- 1 \leq H \leq 3
- 1 \leq W \leq 5
- 1 \leq N \leq 90
- 1 \leq A_{i,j} \leq 90
- A_{i,j} は相異なる
- 1 \leq B_i \leq 90
- B_i は相異なる
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W N
A_{1,1} \cdots A_{1,W}
\vdots
A_{H,1} \cdots A_{H,W}
B_1
\vdots
B_N
出力
答えを 1 行に出力せよ。
入力例 1
3 4 5 12 3 5 7 6 10 11 9 1 2 4 8 2 4 9 6 11
出力例 1
3
- 上から 1 行目にある整数のうち、司会の叫んだ整数は 0 個です。
- 上から 2 行目にある整数のうち、司会の叫んだ整数は 6,11,9 の 3 個です。
- 上から 3 行目にある整数のうち、司会の叫んだ整数は 2,4 の 2 個です。
以上より、0,3,2 のうちの最大値である 3 が答えです。
入力例 2
3 5 2 81 63 31 16 15 30 3 6 54 24 26 41 48 64 66 44 79
出力例 2
0
入力例 3
3 5 12 78 19 70 58 83 12 30 80 20 27 48 71 8 43 82 82 30 43 8 80 70 20 78 12 71 19 48
出力例 3
5
Score : 200 points
Problem Statement
There is a grid with H rows and W columns. Each square has one integer written on it, and these integers are distinct. The square at the i-th row from the top and j-th column from the left has the integer A_{i,j} written on it.
Now, the host called out N distinct integers B_1, \dots, B_N.
If you find, for each row, how many of the integers called out by the host are contained in that row, what is the maximum value among these?
Constraints
- 1 \leq H \leq 3
- 1 \leq W \leq 5
- 1 \leq N \leq 90
- 1 \leq A_{i,j} \leq 90
- A_{i,j} are distinct.
- 1 \leq B_i \leq 90
- B_i are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W N
A_{1,1} \cdots A_{1,W}
\vdots
A_{H,1} \cdots A_{H,W}
B_1
\vdots
B_N
Output
Output the answer in one line.
Sample Input 1
3 4 5 12 3 5 7 6 10 11 9 1 2 4 8 2 4 9 6 11
Sample Output 1
3
- Among the integers in the 1-st row from the top, 0 integers were called out by the host.
- Among the integers in the 2-nd row from the top, 3 integers 6,11,9 were called out by the host.
- Among the integers in the 3-rd row from the top, 2 integers 2,4 were called out by the host.
Thus, the answer is the maximum value among 0,3,2, which is 3.
Sample Input 2
3 5 2 81 63 31 16 15 30 3 6 54 24 26 41 48 64 66 44 79
Sample Output 2
0
Sample Input 3
3 5 12 78 19 70 58 83 12 30 80 20 27 48 71 8 43 82 82 30 43 8 80 70 20 78 12 71 19 48
Sample Output 3
5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
N 匹のトナカイと 1 個のソリがあります。i 番目のトナカイは重さが W_i で力は P_i です。
各トナカイについて、「ソリを引く」または「ソリに乗る」のいずれかを選びます。 ただし、ソリを引くトナカイの力の総和が、ソリに乗るトナカイの重さの総和以上でなければなりません。 最大で何匹トナカイをソリに乗せることができますか?
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T\leq 10^5
- 1\leq N\leq 3\times 10^5
- 1\leq W_i,P_i\leq 10^9
- 入力は全て整数
- 1 つの入力ファイルに含まれる N の総和は 3\times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N W_1 P_1 W_2 P_2 \vdots W_N P_N
出力
T 行出力せよ。i 行目には i 番目のテストケースに対する答えを出力せよ。
入力例 1
3 3 3 1 4 1 5 9 5 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 1 10 133180711 458704923 531424946 225863856 141986070 637075158 500770732 289806469 502866767 408857335 559714289 569084545 287444582 992432993 559747907 753133304 432846188 949871298 727072164 756020367
出力例 1
2 0 6
1 つ目のテストケースについて、3 番目のトナカイがソリを引き、1 番目と 2 番目のトナカイがソリに乗ると、ソリを引くトナカイの力の総和は P_3=9、ソリに乗るトナカイの重さの総和は W_1+W_2=7 となるため条件を満たします。すべてのトナカイがソリに乗ることはできないため、求める答えは 2 です。
Score : 350 points
Problem Statement
There are N reindeer and one sled. The i-th reindeer has weight W_i and strength P_i.
For each reindeer, choose either "pull the sled" or "ride on the sled". Here, the total strength of the reindeer pulling the sled must be greater than or equal to the total weight of the reindeer riding on the sled. What is the maximum number of reindeer that can ride on the sled?
You are given T test cases. Solve each of them.
Constraints
- 1\leq T\leq 10^5
- 1\leq N\leq 3\times 10^5
- 1\leq W_i,P_i\leq 10^9
- All input values are integers.
- The sum of N in one input file is at most 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
Each test case is given in the following format:
N W_1 P_1 W_2 P_2 \vdots W_N P_N
Output
Output T lines. The i-th line should contain the answer for the i-th test case.
Sample Input 1
3 3 3 1 4 1 5 9 5 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 1 10 133180711 458704923 531424946 225863856 141986070 637075158 500770732 289806469 502866767 408857335 559714289 569084545 287444582 992432993 559747907 753133304 432846188 949871298 727072164 756020367
Sample Output 1
2 0 6
For the 1st test case, if the 3rd reindeer pulls the sled and the 1st and 2nd reindeer ride on the sled, the total strength of the reindeer pulling the sled is P_3=9, and the total weight of the reindeer riding on the sled is W_1+W_2=7, satisfying the condition. Since not all reindeer can ride on the sled, the answer is 2.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ N の正整数列 A = (A_1, A_2, \dots, A_N) および長さ M の正整数列 B = (B_1, B_2, \dots, B_M) が与えられます。
\displaystyle \sum_{i=1}^{N} \sum_{j=1}^{M} |A_i - B_j| の値を 998244353 で割ったあまりを求めてください。
制約
- 1 \leq N,M \leq 3 \times 10^5
- 1 \leq A_i, B_j < 998244353
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \cdots A_N B_1 B_2 \cdots B_M
出力
答えを 1 行に出力せよ。
入力例 1
4 2 1 6 9 2 3 1
出力例 1
26
答えは |1-3| + |1-1| + |6-3| + |6-1| + |9-3| + |9-1| + |2-3| + |2-1| = 2+0+3+5+6+8+1+1 = 26 です。
入力例 2
8 8 185991676 311812083 311812083 84357963 185991676 185991676 724020528 369175631 455049197 387671868 4361724 724020528 724020528 455049197 455049197 724020528
出力例 2
529117255
Score : 400 points
Problem Statement
You are given a length-N sequence of positive integers A = (A_1, A_2, \dots, A_N) and a length-M sequence of positive integers B = (B_1, B_2, \dots, B_M).
Find the value of \displaystyle \sum_{i=1}^{N} \sum_{j=1}^{M} |A_i - B_j|, modulo 998244353.
Constraints
- 1 \leq N,M \leq 3 \times 10^5
- 1 \leq A_i, B_j < 998244353
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \cdots A_N B_1 B_2 \cdots B_M
Output
Output the answer in one line.
Sample Input 1
4 2 1 6 9 2 3 1
Sample Output 1
26
The answer is |1-3| + |1-1| + |6-3| + |6-1| + |9-3| + |9-1| + |2-3| + |2-1| = 2+0+3+5+6+8+1+1 = 26.
Sample Input 2
8 8 185991676 311812083 311812083 84357963 185991676 185991676 724020528 369175631 455049197 387671868 4361724 724020528 724020528 455049197 455049197 724020528
Sample Output 2
529117255
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
N+1 個の数列 A_0, A_1, \ldots, A_{N} があります。A_i は以下のように定義されます。
- A_0 は空の数列
- A_i\ (1\leq i\leq N) は数列 A_{x_i}\ (0\leq x_i\lt i) の末尾に整数 y_i を追加することで得られる数列
以下の条件を満たす (1,2,\ldots,N) の順列 P=(P_1, P_2,\ldots,P_N) を求めてください。
- i = 1,2,\ldots,N-1 に対して、以下のいずれかが成り立つ。
- A_{P_i} は辞書順で A_{P_{i+1}} より小さい。
- A_{P_i}= A_{P_{i+1}} かつ P_i\lt P_{i+1}
言い換えると、A_1,A_2,\ldots,A_N を辞書順で小さいものから順に並べたとき(等しい列が複数ある場合には添え字が小さいものを先にするように並べる)、その並びに現れる添え字の列が P です。
数列の辞書順とは?
数列 S = (S_1,S_2,\ldots,S_{|S|}) が数列 T = (T_1,T_2,\ldots,T_{|T|}) より辞書順で小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。 ここで、|S|, |T| はそれぞれ S, T の長さを表します。
- |S| \lt |T| かつ (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|})。
- ある整数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存在して、下記の 2 つがともに成り立つ。
- (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
- S_i が T_i より(数として)小さい。
制約
- 1\leq N\leq 3\times 10^5
- 0\leq x_i\lt i
- 1\leq y_i\leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N x_1 y_1 x_2 y_2 \vdots x_N y_N
出力
P_1, P_2, \ldots, P_N を空白区切りで一行で出力せよ。
入力例 1
4 0 2 0 1 2 2 0 1
出力例 1
2 4 3 1
A_1 = (2), A_2 = (1), A_3 = (1, 2), A_4 = (1) なので P=(2,4,3,1) です。
入力例 2
5 0 1 0 1 0 1 0 1 0 1
出力例 2
1 2 3 4 5
A_1 = A_2 = A_3 = A_4 = A_5 = (1) です。
入力例 3
10 0 305186313 1 915059758 0 105282054 1 696409999 3 185928366 3 573289179 6 254538849 3 105282054 5 696409999 8 168629803
出力例 3
3 8 10 5 9 6 7 1 4 2
Score : 450 points
Problem Statement
There are N+1 sequences A_0, A_1, \ldots, A_{N}. A_i is defined as follows:
- A_0 is an empty sequence.
- A_i\ (1\leq i\leq N) is a sequence obtained by appending an integer y_i to the end of the sequence A_{x_i}\ (0\leq x_i\lt i).
Find the permutation P=(P_1, P_2,\ldots,P_N) of (1,2,\ldots,N) that satisfies the following condition:
- For i = 1,2,\ldots,N-1, one of the following holds:
- A_{P_i} is lexicographically smaller than A_{P_{i+1}}.
- A_{P_i}= A_{P_{i+1}} and P_i\lt P_{i+1}
In other words, when A_1,A_2,\ldots,A_N are arranged in lexicographical order (when there are multiple equal sequences, arrange those with smaller indices first), P is the sequence of indices that appears in that arrangement.
What is the lexicographical order of sequences?
A sequence S = (S_1,S_2,\ldots,S_{|S|}) is lexicographically smaller than a sequence T = (T_1,T_2,\ldots,T_{|T|}) if one of the following two conditions holds. Here, |S| and |T| represent the lengths of S and T, respectively.
- |S| \lt |T| and (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|}).
- There exists an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace such that both of the following hold:
- (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
- S_i is (numerically) smaller than T_i.
Constraints
- 1\leq N\leq 3\times 10^5
- 0\leq x_i\lt i
- 1\leq y_i\leq 10^9
- All input values 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
Output
Output P_1, P_2, \ldots, P_N in one line, separated by spaces.
Sample Input 1
4 0 2 0 1 2 2 0 1
Sample Output 1
2 4 3 1
A_1 = (2), A_2 = (1), A_3 = (1, 2), A_4 = (1), so P=(2,4,3,1).
Sample Input 2
5 0 1 0 1 0 1 0 1 0 1
Sample Output 2
1 2 3 4 5
A_1 = A_2 = A_3 = A_4 = A_5 = (1).
Sample Input 3
10 0 305186313 1 915059758 0 105282054 1 696409999 3 185928366 3 573289179 6 254538849 3 105282054 5 696409999 8 168629803
Sample Output 3
3 8 10 5 9 6 7 1 4 2
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
二次元平面上に N 個のクリスマスツリーがあります。i 番目 (1\le i\le N) のクリスマスツリーは座標 (X_i,Y_i) に存在します。
Q 個のクエリが与えられるので、順にクエリを処理してください。各クエリは、以下のいずれかです。
- タイプ 1 :
1 i x yの形式で与えられる。i 番目のクリスマスツリーの座標を (x,y) に変更する。 - タイプ 2 :
2 L R x yの形式で与えられる。L,L+1,\ldots,R 番目のクリスマスツリーのうち、座標 (x,y) からマンハッタン距離で最も遠いクリスマスツリーまでの距離を出力する。
ただし、座標 (x_1,y_1) と座標 (x_2,y_2) のマンハッタン距離は |x_1-x_2|+|y_1-y_2| で定義されます。
制約
- 1\le N,Q\le 2\times 10^5
- -10^9\le X_i,Y_i\le 10^9
- 1\le i\le N
- 1\le L\le R\le N
- -10^9\le x,y\le 10^9
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
ここで、 i 番目のクエリ \text{query}_i は以下のいずれかの形式で与えられる。
1 i x y
2 L R x y
出力
問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。
入力例 1
3 4 -1 -1 1 2 -2 1 2 1 2 0 0 2 1 3 -1 2 1 1 0 1 2 1 3 -1 2
出力例 1
3 3 2
はじめ、1,2,3 番目のクリスマスツリーはそれぞれ座標 (-1,-1),(1,2),(-2,1) に存在します。
各クエリは以下のように処理されます。
- 1,2 番目のクリスマスツリーと座標 (0,0) のマンハッタン距離はそれぞれ 2,3 です。したがって、 2,3 の最大値である 3 を出力します。
- 1,2,3 番目のクリスマスツリーと座標 (-1,2) のマンハッタン距離はそれぞれ 3,2,2 です。したがって、 3,2,2 の最大値である 3 を出力します。
- 1 番目のクリスマスツリーの座標を (0,1) に変更します。1,2,3 番目のクリスマスツリーの座標はそれぞれ (0,1),(1,2),(-2,1) になります。
- 1,2,3 番目のクリスマスツリーと座標 (-1,2) のマンハッタン距離はそれぞれ 2,2,2 です。したがって、 2,2,2 の最大値である 2 を出力します。
入力例 2
5 7 -9 5 -2 -9 10 -6 9 8 2 9 1 3 -9 -6 2 3 4 2 7 1 4 -2 -10 2 1 2 0 -10 2 3 4 10 -9 2 3 4 8 7 2 5 5 0 2
出力例 2
24 24 22 30 9
Score : 500 points
Problem Statement
There are N Christmas trees on a two-dimensional plane. The i-th (1\le i\le N) Christmas tree is located at coordinates (X_i,Y_i).
You are given Q queries. Process the queries in order. Each query is one of the following:
- Type 1 : Given in the form
1 i x y. Change the coordinates of the i-th Christmas tree to (x,y). - Type 2 : Given in the form
2 L R x y. Output the Manhattan distance from the coordinates (x,y) to the farthest Christmas tree among the L,L+1,\ldots,R-th Christmas trees.
Here, the Manhattan distance between coordinates (x_1,y_1) and coordinates (x_2,y_2) is defined as |x_1-x_2|+|y_1-y_2|.
Constraints
- 1\le N,Q\le 2\times 10^5
- -10^9\le X_i,Y_i\le 10^9
- 1\le i\le N
- 1\le L\le R\le N
- -10^9\le x,y\le 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Here, the i-th query \text{query}_i is given in one of the following formats:
1 i x y
2 L R x y
Output
Output the answers to the queries, separated by newlines, according to the instructions in the problem statement.
Sample Input 1
3 4 -1 -1 1 2 -2 1 2 1 2 0 0 2 1 3 -1 2 1 1 0 1 2 1 3 -1 2
Sample Output 1
3 3 2
Initially, the 1st, 2nd, 3rd Christmas trees are located at coordinates (-1,-1), (1,2), (-2,1), respectively.
Each query is processed as follows:
- The Manhattan distances from the 1st and 2nd Christmas trees to coordinates (0,0) are 2 and 3, respectively. Thus, output 3, which is the maximum value among 2,3.
- The Manhattan distances from the 1st, 2nd, 3rd Christmas trees to coordinates (-1,2) are 3, 2, 2, respectively. Thus, output 3, which is the maximum value among 3,2,2.
- Change the coordinates of the 1st Christmas tree to (0,1). The coordinates of the 1st, 2nd, 3rd Christmas trees become (0,1),(1,2),(-2,1), respectively.
- The Manhattan distances from the 1st, 2nd, 3rd Christmas trees to coordinates (-1,2) are 2, 2, 2, respectively. Thus, output 2, which is the maximum value among 2,2,2.
Sample Input 2
5 7 -9 5 -2 -9 10 -6 9 8 2 9 1 3 -9 -6 2 3 4 2 7 1 4 -2 -10 2 1 2 0 -10 2 3 4 10 -9 2 3 4 8 7 2 5 5 0 2
Sample Output 2
24 24 22 30 9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 625 点
問題文
今年のクリスマスの季節も終わり、いよいよ年越しです。高橋君はクリスマスツリーを片付ける大掃除に追われています。
赤・青・緑の 3 色の電球に彩られたクリスマスツリーがあります。クリスマスツリーには N 個の電球があり、それらは N-1 本のリボンで結ばれています。電球を頂点、リボンを辺とみなしたとき、このグラフは木です。
電球には 1 から N までの番号が、リボンには 1 から N-1 までの番号がつけられており、リボン i は電球 u_i と v_i を結んでいます。電球 i ははじめ、c_i が R ならば赤で、G ならば緑で、B ならば青で点灯しています。
高橋君は、以下の操作を N-1 回行い、すべてのリボンを取り外そうと考えています。
- まだ取り外されていないリボンのうち、両端の電球の色が異なるものを 1 本選び、そのリボンを取り外す。
- 取り外したリボンの両端の電球の番号を u, v とする。電球 u, v それぞれについて、点灯している色を次の規則で変更する。
- 赤で点灯していた場合、緑で点灯させる。
- 緑で点灯していた場合、青で点灯させる。
- 青で点灯していた場合、赤で点灯させる。
高橋君がこの操作を繰り返してすべてのリボンを取り外すことが可能かどうかを判定し、可能ならばそのような操作方法を 1 つ出力してください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T\leq 20000
- 2\leq N\leq 2000
- c_i は
R,G,Bのいずれか - 1\leq u_i,v_i\leq N
- 電球を頂点、リボンを辺とみなしたとき、与えられるグラフは木
- T,N,u_i,v_i は整数
- 1 つの入力ファイルに含まれる N^2 の総和は 2000^2 以下
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N
c_1c_2\ldots c_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
出力
\mathrm{case}_1, \mathrm{case}_2, \ldots, \mathrm{case}_T に対する答えを順に以下の形式で出力せよ。
リボンをすべて取り外すことが不可能な場合、No と出力せよ。
可能な場合、i 回目の操作で取り外すリボンの番号を e_i として、
Yes
e_1 e_2 \ldots e_{N-1}
と出力せよ。(e_1, e_2, \ldots, e_{N-1}) は (1, 2, \ldots, N-1) の順列である必要がある。
解が複数存在する場合、どれを出力しても正解とみなされる。
入力例 1
3 4 GBBR 1 2 1 3 1 4 3 RRR 1 2 2 3 5 RGBRG 1 2 2 3 3 4 3 5
出力例 1
Yes 1 3 2 No Yes 1 4 2 3
1 つ目のテストケースについて、例えば以下のように操作を行うことですべてのリボンを取り外すことができます。
- はじめ、各電球の色は(電球 1 から)順に緑、青、青、赤である。
- リボン 1 を取り外す。取り外した後の各電球の色は順に青、赤、青、赤である。
- リボン 3 を取り外す。取り外した後の各電球の色は順に赤、赤、青、緑である。
- リボン 2 を取り外す。取り外した後の各電球の色は順に緑、赤、赤、緑である。
条件を満たす (e_1, e_2,e_3) は (1, 3, 2) または (2, 3, 1) の 2 つであり、どちらを出力しても正解となります。
2 つ目のテストケースについて、どのように操作をしてもすべてのリボンを取り外すことはできません。
Score : 625 points
Problem Statement
The Christmas season this year is over, and it is finally time for the new year. Takahashi is busy with the big cleanup of putting away the Christmas tree.
There is a Christmas tree decorated with bulbs in three colors: red, blue, and green. The Christmas tree has N bulbs, and they are connected by N-1 ribbons. When viewing the bulbs as vertices and the ribbons as edges, this graph is a tree.
The bulbs are numbered from 1 to N, and the ribbons are numbered from 1 to N-1. Ribbon i connects bulbs u_i and v_i. Bulb i is initially lit in red if c_i is R, in green if it is G, and in blue if it is B.
Takahashi is considering performing the following operation N-1 times to remove all ribbons.
- Choose one ribbon among those not yet removed where the bulbs at both ends have different colors, and remove that ribbon.
- Let bulbs u and v be the bulbs at both ends of the removed ribbon. For each of the bulbs u and v, change the color they are lit in according to the following rule:
- If it was lit in red, light it in green.
- If it was lit in green, light it in blue.
- If it was lit in blue, light it in red.
Determine whether it is possible for Takahashi to remove all ribbons by repeating this operation. If possible, output one such method.
You are given T test cases. Solve each of them.
Constraints
- 1\leq T\leq 20000
- 2\leq N\leq 2000
- c_i is
R,G, orB. - 1\leq u_i,v_i\leq N
- When viewing the bulbs as vertices and the ribbons as edges, the given graph is a tree.
- T,N,u_i,v_i are integers.
- The sum of N^2 in one input file is at most 2000^2.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N
c_1c_2\ldots c_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
Output
Output the answers for \mathrm{case}_1, \mathrm{case}_2, \ldots, \mathrm{case}_T in order in the following format.
If it is impossible to remove all ribbons, output No.
If it is possible, let e_i be the number of the ribbon removed in the i-th operation, and output:
Yes
e_1 e_2 \ldots e_{N-1}
(e_1, e_2, \ldots, e_{N-1}) must be a permutation of (1, 2, \ldots, N-1).
If there are multiple solutions, any of them will be accepted as correct.
Sample Input 1
3 4 GBBR 1 2 1 3 1 4 3 RRR 1 2 2 3 5 RGBRG 1 2 2 3 3 4 3 5
Sample Output 1
Yes 1 3 2 No Yes 1 4 2 3
For the first test case, for example, all ribbons can be removed by performing the operations as follows:
- Initially, the colors of the bulbs are green, blue, blue, red, in order (from bulb 1).
- Remove ribbon 1. After removal, the colors of the bulbs are blue, red, blue, red, in order.
- Remove ribbon 3. After removal, the colors of the bulbs are red, red, blue, green, in order.
- Remove ribbon 2. After removal, the colors of the bulbs are green, red, red, green, in order.
The (e_1, e_2, e_3) satisfying the condition are (1, 3, 2) and (2, 3, 1), and either one will be accepted as correct.
For the second test case, it is impossible to remove all ribbons no matter how you perform the operations.