Engineers

Time limit: 2.25s Memory limit: 512MB Input: Output:

It is time for the annual server maintenance at Vianu. Some of the maintenance can be done by humans, but the cables connecting the network are routed through a series of underground pipes that are too small to access! Luckily, a brilliant engineer came up with the idea of letting trained rats check the computers instead.

Each rat can enter the network at one computer, travel along the cables through the underground pipes, and exit at another computer, checking all computers along the way. Because the rats are quite lazy, each rat will only follow a single simple path between its entry and exit point before getting tired.

The maintenance crew wants to have rats check some computers so that among the remaining unchecked ones, the configuration of security codes is "balanced enough". Your task is to determine the minimum number of rats needed.

Task

There are NN computers, numbered from 00 to N1N - 1, linked through N1N - 1 cables. The network is a tree (connected and acyclic).

A single rat can check every computer on the unique simple path between the computer where it enters the network and the computer where it exits. Formally, if a rat enters at computer SS and exits at computer TT (S,T{0,,N1}S, T \in \{0, \dots, N - 1\}), then the rat will check all computers v1,v2,,vkv_1, v_2, \dots, v_k such that:

  • v1=Sv_1 = S and vk=Tv_k = T,
  • for all 1i<k1 \leq i < k, computers viv_i and vi+1v_{i+1} are directly connected by a cable,
  • kk is minimal.

Rats may reuse the same cables and computers, and a computer can be checked by multiple rats.

Each computer i{0,,N1}i \in \{0, \dots, N - 1\} has an associated positive integer code Ci1C_i \ge 1. The maintenance crew fix a maximum acceptable difference DD. After all rats have done their work, let the set of remaining unchecked computers be R{0,,N1}R \subseteq \{0, \dots, N - 1\}. The crew want to ensure that CiCjD|C_i - C_j| \leq D for any pair of i,jRi, j \in R.

In other words, the difference between the maximum and minimum code among remaining computers must be at most DD.

Each rat can only check one path before getting tired. You want to minimize the number of rats used.

Implementation

You should implement the function

int solve(int N, int D, std::vector<int> C, std::vector<int> P, std::vector<int> Q)

This function receives as parameters: NN, the number of computers, DD, the maximum acceptable difference, CC, the codes of the computers, and two vectors of length N1N - 1, PP and QQ, meaning there is a cable between computers PiP_i and QiQ_i, for all 0iN10 \leq i \leq N - 1.

The function should return the minimum number of rats needed so that after all rats have checked their paths, the remaining unchecked computers satisfy maxiRCiminiRCiD.max_{i \in R} C_i - min_{i \in R} C_i \leq D.

Constraints

  • 1N200 0001 \leq N \leq 200 \ 000.
  • 1Ci1 000 000 0001 \leq C_i \leq 1 \ 000 \ 000 \ 000 for all 0iN10 \leq i \leq N - 1.
  • 1D1 000 000 0001 \leq D \leq 1 \ 000 \ 000 \ 000.
  • 0pi,qi<N0 \leq p_i, q_i < N, piqip_i \neq q_i, and all pairs (pi,qi)(p_i, q_i) are distinct
# Points Constraints
1 7 N20N \leq 20 and 1Ci501 \le C_i \leq 50 for all 0iN10 \leq i \leq N - 1.
2 6 N1000N \leq 1000 and 1Ci1 0001 \le C_i \leq 1 \ 000 for all 0iN10 \leq i \leq N - 1.
3 11 N1 000N \leq 1 \ 000.
4 16 pi=0,qi=i+1p_i = 0, q_i = i + 1 for all 0i<N10 \leq i < N - 1.
5 26 N50 000N \leq 50 \ 000.
6 34 No additional constraints.

Example 1

input

5 3
1 10 3 8 6
0 1
1 2
2 3
2 4

output

1

Explanation

In the first example, there are N=5N = 5 computers and D=3D = 3. The network can be visualized as:

One possible strategy is to send a single rat on a path that starts at computer 00 and exits at computer 33, passing along the path 01230 - 1 - 2 - 3. After such a path is checked, the network is safe.

Example 2

input

20 30
13 36 11 35 4 9 42 9 1 4 11 3 15 31 46 41 31 17 11 12
19 5
19 0
19 13
19 9
19 4
19 10
5 1
19 18
0 7
5 8
19 12
5 17
13 16
5 14
13 3
19 6
5 15
5 2
4 11

output

3

Log in or sign up to be able to send submissions!