Cow Telephones

Time limit1sMemory limit128 MB

Summary
Given a tree with cows at its leaves and vertex capacity K plus unit edge capacity, find the maximum number of disjoint leaf-to-leaf conversation paths.
Level

Hard8 of 10

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

Problem

The cows have built a telephone network. For this problem it can be viewed as an undirected tree with NN vertices (1≤N≤100,0001 \le N \le 100{,}000), numbered 11 through NN. Each vertex is a telephone switchboard, and each edge is a telephone wire joining two switchboards. Edge ii is given by two integers AiA_i and BiB_i, the two vertices it connects (1≤Ai≤N1 \le A_i \le N, 1≤Bi≤N1 \le B_i \le N, Ai≠BiA_i \ne B_i).

Some switchboards have exactly one wire connecting them to another switchboard; these are the leaves of the tree, and each leaf is a telephone booth in a cow field.

For two cows to talk, their conversation travels along the unique shortest path between the two vertices where the cows stand. A single switchboard can handle at most KK simultaneous conversations (1≤K≤101 \le K \le 10), and at most one conversation may pass through any given wire at any one time.

Given that there is one cow at every leaf of the tree, what is the maximum number of pairs of cows that can talk at the same time? Each cow may take part in at most one conversation.

Consider this six-vertex telephone network with K=1K = 1:

       1   5          C1   C5
       |   |          ||   ||
       2---4   -->    |2---4|
       |   |          ||   ||
       3   6          C3   C6

There are cows at vertices 1,3,5,1, 3, 5, and 66. If cow 11 talks to cow 33 and cow 55 talks to cow 66, no switchboard exceeds its limit, so the answer for this example is 22 (two pairs of cows talking simultaneously).

Input

  • Line 1: Two space-separated integers NN and KK.
  • Lines 2 to NN: Line i+1i+1 contains two space-separated integers AiA_i and BiB_i for edge ii.

Output

  • Line 1: The maximum number of pairs of cows that can hold conversations simultaneously.

Examples5

  1. Example 1

    Input
    6 1
    1 2
    2 3
    2 4
    4 5
    4 6
    
    Expected output
    2
    
  2. Example 2

    Input
    2 1
    1 2
    
    Expected output
    1
    
  3. Example 3

    Input
    5 2
    1 2
    1 3
    1 4
    1 5
    
    Expected output
    2
    
  4. Example 4

    Input
    5 1
    1 2
    1 3
    1 4
    1 5
    
    Expected output
    1
    
  5. Example 5

    Input
    8 2
    1 2
    1 3
    1 4
    1 5
    2 6
    2 7
    2 8
    
    Expected output
    3