Problem p04049 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define sz(v) (v).size()
#define inf 1e18
#define md 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define tel(a)                                                                 \
  { cout << a << "\n"; }
#define tell(a, b)                                                             \
  { cout << a << " | " << b << "\n"; }
#define telll(a, b, c)                                                         \
  { cout << a << " | " << b << " | " << c << "\n"; }
#define teln(v, n)                                                             \
  {                                                                            \
    cout << "v- ";                                                             \
    rep(o, 0, n) cout << v[o] << " ";                                          \
    cout << "\n";                                                              \
  }
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define tihs                                                                   \
  if (fopen("inp.txt", "r"))                                                   \
    freopen("inp.txt", "r", stdin), freopen("out.txt", "w", stdout);
using namespace std;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define M 1010

vll g[M];
ll d[M][M];
vector<pii> edges;
int main() {
  IOS;
  tihs;

  ll n, k;
  cin >> n >> k;
  rep(i, 0, n - 1) {
    ll u, v;
    cin >> u >> v;
    u--, v--;
    g[u].pb(v), g[v].pb(u);
    edges.pb({u, v});
  }
  queue<ll> q;
  rep(i, 0, n) {
    rep(j, 0, n) d[i][j] = -1;
    q.push(i);
    d[i][i] = 0;
    while (!q.empty()) {
      ll u = q.front();
      q.pop();
      for (auto v : g[u])
        if (d[i][v] == -1)
          d[i][v] = d[i][u] + 1, q.push(v);
    }
  }
  ll mn = n;
  if (k % 2 == 0) {
    rep(i, 0, n) {
      ll cnt = 0;
      rep(j, 0, n) if (d[i][j] > k / 2 and j != i) cnt++;
      mn = min(mn, cnt);
    }
    cout << mn;
    return 0;
  }
  for (auto p : edges) {
    ll v1 = p.fi, v2 = p.se, cnt = 0;
    rep(j, 0, n) {
      if (j == v1 or j == v2)
        continue;
      if (min(d[v1][j], d[v2][j]) > k / 2)
        cnt++;
    }
    mn = min(mn, cnt);
  }
  cout << mn;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define sz(v) (v).size()
#define inf 1e18
#define md 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define tel(a)                                                                 \
  { cout << a << "\n"; }
#define tell(a, b)                                                             \
  { cout << a << " | " << b << "\n"; }
#define telll(a, b, c)                                                         \
  { cout << a << " | " << b << " | " << c << "\n"; }
#define teln(v, n)                                                             \
  {                                                                            \
    cout << "v- ";                                                             \
    rep(o, 0, n) cout << v[o] << " ";                                          \
    cout << "\n";                                                              \
  }
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define tihs                                                                   \
  if (fopen("inp.txt", "r"))                                                   \
    freopen("inp.txt", "r", stdin), freopen("out.txt", "w", stdout);
using namespace std;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define M 2010

vll g[M];
ll d[M][M];
vector<pii> edges;
int main() {
  IOS;
  tihs;

  ll n, k;
  cin >> n >> k;
  rep(i, 0, n - 1) {
    ll u, v;
    cin >> u >> v;
    u--, v--;
    g[u].pb(v), g[v].pb(u);
    edges.pb({u, v});
  }
  queue<ll> q;
  rep(i, 0, n) {
    rep(j, 0, n) d[i][j] = -1;
    q.push(i);
    d[i][i] = 0;
    while (!q.empty()) {
      ll u = q.front();
      q.pop();
      for (auto v : g[u])
        if (d[i][v] == -1)
          d[i][v] = d[i][u] + 1, q.push(v);
    }
  }
  ll mn = n;
  if (k % 2 == 0) {
    rep(i, 0, n) {
      ll cnt = 0;
      rep(j, 0, n) if (d[i][j] > k / 2 and j != i) cnt++;
      mn = min(mn, cnt);
    }
    cout << mn;
    return 0;
  }
  for (auto p : edges) {
    ll v1 = p.fi, v2 = p.se, cnt = 0;
    rep(j, 0, n) {
      if (j == v1 or j == v2)
        continue;
      if (min(d[v1][j], d[v2][j]) > k / 2)
        cnt++;
    }
    mn = min(mn, cnt);
  }
  cout << mn;

  return 0;
}