Problem p04049 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;

#define fi first
#define se second

const int MAXN = 1005;
const int INF = 1e9;

int N, K, dist[MAXN];
vector<int> adjList[MAXN];
vector<ii> edgeList;

int main() {

  scanf("%d%d", &N, &K);
  for (int i = 1, u, v; i < N; i++) {
    scanf("%d%d", &u, &v);

    adjList[u].push_back(v);
    adjList[v].push_back(u);
    edgeList.push_back(ii(u, v));
  }

  int D = K / 2;

  int ans = INF;

  if (K % 2 == 0) {
    for (int i = 1; i <= N; i++) {
      memset(dist, -1, sizeof(dist));
      dist[i] = 0;
      queue<int> q;
      q.push(i);

      int tmp = 0;
      while (!q.empty()) {
        int node = q.front();
        q.pop();

        // cout << node << " " << dist[node] << endl;

        if (dist[node] > D)
          tmp++;

        for (int j = 0; j < adjList[node].size(); j++) {
          int neighbor = adjList[node][j];

          if (dist[neighbor] == -1) {
            dist[neighbor] = dist[node] + 1;
            q.push(neighbor);
          }
        }
      }

      ans = min(ans, tmp);
    }
  } else {
    for (int i = 0; i < edgeList.size(); i++) {
      memset(dist, -1, sizeof(dist));
      dist[edgeList[i].fi] = dist[edgeList[i].se] = 0;
      queue<int> q;
      q.push(edgeList[i].fi);
      q.push(edgeList[i].se);

      int tmp = 0;
      while (!q.empty()) {
        int node = q.front();
        q.pop();

        // cout << node << " " << dist[node] << endl;

        if (dist[node] > D)
          tmp++;

        for (int j = 0; j < adjList[node].size(); j++) {
          int neighbor = adjList[node][j];

          if (dist[neighbor] == -1) {
            dist[neighbor] = dist[node] + 1;
            q.push(neighbor);
          }
        }
      }

      ans = min(ans, tmp);
    }
  }

  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;

#define fi first
#define se second

const int MAXN = 2005;
const int INF = 1e9;

int N, K, dist[MAXN];
vector<int> adjList[MAXN];
vector<ii> edgeList;

int main() {

  scanf("%d%d", &N, &K);
  for (int i = 1, u, v; i < N; i++) {
    scanf("%d%d", &u, &v);

    adjList[u].push_back(v);
    adjList[v].push_back(u);
    edgeList.push_back(ii(u, v));
  }

  int D = K / 2;

  int ans = INF;

  if (K % 2 == 0) {
    for (int i = 1; i <= N; i++) {
      memset(dist, -1, sizeof(dist));
      dist[i] = 0;
      queue<int> q;
      q.push(i);

      int tmp = 0;
      while (!q.empty()) {
        int node = q.front();
        q.pop();

        // cout << node << " " << dist[node] << endl;

        if (dist[node] > D)
          tmp++;

        for (int j = 0; j < adjList[node].size(); j++) {
          int neighbor = adjList[node][j];

          if (dist[neighbor] == -1) {
            dist[neighbor] = dist[node] + 1;
            q.push(neighbor);
          }
        }
      }

      ans = min(ans, tmp);
    }
  } else {
    for (int i = 0; i < edgeList.size(); i++) {
      memset(dist, -1, sizeof(dist));
      dist[edgeList[i].fi] = dist[edgeList[i].se] = 0;
      queue<int> q;
      q.push(edgeList[i].fi);
      q.push(edgeList[i].se);

      int tmp = 0;
      while (!q.empty()) {
        int node = q.front();
        q.pop();

        // cout << node << " " << dist[node] << endl;

        if (dist[node] > D)
          tmp++;

        for (int j = 0; j < adjList[node].size(); j++) {
          int neighbor = adjList[node][j];

          if (dist[neighbor] == -1) {
            dist[neighbor] = dist[node] + 1;
            q.push(neighbor);
          }
        }
      }

      ans = min(ans, tmp);
    }
  }

  printf("%d\n", ans);

  return 0;
}