#P6118. [JOI 2019 Final] 独特的城市 / Unique Cities

[JOI 2019 Final] 独特的城市 / Unique Cities

Description

JOI Country has NN cities, numbered from 11 to NN. These cities are connected by N1N - 1 bidirectional roads. The ii-th road connects cities AiA_i and BiB_i. From any city, you can reach all cities.

JOI Country has some local products. Each product type is numbered between 11 and MM (including 11 and MM), but some integers from 11 to MM may not correspond to any product in JOI Country. Each city in JOI Country produces exactly one product. The product produced by city jj is CjC_j. Multiple cities may produce the same product.

We define the distance between two cities as the minimum number of roads that must be passed through to go from one city to the other. For a city xx, we define a city yy (yxy \neq x) to be a unique city if and only if, for any city zz (zx,zyz \neq x, z \neq y), the distance between xx and yy is not equal to the distance between xx and zz.

Mr. K, the Minister of Transportation of JOI Country, wants to know how many different kinds of products can be produced in total by the unique cities of city jj.

Given the road information of JOI Country and the product produced by each city, write a program to compute, for each city, how many different kinds of products can be produced in total by its unique cities.

Input Format

The first line contains two integers N,MN, M, as described in the statement.

The next N1N - 1 lines each contain two integers Ai,BiA_i, B_i, as described in the statement.

The last line contains NN positive integers. The ii-th integer is CiC_i, as described in the statement.

Output Format

Output NN lines. The ii-th line should contain the number of different kinds of products that can be produced in total by the unique cities of city ii.

5 4
1 2
2 3
3 4
3 5
1 2 1 2 4
2
0
1
1
1
7 1
1 2
2 3
3 4
4 5
5 6
6 7
1 1 1 1 1 1 1
1
1
1
0
1
1
1
10 10
2 6
5 8
10 8
1 4
10 6
4 5
10 7
6 9
3 7
1 2 3 4 5 6 7 8 9 10
4
3
4
2
0
2
2
0
3
2
22 12
9 6
12 13
4 20
21 22
3 19
2 9
6 18
18 11
18 3
16 2
6 4
3 17
16 10
8 16
22 1
16 14
15 8
9 21
2 12
21 5
12 7
1 1 4 8 4 11 7 6 7 11 6 11 10 4 7 5 3 12 9 6 12 2
2
0
1
1
1
1
1
0
0
1
2
0
1
1
2
0
2
1
2
3
0
0

Hint

Sample Explanation 1:

For city 11, its unique cities are cities 22 and 33. City 22 produces product 22, and city 33 produces product 33, so there are 22 kinds of products in total. Therefore the answer is 22.

For city 22, there are no unique cities, so output 00.

For city 33, its unique city is city 11. City 11 produces product 11, so the answer is 11.

For city 44, its unique cities are cities 11 and 33. Both cities 11 and 33 produce product 11, so the answer is 11.

For city 55, its unique cities are cities 11 and 33. Both cities 11 and 33 produce product 11, so the answer is 11.

Note: No city produces product 33.

For 4%4\% of the testdata, N2000N \le 2000.

For another 32%32\% of the testdata, M=1M = 1.

For another 32%32\% of the testdata, M=N,Cj=j(1jN)M = N, C_j = j (1 \le j \le N).

For 100%100\% of the testdata, $1 \le N \le 2 \times 10^5, 1 \le M, A_i, B_i \le N, A_i \neq B_i, 1 \le C_j \le M$.

Translated by ChatGPT 5