#P15947. [JOI Final 2026] 集邮 5 / Collecting Stamps 5

[JOI Final 2026] 集邮 5 / Collecting Stamps 5

Problem Description

There are NN towns in the country of IOI where JOI-kun lives, numbered from 11 to NN. Also, there are N1N-1 roads in the country of IOI, numbered from 11 to N1N-1. Road jj (1jN11 \leq j \leq N-1) connects town UjU_j and town VjV_j in both directions. It is possible to travel from any town to any other town by using some number of roads.

A stamp rally will now be held in the country of IOI. One stamp station will be installed in each town. The stamp station in town ii (1iN1 \leq i \leq N) will be installed at time TiT_i.

JOI-kun decides to participate in the stamp rally. At time 00, JOI-kun starts from one of the towns. Also, JOI-kun has stamina DD at time 00.

When JOI-kun is in town ii at time tt, he takes the following actions.

  1. First, if a stamp station has already been installed in the current town, he presses the stamp. That is, he presses the stamp if TitT_i \leq t.

  2. Next, he chooses either to finish the stamp rally or to move to another town. However, he can choose to move to another town only if there exists an adjacent town connected to town ii by a road that he has not visited yet, and his current stamina is at least 11.

  3. If JOI-kun chooses to move to another town, he chooses an unvisited town jj among the towns connected to town ii by a road, and moves there. His stamina decreases by 11, and he arrives at town jj at time t+1t + 1.

  4. If JOI-kun chooses to finish the stamp rally, the rally is successful if he has pressed a stamp at least once up to that point, and he can receive a present there. Otherwise, the rally is unsuccessful.

Assume that all times other than travel time between towns are negligible. Note that JOI-kun cannot stay in the same town.

You are an event organizer. If JOI-kun succeeds in the stamp rally, you need to prepare presents in the corresponding towns. However, the number of presents is limited, so you want to prepare presents in as few towns as possible. Unfortunately, you do not know from which town JOI-kun will start. Therefore, for each ss (1sN1 \leq s \leq N), you want to find the number of towns in which presents must be prepared when JOI-kun starts from town ss. In other words, you need to count the number of gg (1gN1 \leq g \leq N) such that it is possible for the stamp rally to be successful when JOI-kun finishes at town gg.

Given the information about the towns and roads in the country of IOI, JOI-kun’s stamina, and the installation times of the stamp stations, write a program that computes, for each town, the number of towns in which presents must be prepared when JOI-kun starts from that town.

Input Format

Read the following data from the standard input.

NN DD
T1T_1 T2T_2 \cdots TNT_N
U1U_1 V1V_1
U2U_2 V2V_2
\vdots
UN1U_{N-1} VN1V_{N-1}

Output Format

Write NN lines to the standard output. The ss-th line (1sN1 \leq s \leq N) should contain the number of towns in which presents must be prepared when JOI-kun starts from town ss.

5 2
2 2 0 1 3
1 2
2 3
2 4
4 5
2
3
4
2
2
5 1
0 1 2 1 2
1 2
2 3
3 4
4 5
2
1
2
0
1
7 6
2 3 0 4 1 3 4
1 2
2 3
2 4
1 5
1 6
6 7
2
2
7
5
1
2
5

Hint

Sample 1

We show one example of JOI-kun’s actions when s = 11.

  • At time 00, JOI-kun is in town 11 and takes the following actions.
    • The stamp station in town 11 has not been installed yet, so JOI-kun does not press a stamp.
    • JOI-kun’s current stamina is 22. He moves to town 22, which is one of the unvisited towns connected to town 11 by a road.
    • JOI-kun’s stamina decreases by 11, and he arrives at town 22 at time 11.
  • At time 11, JOI-kun is in town 22 and takes the following actions.
    • The stamp station in town 22 has not been installed yet, so JOI-kun does not press a stamp.
    • JOI-kun’s current stamina is 11. He moves to town 33, which is one of the unvisited towns connected to town 22 by a road.
    • JOI-kun’s stamina decreases by 11, and he arrives at town 33 at time 22.
  • At time 22, JOI-kun is in town 33 and takes the following actions.
    • The stamp station in town 33 has already been installed, so JOI-kun presses a stamp.
    • JOI-kun chooses to finish the stamp rally here. Since he has pressed a stamp at least once, the stamp rally is successful. He receives a present.

Therefore, when JOI-kun starts from town 11 and finishes the stamp rally at town 33, the stamp rally can be successful, so it is necessary to prepare a present in town 33. When JOI-kun starts from town 11, presents must be prepared only in towns 33 and 44, so the first line of the output should be 22.

Also, when JOI-kun starts from town 22, presents must be prepared only in towns 3,43,4, and 55, so the second line of the output should be 33.

This input example satisfies the constraints of subtasks 3,63,6.

Sample 2

This input example satisfies the constraints of subtasks 1,2,3,4,61,2,3,4,6.

Sample 3

This input example satisfies the constraints of subtasks 3,5,63,5,6.

Constraints

  • 2N4000002 \leq N \leq 400\,000.
  • 0DN10 \leq D \leq N-1.
  • 0TiN0 \leq T_i \leq N (1iN1 \leq i \leq N).
  • 1Uj<VjN1 \leq U_j < V_j \leq N (1jN11 \leq j \leq N-1).
  • It is possible to travel from any town to any other town by using some number of roads.
  • Given values are all integers.

Subtasks

  1. (3 points) D1D \leq 1.
  2. (7 points) N3000,(Uj,Vj)=(j,j+1)N \leq 3\,000,(U_j, V_j) = (j, j+1) (1jN11 \leq j \leq N-1).
  3. (10 points) N3000N \leq 3\,000.
  4. (11 points) (Uj,Vj)=(j,j+1)(U_j, V_j) = (j, j+1) (1jN11 \leq j \leq N-1).
  5. (41 points) D=N1,N150000D = N-1,N \leq 150\,000.
  6. (28 points) No additional constraints.