Problem p04049 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)
int n, k, dist[3000];
vector<int> X[3000];
queue<int> Q;
bool I[3000];
int search_1(int p) {
  int maxn = 0;
  for (int i = 1; i <= n; i++) {
    for (int i = 0; i < 3000; i++)
      dist[i] = 100000;
    dist[i] = 0;
    Q.push(i);
    while (!Q.empty()) {
      int a1 = Q.front();
      Q.pop();
      for (int i = 0; i < X[a1].size(); i++) {
        if (dist[X[a1][i]] > dist[a1] + 1) {
          dist[X[a1][i]] = dist[a1] + 1;
          Q.push(X[a1][i]);
        }
      }
    }
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      if (dist[i] <= p)
        sum++;
    }
    maxn = max(maxn, sum);
  }
  return n - maxn;
}
int search_2(int p) {
  int maxn = 0;
  for (int t = 1; t <= n; t++) {
    for (int i = 0; i < 3000; i++)
      dist[i] = 100000;
    dist[t] = 0;
    Q.push(t);
    while (!Q.empty()) {
      int a1 = Q.front();
      Q.pop();
      for (int i = 0; i < X[a1].size(); i++) {
        if (dist[X[a1][i]] > dist[a1] + 1) {
          dist[X[a1][i]] = dist[a1] + 1;
          Q.push(X[a1][i]);
        }
      }
    }
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      if (dist[i] <= p)
        sum++;
    }
    int D = 0;
    for (int i = 0; i < X[1].size(); i++) {
      for (int j = 0; j < 3000; j++)
        I[j] = false;
      Q.push(X[t][i]);
      I[X[t][i]] = true;
      while (!Q.empty()) {
        int a1 = Q.front();
        Q.pop();
        for (int j = 0; j < X[a1].size(); j++) {
          int to = X[a1][j];
          if (to == t)
            continue;
          if (I[to] == false) {
            I[to] = true;
            Q.push(to);
          }
        }
      }
      int sum1 = 0;
      for (int i = 1; i <= n; i++) {
        if (I[i] == true && dist[i] == p + 1)
          sum1++;
      }
      D = max(D, sum1);
    }
    maxn = max(maxn, D + sum);
  }
  return n - maxn;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    X[a].push_back(b);
    X[b].push_back(a);
  }
  if (k % 2 == 0) {
    cout << search_1(k / 2) << endl;
  } else {
    cout << search_2((k - 1) / 2) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)
int n, k, dist[3000];
vector<int> X[3000];
queue<int> Q;
bool I[3000];
int search_1(int p) {
  int maxn = 0;
  for (int i = 1; i <= n; i++) {
    for (int i = 0; i < 3000; i++)
      dist[i] = 100000;
    dist[i] = 0;
    Q.push(i);
    while (!Q.empty()) {
      int a1 = Q.front();
      Q.pop();
      for (int i = 0; i < X[a1].size(); i++) {
        if (dist[X[a1][i]] > dist[a1] + 1) {
          dist[X[a1][i]] = dist[a1] + 1;
          Q.push(X[a1][i]);
        }
      }
    }
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      if (dist[i] <= p)
        sum++;
    }
    maxn = max(maxn, sum);
  }
  return n - maxn;
}
int search_2(int p) {
  int maxn = 0;
  for (int t = 1; t <= n; t++) {
    for (int i = 0; i < 3000; i++)
      dist[i] = 100000;
    dist[t] = 0;
    Q.push(t);
    while (!Q.empty()) {
      int a1 = Q.front();
      Q.pop();
      for (int i = 0; i < X[a1].size(); i++) {
        if (dist[X[a1][i]] > dist[a1] + 1) {
          dist[X[a1][i]] = dist[a1] + 1;
          Q.push(X[a1][i]);
        }
      }
    }
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      if (dist[i] <= p)
        sum++;
    }
    int D = 0;
    for (int i = 0; i < X[t].size(); i++) {
      for (int j = 0; j < 3000; j++)
        I[j] = false;
      Q.push(X[t][i]);
      I[X[t][i]] = true;
      while (!Q.empty()) {
        int a1 = Q.front();
        Q.pop();
        for (int j = 0; j < X[a1].size(); j++) {
          int to = X[a1][j];
          if (to == t)
            continue;
          if (I[to] == false) {
            I[to] = true;
            Q.push(to);
          }
        }
      }
      int sum1 = 0;
      for (int i = 1; i <= n; i++) {
        if (I[i] == true && dist[i] == p + 1)
          sum1++;
      }
      D = max(D, sum1);
    }
    maxn = max(maxn, D + sum);
  }
  return n - maxn;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    X[a].push_back(b);
    X[b].push_back(a);
  }
  if (k % 2 == 0) {
    cout << search_1(k / 2) << endl;
  } else {
    cout << search_2((k - 1) / 2) << endl;
  }
  return 0;
}