Under construction

Still being built. Things may change

Keeping the Tree Healthy

Interview

Time limit1sMemory limit1024 MB

Summary
Given a tree whose nodes are marked 1 (unhealthy) or 0, cut the fewest edges so every connected component has at most K unhealthy nodes, and print that count.
Level

Medium6 of 10

Topics
Tree, Dynamic programming, Greedy, Sorting
Solved
No attempts yet

Problem

Yehwan likes playing with trees, so they built one out of tree branches. After some time, Yehwan noticed that some of the joints between branches are rotting. Yehwan wants to take a rose pruner and cut the fewest possible branches so that every connected component becomes healthy.

Call the points where branches meet nodes, and consider the graph GG formed by connecting nodes with edges (branches). Graph GG is a tree. A rotting node is called an unhealthy node. Yehwan considers a connected component healthy if it contains at most KK unhealthy nodes.

The figure above shows one way to split the tree into healthy parts when K=2K=2. (Red nodes are unhealthy nodes.)

Given the total number of nodes NN and the maximum number of unhealthy nodes KK in a connected component, print the minimum number of edges to remove so that every remaining connected component is healthy.

Input

The first line contains two integers NN and KK, separated by a space.

The second line contains NN integers H1,H2,⋯ ,HNH_1, H_2, \cdots, H_N, separated by spaces. For each 1≤i≤N1\leq i\leq N, Hi=1H_i=1 if node ii is an unhealthy node, and Hi=0H_i=0 otherwise.

The next N−1N-1 lines each contain two integers UU and VV, separated by a space. This means an edge exists between node UU and node VV.

Output

Print the minimum number of edges to remove so that every remaining connected component is healthy.

Constraints

  • 1≤N≤1051\leq N\leq 10^5, 1≤K≤N1\leq K\leq N
  • For each 1≤i≤N1\leq i\leq N, Hi∈{0,1}H_i\in\{0, 1\}
  • 1≤U≤N1\leq U\leq N, 1≤V≤N1\leq V\leq N, U≠VU\neq V
  • All given numbers are integers.
  • The given graph is a tree.

Examples1

  1. Example 1

    Input
    12 2
    1 0 0 1 0 1 0 0 1 0 0 1
    1 2
    1 3
    2 4
    2 5
    3 6
    4 7
    5 8
    6 9
    6 10
    7 11
    7 12
    
    Expected output
    2