Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
N 人のグループが旅行に行きました。メンバーには 1 から N までの番号が付けられています。旅行中に発生した費用は、そのつどメンバーの誰か一人が立て替えて支払いました。旅行が終わった後、全員で費用を公平に精算することにしました。
旅行中には合計 M 回の支払いが発生しました。i 回目 (1 \leq i \leq M) の支払いでは、人 P_i が C_i 円を立て替えて支払いました。この支払いは K_i 人のメンバーが利用したもので、利用者は B_{i,1}, B_{i,2}, \ldots, B_{i,K_i} です。立て替えた人 P_i 自身が利用者に含まれることも、含まれないこともあります。各支払いについて、利用した K_i 人で均等に費用を負担するものとします。すなわち、利用者一人あたりの負担額は \frac{C_i}{K_i} 円です。
すべての支払いを考慮したうえで、人 j (1 \leq j \leq N) について、精算額 D_j を次のように定義します。
D_j = \sum_{\substack{1 \leq i \leq M \\ P_i = j}} C_i \;-\; \sum_{\substack{1 \leq i \leq M \\ j \in \{B_{i,1}, \ldots, B_{i,K_i}\}}} \frac{C_i}{K_i}
すなわち、D_j は「人 j が実際に立て替えた合計金額」から「人 j が本来負担すべき合計金額」を引いた値です。人 j がどの支払いでも立て替えを行っていない場合、立て替えた合計金額は 0 円とします。同様に、人 j がどの支払いの利用者にも含まれていない場合、負担すべき合計金額は 0 円とします。
D_j が正なら人 j は D_j 円を受け取るべきであり、D_j が負なら人 j は |D_j| 円を支払うべきであり、D_j = 0 なら精算の必要はありません。
N 人全員について D_j の値をそれぞれ求めてください。なお、すべての \frac{C_i}{K_i} は整数になることが保証されるため、D_j も必ず整数になります。
制約
- 2 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq P_i \leq N
- 1 \leq C_i \leq 10^9
- 1 \leq K_i \leq N
- 1 \leq B_{i,1} < B_{i,2} < \ldots < B_{i,K_i} \leq N
- P_i は B_{i,1}, B_{i,2}, \ldots, B_{i,K_i} に含まれるとは限らない
- \frac{C_i}{K_i} は整数である
- \sum_{i=1}^{M} K_i \leq 10^5
- 入力はすべて整数である
入力
N M
P_1 C_1 K_1 B_{1,1} B_{1,2} \ldots B_{1,K_1}
P_2 C_2 K_2 B_{2,1} B_{2,2} \ldots B_{2,K_2}
\vdots
P_M C_M K_M B_{M,1} B_{M,2} \ldots B_{M,K_M}
- 1 行目には、グループの人数 N と支払い回数 M が、スペース区切りで与えられる。
- 続く M 行にわたって、各支払いの情報が与えられる。
- そのうち i 番目の行 (1 \leq i \leq M) では、i 回目の支払いについて、立て替えた人の番号 P_i、金額 C_i、利用者数 K_i、利用者の番号 B_{i,1}, B_{i,2}, \ldots, B_{i,K_i} がスペース区切りで与えられる。
出力
N 行出力せよ。j 行目 (1 \leq j \leq N) には、人 j の精算額 D_j の値を整数として出力せよ。D_j が負の場合は負の符号を付けて出力し、D_j = 0 の場合は 0 を出力せよ。
入力例 1
3 2 1 6000 3 1 2 3 2 4000 2 2 3
出力例 1
4000 0 -4000
入力例 2
5 4 1 10000 5 1 2 3 4 5 3 6000 3 1 2 3 2 8000 2 2 4 5 3000 3 3 4 5
出力例 2
6000 0 1000 -7000 0
入力例 3
6 5 1 12000 6 1 2 3 4 5 6 2 9000 3 1 2 3 4 8000 4 1 4 5 6 6 5000 2 2 6 3 7000 2 3 5
出力例 3
5000 1500 -1500 4000 -7500 -1500
Score : 266 pts
Problem Statement
A group of N people went on a trip. The members are numbered from 1 to N. Expenses incurred during the trip were each paid upfront by one of the members on behalf of the others. After the trip, they decided to settle all expenses fairly among everyone.
A total of M payments were made during the trip. For the i-th payment (1 \leq i \leq M), person P_i paid C_i yen upfront. This payment was for K_i members who used it, and the users are B_{i,1}, B_{i,2}, \ldots, B_{i,K_i}. The person P_i who paid upfront may or may not be included among the users. For each payment, the cost is to be shared equally among the K_i users. That is, the amount each user should bear is \frac{C_i}{K_i} yen.
Considering all payments, define the settlement amount D_j for person j (1 \leq j \leq N) as follows:
D_j = \sum_{\substack{1 \leq i \leq M \\ P_i = j}} C_i \;-\; \sum_{\substack{1 \leq i \leq M \\ j \in \{B_{i,1}, \ldots, B_{i,K_i}\}}} \frac{C_i}{K_i}
In other words, D_j is the value obtained by subtracting "the total amount person j should have borne" from "the total amount person j actually paid upfront." If person j did not pay upfront for any payment, the total amount paid upfront is 0 yen. Similarly, if person j is not included as a user in any payment, the total amount they should bear is 0 yen.
If D_j is positive, person j should receive D_j yen. If D_j is negative, person j should pay |D_j| yen. If D_j = 0, no settlement is needed.
Determine the value of D_j for all N people. Note that it is guaranteed that every \frac{C_i}{K_i} is an integer, so D_j is always an integer as well.
Constraints
- 2 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq P_i \leq N
- 1 \leq C_i \leq 10^9
- 1 \leq K_i \leq N
- 1 \leq B_{i,1} < B_{i,2} < \ldots < B_{i,K_i} \leq N
- P_i is not necessarily included among B_{i,1}, B_{i,2}, \ldots, B_{i,K_i}
- \frac{C_i}{K_i} is an integer
- \sum_{i=1}^{M} K_i \leq 10^5
- All input values are integers
Input
N M
P_1 C_1 K_1 B_{1,1} B_{1,2} \ldots B_{1,K_1}
P_2 C_2 K_2 B_{2,1} B_{2,2} \ldots B_{2,K_2}
\vdots
P_M C_M K_M B_{M,1} B_{M,2} \ldots B_{M,K_M}
- The first line contains the number of people in the group N and the number of payments M, separated by a space.
- The following M lines provide the information for each payment.
- The i-th of these lines (1 \leq i \leq M) contains, for the i-th payment, the number of the person who paid upfront P_i, the amount C_i, the number of users K_i, and the user numbers B_{i,1}, B_{i,2}, \ldots, B_{i,K_i}, separated by spaces.
Output
Output N lines. On the j-th line (1 \leq j \leq N), output the settlement amount D_j for person j as an integer. If D_j is negative, output it with a negative sign. If D_j = 0, output 0.
Sample Input 1
3 2 1 6000 3 1 2 3 2 4000 2 2 3
Sample Output 1
4000 0 -4000
Sample Input 2
5 4 1 10000 5 1 2 3 4 5 3 6000 3 1 2 3 2 8000 2 2 4 5 3000 3 3 4 5
Sample Output 2
6000 0 1000 -7000 0
Sample Input 3
6 5 1 12000 6 1 2 3 4 5 6 2 9000 3 1 2 3 4 8000 4 1 4 5 6 6 5000 2 2 6 3 7000 2 3 5
Sample Output 3
5000 1500 -1500 4000 -7500 -1500
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君は文化祭で風船割りゲームの出店を手伝っています。会場には N 個の風船が一列に並んでおり、i 番目の風船の耐久値は H_i です。
高橋君のチームには M 本のダーツがあり、j 番目のダーツの攻撃力は P_j です。
高橋君は合計で K 回以下、ダーツを投げることができます。1 回の投擲(とうてき)では、M 本のダーツの中から 1 本を選び、N 個の風船の中から 1 個を選んで、その風船にダーツを投げます。これにより、選んだ風船の耐久値が選んだダーツの攻撃力の分だけ減少します。
各ダーツは何度でも繰り返し使うことができます。また、同じ風船を何度狙ってもよく、1 つの風船に対して異なるダーツを投げることも自由にできます。
風船の耐久値が 0 以下になると、その風船は割れます。すでに割れた風船にダーツを投げることもできますが、その投擲は 1 回分として数えられ、割れた風船の数が増えることはありません。
高橋君が最適にダーツを投げたとき、割れる風船の数の最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq K \leq 10^{18}
- 1 \leq H_i \leq 10^{18} (1 \leq i \leq N)
- 1 \leq P_j \leq 10^{18} (1 \leq j \leq M)
- 入力はすべて整数である。
入力
N M K H_1 H_2 \ldots H_N P_1 P_2 \ldots P_M
- 1 行目には、風船の数 N、ダーツの本数 M、投擲回数の上限 K が、スペース区切りで与えられる。
- 2 行目には、各風船の耐久値 H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。
- 3 行目には、各ダーツの攻撃力 P_1, P_2, \ldots, P_M が、スペース区切りで与えられる。
出力
最適にダーツを投げたときに割れる風船の最大数を 1 行で出力せよ。
入力例 1
5 2 5 3 1 4 1 5 2 3
出力例 1
4
入力例 2
3 1 2 10 20 30 5
出力例 2
1
入力例 3
7 3 15 10 5 8 12 3 7 20 4 6 2
出力例 3
7
入力例 4
10 5 50 100 200 300 400 500 600 700 800 900 1000 3 7 10 5 2
出力例 4
2
入力例 5
1 1 1 1 1
出力例 5
1
Score : 333 pts
Problem Statement
Takahashi is helping run a balloon popping game booth at a school festival. There are N balloons lined up in a row at the venue, and the i-th balloon has a durability of H_i.
Takahashi's team has M darts, and the j-th dart has an attack power of P_j.
Takahashi can throw darts at most K times in total. In each throw, he chooses 1 dart from the M darts and 1 balloon from the N balloons, and throws that dart at that balloon. This reduces the chosen balloon's durability by the chosen dart's attack power.
Each dart can be used any number of times. He may also target the same balloon multiple times, and is free to throw different darts at the same balloon.
When a balloon's durability becomes 0 or less, it pops. He may also throw darts at already popped balloons, but such a throw still counts as one of his throws, and it does not increase the number of popped balloons.
Determine the maximum number of balloons that can be popped when Takahashi throws darts optimally.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq K \leq 10^{18}
- 1 \leq H_i \leq 10^{18} (1 \leq i \leq N)
- 1 \leq P_j \leq 10^{18} (1 \leq j \leq M)
- All input values are integers.
Input
N M K H_1 H_2 \ldots H_N P_1 P_2 \ldots P_M
- The first line contains the number of balloons N, the number of darts M, and the maximum number of throws K, separated by spaces.
- The second line contains the durability of each balloon H_1, H_2, \ldots, H_N, separated by spaces.
- The third line contains the attack power of each dart P_1, P_2, \ldots, P_M, separated by spaces.
Output
Print in one line the maximum number of balloons that can be popped when darts are thrown optimally.
Sample Input 1
5 2 5 3 1 4 1 5 2 3
Sample Output 1
4
Sample Input 2
3 1 2 10 20 30 5
Sample Output 2
1
Sample Input 3
7 3 15 10 5 8 12 3 7 20 4 6 2
Sample Output 3
7
Sample Input 4
10 5 50 100 200 300 400 500 600 700 800 900 1000 3 7 10 5 2
Sample Output 4
2
Sample Input 5
1 1 1 1 1
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は N 段の階段の前に立っています。高橋君は現在 0 段目(地面)にいて、N 段目(頂上)まで上ることを目指します。
高橋君は1回の移動で、現在いる段から 1 段上、2 段上、または 3 段上の段へ移動することができます。すなわち、現在 i 段目にいるとき、i+1 段目、i+2 段目、i+3 段目のいずれかへ直接移動できます。ただし、移動先の段にのみ着地し、途中の段には着地しません。
階段のいくつかの段は老朽化しており壊れています。壊れている段は M 個あり、それぞれ B_1, B_2, \ldots, B_M 段目です(B_i はすべて異なります)。壊れている段に着地することはできません。ただし、壊れている段を飛び越えること(着地せずに通過すること)は可能です。例えば、i+1 段目が壊れていても、i 段目から i+2 段目や i+3 段目へ移動することができます。なお、0 段目(地面)と N 段目(頂上)は壊れていないことが保証されています。
高橋君が 0 段目から N 段目まで上る方法の数を求めてください。ただし、上り方が1通りも存在しない場合は 0 とします。答えは非常に大きくなる可能性があるため、10^9 + 7 で割った余りを出力してください。
制約
- 1 \leq N \leq 10^5
- 0 \leq M \leq N - 1
- 1 \leq B_i \leq N - 1 (1 \leq i \leq M)
- B_i はすべて異なる
- B_1, B_2, \ldots, B_M は昇順とは限らない
- 入力はすべて整数
入力
N M B_1 B_2 \ldots B_M
- 1 行目には、階段の段数を表す整数 N と、壊れている段の数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、壊れている段の番号を表す M 個の整数 B_1, B_2, \ldots, B_M が、スペース区切りで与えられる。順序は不定である。M = 0 の場合、2 行目は与えられない。
出力
高橋君が 0 段目から N 段目まで上る方法の数を 10^9 + 7 で割った余りを 1 行で出力せよ。
入力例 1
5 1 3
出力例 1
5
入力例 2
4 3 1 2 3
出力例 2
0
入力例 3
20 3 5 10 15
出力例 3
6728
入力例 4
1000 5 100 200 300 400 500
出力例 4
935523008
入力例 5
1 0
出力例 5
1
Score : 366 pts
Problem Statement
Takahashi is standing in front of a staircase with N steps. Takahashi is currently on step 0 (the ground) and aims to reach step N (the top).
In one move, Takahashi can move from his current step to the step that is 1 step above, 2 steps above, or 3 steps above. That is, when he is currently on step i, he can directly move to step i+1, step i+2, or step i+3. However, he only lands on the destination step and does not land on any intermediate steps.
Some steps of the staircase are deteriorated and broken. There are M broken steps, which are steps B_1, B_2, \ldots, B_M (all B_i are distinct). It is not possible to land on a broken step. However, it is possible to jump over a broken step (passing it without landing). For example, even if step i+1 is broken, Takahashi can move from step i to step i+2 or step i+3. It is guaranteed that step 0 (the ground) and step N (the top) are not broken.
Find the number of ways Takahashi can climb from step 0 to step N. If there is no valid way to climb, output 0. Since the answer can be very large, output the remainder when divided by 10^9 + 7.
Constraints
- 1 \leq N \leq 10^5
- 0 \leq M \leq N - 1
- 1 \leq B_i \leq N - 1 (1 \leq i \leq M)
- All B_i are distinct
- B_1, B_2, \ldots, B_M are not necessarily in ascending order
- All inputs are integers
Input
N M B_1 B_2 \ldots B_M
- The first line contains an integer N representing the number of steps and an integer M representing the number of broken steps, separated by a space.
- The second line contains M integers B_1, B_2, \ldots, B_M representing the numbers of the broken steps, separated by spaces. The order is arbitrary. If M = 0, the second line is not given.
Output
Output in one line the number of ways Takahashi can climb from step 0 to step N, modulo 10^9 + 7.
Sample Input 1
5 1 3
Sample Output 1
5
Sample Input 2
4 3 1 2 3
Sample Output 2
0
Sample Input 3
20 3 5 10 15
Sample Output 3
6728
Sample Input 4
1000 5 100 200 300 400 500
Sample Output 4
935523008
Sample Input 5
1 0
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君はイベント企画会社を経営しており、N 件のイベント開催依頼を受けている。依頼には 1 から N までの番号が付けられており、依頼 i(1 \leq i \leq N)には、会場の使用開始時刻 L_i、使用終了時刻 R_i、および報酬 V_i が設定されている。ここで時刻は整数で表される。
高橋君が利用できる会場は 1 つだけであり、同じ時間帯に複数のイベントを開催することはできない。依頼 i を実行すると、半開区間 [L_i, R_i) で表される時間帯(時刻 L_i 以上 R_i 未満)の間、会場を占有する。依頼 i と依頼 j(i \neq j)が両立するとは、占有する時間帯が重ならないこと、すなわち R_i \leq L_j または R_j \leq L_i が成り立つことをいう。特に、一方の終了時刻と他方の開始時刻が一致する場合(例えば R_i = L_j)は両立する。
一方、ライバル会社の青木君は、高橋君の利益を最小化するために妨害工作を行う。青木君は、N 件の依頼のうちちょうど K 件を選んでキャンセルさせる(キャンセルする K 件は互いに異なる依頼でなければならない)。青木君は、高橋君がキャンセル後に最適に行動することを見越した上で、高橋君が最終的に得られる報酬の合計が最小となるようにキャンセルする K 件を選ぶ。
キャンセルされずに残った N - K 件の依頼の中から、高橋君はどの 2 つも両立するように 0 件以上の依頼を選んで実行し、報酬の合計を最大化する。各依頼は最大 1 回しか実行できない。0 件を選んだ場合、報酬の合計は 0 である。
青木君が最適にキャンセルを行い、その後高橋君が残った依頼から最適に選んだとき、高橋君が得られる報酬の合計を求めよ。
制約
- 1 \leq N \leq 8
- 0 \leq K \leq N
- 0 \leq L_i < R_i \leq 100
- 1 \leq V_i \leq 1000
- 入力はすべて整数である
入力
N K L_1 R_1 V_1 L_2 R_2 V_2 \vdots L_N R_N V_N
- 1 行目には、依頼の件数 N と、青木君がキャンセルさせる件数 K が、スペース区切りで与えられる。
- 続く N 行のうち i 行目(1 \leq i \leq N)には、依頼 i の使用開始時刻 L_i、使用終了時刻 R_i、報酬 V_i がスペース区切りで与えられる。
出力
青木君が最適にキャンセルを行い、その後高橋君が最適に依頼を選んだときの、高橋君が得られる報酬の合計を 1 行で出力せよ。
入力例 1
3 1 0 2 5 2 4 3 0 3 10
出力例 1
8
入力例 2
4 2 0 5 100 5 10 100 0 10 50 3 7 50
出力例 2
50
入力例 3
6 2 0 3 10 3 6 20 6 9 10 1 5 25 4 8 15 0 9 30
出力例 3
30
入力例 4
8 3 0 10 50 10 20 60 20 30 70 0 15 100 15 30 90 5 25 80 0 30 200 12 18 40
出力例 4
110
入力例 5
1 1 0 100 1000
出力例 5
0
Score : 400 pts
Problem Statement
Takahashi runs an event planning company and has received N event requests. The requests are numbered from 1 to N, and request i (1 \leq i \leq N) specifies a venue start time L_i, end time R_i, and reward V_i. Times are represented as integers.
Takahashi has access to only one venue and cannot hold multiple events during the same time period. Executing request i occupies the venue during the time period represented by the half-open interval [L_i, R_i) (from time L_i inclusive to time R_i exclusive). Requests i and j (i \neq j) are said to be compatible if their occupied time periods do not overlap, that is, if R_i \leq L_j or R_j \leq L_i holds. In particular, if one's end time equals the other's start time (e.g., R_i = L_j), they are compatible.
Meanwhile, his rival Aoki will carry out sabotage to minimize Takahashi's profit. Aoki selects exactly K of the N requests to cancel (the K cancelled requests must all be distinct). Aoki chooses which K requests to cancel so as to minimize the total reward Takahashi can ultimately obtain, anticipating that Takahashi will act optimally after the cancellation.
From the remaining N - K requests that were not cancelled, Takahashi selects 0 or more requests to execute such that every pair of selected requests is compatible, maximizing the total reward. Each request can be executed at most once. If 0 requests are selected, the total reward is 0.
Determine the total reward Takahashi obtains when Aoki cancels optimally and then Takahashi selects optimally from the remaining requests.
Constraints
- 1 \leq N \leq 8
- 0 \leq K \leq N
- 0 \leq L_i < R_i \leq 100
- 1 \leq V_i \leq 1000
- All input values are integers
Input
N K L_1 R_1 V_1 L_2 R_2 V_2 \vdots L_N R_N V_N
- The first line contains the number of requests N and the number of requests K that Aoki will cancel, separated by a space.
- The following N lines each describe a request: the i-th line (1 \leq i \leq N) contains the start time L_i, end time R_i, and reward V_i of request i, separated by spaces.
Output
Output in a single line the total reward Takahashi obtains when Aoki cancels optimally and then Takahashi selects optimally from the remaining requests.
Sample Input 1
3 1 0 2 5 2 4 3 0 3 10
Sample Output 1
8
Sample Input 2
4 2 0 5 100 5 10 100 0 10 50 3 7 50
Sample Output 2
50
Sample Input 3
6 2 0 3 10 3 6 20 6 9 10 1 5 25 4 8 15 0 9 30
Sample Output 3
30
Sample Input 4
8 3 0 10 50 10 20 60 20 30 70 0 15 100 15 30 90 5 25 80 0 30 200 12 18 40
Sample Output 4
110
Sample Input 5
1 1 0 100 1000
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
ある町には N 個の交差点があり、N - 1 本の道路でつながっています。どの 2 つの交差点の間も、道路をたどってちょうど 1 通りの経路で行き来できます(すなわち、交差点と道路は木構造をなしています)。
交差点 a と交差点 b の 距離 を、a から b へ行くために通る道路の本数と定めます。特に、交差点 a から交差点 a 自身への距離は 0 です。
最初、どの交差点にもお店はありません。
Q 日間にわたって、i 日目 (1 \leq i \leq Q) には交差点 C_i に新たにお店がオープンします。同じ交差点に 2 回以上お店がオープンすることはありません。一度オープンしたお店は、その後もずっと営業し続けます。
青木君は町のどこかの交差点に立っているとき、距離 R 以下にあるお店がちょうど 1 軒だけならば、そのお店を独り占めできて嬉しいと感じます。
各日の終わりにおいて、青木君が嬉しいと感じる交差点の個数を求めてください。すなわち、交差点 x からの距離が R 以下であるお店の数がちょうど 1 であるような交差点 x の個数を求めてください。ただし、交差点 x 自身にお店がある場合(距離 0)もお店の数に含みます。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq N
- N + Q \leq 2 \times 10^5
- 1 \leq R \leq 20
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- 与えられるグラフは木である
- 1 \leq C_i \leq N
- C_i はすべて異なる
- 入力はすべて整数である
入力
N Q R
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
C_1
C_2
\vdots
C_Q
- 1 行目には、交差点の個数 N、日数 Q、距離の上限 R がスペース区切りで与えられます。
- 続く N - 1 行のうち i 行目には、交差点 u_i と交差点 v_i を結ぶ道路があることを表す u_i と v_i がスペース区切りで与えられます。
- その後の Q 行のうち i 行目には、i 日目に新たにお店がオープンする交差点の番号 C_i が与えられます。
出力
A_1 A_2 \vdots A_Q
i 行目には、i 日目の終わりにおいて、距離が R 以下であるお店がちょうど 1 軒である交差点の個数 A_i を出力してください。
入力例 1
5 3 1 1 2 2 3 3 4 4 5 3 1 5
出力例 1
3 3 3
入力例 2
6 4 2 1 2 1 3 1 4 1 5 1 6 2 3 4 5
出力例 2
6 0 0 0
入力例 3
15 8 2 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 6 11 6 12 7 13 13 14 13 15 8 12 5 14 1 10 7 3
出力例 3
4 8 9 13 8 7 4 2
入力例 4
40 20 4 1 2 1 3 1 4 2 5 2 6 3 7 3 8 4 9 4 10 5 11 5 12 6 13 7 14 7 15 8 16 9 17 10 18 10 19 11 20 12 21 13 22 14 23 15 24 16 25 17 26 18 27 19 28 20 29 21 30 22 31 23 32 24 33 25 34 26 35 27 36 28 37 29 38 30 39 31 40 38 34 22 5 27 15 40 1 19 30 8 36 24 12 3 33 10 29 6 17
出力例 4
5 10 16 13 15 19 17 8 7 5 4 4 3 3 2 2 0 0 0 1
入力例 5
1 1 20 1
出力例 5
1
Score : 433 pts
Problem Statement
A town has N intersections connected by N - 1 roads. Any two intersections can be reached from each other by following roads along exactly one unique path (that is, the intersections and roads form a tree structure).
The distance between intersection a and intersection b is defined as the number of roads traversed to travel from a to b. In particular, the distance from intersection a to itself is 0.
Initially, there are no shops at any intersection. Over a period of Q days, on day i (1 \leq i \leq Q), a new shop opens at intersection C_i. No intersection will have a shop open more than once. Once a shop opens, it continues to operate indefinitely.
When Aoki is standing at some intersection in the town, he feels happy if there is exactly 1 shop within distance R or less, because he can have that shop all to himself.
For each day, determine the number of intersections where Aoki would feel happy at the end of that day. That is, find the number of intersections x such that the number of shops within distance R from intersection x is exactly 1. Note that if intersection x itself has a shop (distance 0), it is also counted among the shops.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq N
- N + Q \leq 2 \times 10^5
- 1 \leq R \leq 20
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- The given graph is a tree
- 1 \leq C_i \leq N
- All C_i are distinct
- All input values are integers
Input
N Q R
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
C_1
C_2
\vdots
C_Q
- The first line contains the number of intersections N, the number of days Q, and the distance limit R, separated by spaces.
- The following N - 1 lines each contain u_i and v_i separated by a space, indicating that there is a road connecting intersection u_i and intersection v_i.
- The subsequent Q lines each contain C_i, the number of the intersection where a new shop opens on day i.
Output
A_1 A_2 \vdots A_Q
On the i-th line, output A_i, the number of intersections where the number of shops within distance R is exactly 1, at the end of day i.
Sample Input 1
5 3 1 1 2 2 3 3 4 4 5 3 1 5
Sample Output 1
3 3 3
Sample Input 2
6 4 2 1 2 1 3 1 4 1 5 1 6 2 3 4 5
Sample Output 2
6 0 0 0
Sample Input 3
15 8 2 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 6 11 6 12 7 13 13 14 13 15 8 12 5 14 1 10 7 3
Sample Output 3
4 8 9 13 8 7 4 2
Sample Input 4
40 20 4 1 2 1 3 1 4 2 5 2 6 3 7 3 8 4 9 4 10 5 11 5 12 6 13 7 14 7 15 8 16 9 17 10 18 10 19 11 20 12 21 13 22 14 23 15 24 16 25 17 26 18 27 19 28 20 29 21 30 22 31 23 32 24 33 25 34 26 35 27 36 28 37 29 38 30 39 31 40 38 34 22 5 27 15 40 1 19 30 8 36 24 12 3 33 10 29 6 17
Sample Output 4
5 10 16 13 15 19 17 8 7 5 4 4 3 3 2 2 0 0 0 1
Sample Input 5
1 1 20 1
Sample Output 5
1