Problem:
Score : 600 points

Problem Statement

Given an undirected tree, let the distance between vertices u and v be the
number of edges on the simple path from u to v.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree good if and only if its diameter is at most K.

You are given an undirected tree with N vertices numbered 1 through N.
For each i (1≦i≦N-1), there is an edge connecting vertices A_i and B_i.

You want to remove zero or more vertices from the tree, so that the resulting tree is good.
When a vertex is removed, all incident edges will also be removed.
The resulting graph must be connected.

Find the minimum number of vertices that you need to remove in order to produce a good tree.

Constraints

2≦N≦2000

1≦K≦N-1

1≦A_i≦N, 1≦B_i≦N

The graph defined by A_i and B_i is a tree.

Input

The input is given from Standard Input in the following format:

N K
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}

Output

Print the minimum number of vertices that you need to remove in order to produce a good tree.

Sample Input 1

6 2
1 2
3 2
4 2
1 6
5 6

Sample Output 1

2

The tree is shown below. Removing vertices 5 and 6 will result in a good tree with the diameter of 2.

Sample Input 2

6 5
1 2
3 2
4 2
1 6
5 6

Sample Output 2

0

Since the given tree is already good, you do not need to remove any vertex.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.StringTokenizer;
 
public class Main {
	
	public static final int INF = Integer.MAX_VALUE /2 - 1;
	
	public static int double_sweep(ArrayList<HashSet<Integer>> adj, boolean[] deleted, final int K){
		int start = -1;
		for(int i = 0; i < deleted.length; i++){
			if(!deleted[i]){ start = i; }
		}
		
		LinkedList<Integer> node_queue = new LinkedList<Integer>();
		LinkedList<Integer> parent_queue = new LinkedList<Integer>();
		int[] node_depth = new int[adj.size()];
		Arrays.fill(node_depth, INF);
		
		node_queue.add(start);
		node_depth[start] = 0;
		parent_queue.add(-1);
		
		int max_depth = 0, max_node = start;
		while(!node_queue.isEmpty()){
			final int node   = node_queue.poll();
			final int parent = parent_queue.poll();
			
			for(final int next : adj.get(node)){
				if(next == parent){ continue; }
				if(deleted[next]){ continue; }
				
				node_depth[next] = node_depth[node] + 1;
				if(max_depth < node_depth[next]){
					max_depth = node_depth[next];
					max_node = next;
				}
				node_queue.add(next);
				parent_queue.add(node);
			}
		}
		
		node_queue.clear();
		parent_queue.clear();
		Arrays.fill(node_depth, INF);
		
		node_queue.add(max_node);
		parent_queue.add(-1);
		node_depth[max_node] = 0;
		max_depth = 0;
		
		while(!node_queue.isEmpty()){
			final int node   = node_queue.poll();
			final int parent = parent_queue.poll();
			
			for(final int next : adj.get(node)){
				if(next == parent){ continue; }
				if(deleted[next]){ continue; }
				
				node_depth[next] = node_depth[node] + 1;
				max_depth = Math.max(max_depth, node_depth[next]);
				node_queue.add(next);
				parent_queue.add(node);
			}
		}
		
		if(max_depth <= K){ return 0; }
		
		int delete_count = 0;
		for(int i = 0; i < node_depth.length; i++){
			if(node_depth[i] == max_depth){
				deleted[i] = true;
				delete_count++;
			}
		}
		
		return delete_count;
	}
	
	public static void main(String[] args) throws IOException {
		final Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final int K = sc.nextInt();
		
		ArrayList<HashSet<Integer>> adj = new ArrayList<HashSet<Integer>>();
		for(int i = 0; i < N; i++){
			adj.add(new HashSet<Integer>());
		}
		
		for(int i = 0; i < N - 1; i++){
			final int fst = sc.nextInt() - 1;
			final int snd = sc.nextInt() - 1;
			
			adj.get(fst).add(snd);
			adj.get(snd).add(fst);
		}
		
		boolean[] deleted = new boolean[N];
		int count = 0;
		while(true){
			final int ret = double_sweep(adj, deleted, K);
			if(ret == 0){ break; }
			
			count += ret;
		}
		System.out.println(count);
	}
 
	public static class Scanner implements Closeable {
		private BufferedReader br;
		private StringTokenizer tok;
 
		public Scanner(InputStream is) throws IOException {
			br = new BufferedReader(new InputStreamReader(is));
		}
 
		private void getLine() throws IOException {
			while (!hasNext()) {
				tok = new StringTokenizer(br.readLine());
			}
		}
 
		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}
 
		public String next() throws IOException {
			getLine();
			return tok.nextToken();
		}
 
		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
 
		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}
 
		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}
 
		public void close() throws IOException {
			br.close();
		}
	}
}