Problem p04049 (C++)

Buggy submission (status: Runtime Error):
/* おまじない　*/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
typedef long long ll;
typedef long double ld;
using namespace std;
const ll MOD = pow(10, 9) + 7;
const ll INF = (1LL << 62);
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
void init() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

int main() {
  int n, x;
  ll N, K;
  cin >> N >> K;
  if (K % 2 == 0) {
    vector<ll> G[N];
    for (int i = 0; i < N - 1; i++) {
      ll a, b;
      cin >> a >> b;
      a--;
      b--;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    ll ans = INF;
    for (int i = 0; i < N; i++) {
      // iを中心とする直径距離を求める
      vector<ll> d(N, -1);
      queue<pair<ll, ll>> que;
      que.push(make_pair(i, 0));
      d[i] = 0;
      while (!que.empty()) {
        ll u = que.front().first;
        ll dis = que.front().second;
        que.pop();
        for (int j = 0; j < G[u].size(); j++) {
          ll v = G[u][j];
          if (d[v] != -1)
            continue;
          d[v] = dis + 1;
          que.push(make_pair(v, dis + 1));
        }
      }
      ll num = 0;
      for (int j = 0; j < N; j++) {
        if (d[j] > K / 2)
          num++;
      }
      ans = min(num, ans);
    }
    cout << ans << endl;
    return 0;
  }
  return 1;
}


Fixed submission:
/* おまじない　*/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
typedef long long ll;
typedef long double ld;
using namespace std;
const ll MOD = pow(10, 9) + 7;
const ll INF = (1LL << 62);
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
void init() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

int main() {
  int n, x;
  ll N, K;
  cin >> N >> K;
  if (K % 2 == 0) {
    vector<ll> G[N];
    for (int i = 0; i < N - 1; i++) {
      ll a, b;
      cin >> a >> b;
      a--;
      b--;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    ll ans = INF;
    for (int i = 0; i < N; i++) {
      // iを中心とする直径距離を求める
      vector<ll> d(N, -1);
      queue<pair<ll, ll>> que;
      que.push(make_pair(i, 0));
      d[i] = 0;
      while (!que.empty()) {
        ll u = que.front().first;
        ll dis = que.front().second;
        que.pop();
        for (int j = 0; j < G[u].size(); j++) {
          ll v = G[u][j];
          if (d[v] != -1)
            continue;
          d[v] = dis + 1;
          que.push(make_pair(v, dis + 1));
        }
      }
      ll num = 0;
      for (int j = 0; j < N; j++) {
        if (d[j] > K / 2)
          num++;
      }
      ans = min(num, ans);
    }
    cout << ans << endl;
    return 0;
  }

  // 奇数
  vector<pair<ll, ll>> edge;
  vector<ll> G[N];
  for (int i = 0; i < N - 1; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    edge.push_back(make_pair(a, b));
    G[a].push_back(b);
    G[b].push_back(a);
  }

  ll ans = INF;

  for (int i = 0; i < edge.size(); i++) {
    // iの辺が中心として距離を求める
    vector<ll> d(N, -1);
    queue<pair<ll, ll>> que;
    ll a = edge[i].first;
    ll b = edge[i].second;
    que.push(make_pair(a, 0));
    que.push(make_pair(b, 0));
    d[a] = 0;
    d[b] = 0;
    while (!que.empty()) {
      ll u = que.front().first;
      ll dis = que.front().second;
      que.pop();
      for (int j = 0; j < G[u].size(); j++) {
        ll v = G[u][j];
        if (d[v] != -1)
          continue;
        d[v] = dis + 1;
        que.push(make_pair(v, dis + 1));
      }
    }
    ll num = 0;
    for (int j = 0; j < N; j++) {
      if (d[j] > K / 2)
        num++;
    }
    ans = min(num, ans);
  }

  cout << ans << endl;
}