Problem p04049 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()

const ll MOD = (ll)1e9 + 7;
const ll HINF = (ll)1e18;
const ll LINF = (ll)1e9;
const long double PI = 3.1415926535897932384626433;

template <class T> void corner(bool flg, T hoge) {
  if (flg) {
    cout << hoge << endl;
    exit(0);
  } else
    return;
}

template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto &x : obj)
    o << " {" << x.first << " : " << x.second << "}"
      << ",";
  o << " }";
  return o;
}

template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}

template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
  o << "{";
  for (int i = 0; i < (int)obj.size(); ++i)
    o << (i > 0 ? ", " : "") << obj[i];
  o << "}";
  return o;
}

template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
  o << "{" << obj.first << ", " << obj.second << "}";
  return o;
}

template <template <class tmp> class T, class U>
ostream &operator<<(ostream &o, const T<U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}

void print(void) { cout << endl; }

template <class Head> void print(Head &&head) {
  cout << head;
  print();
}

template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  cout << head << " ";
  print(forward<Tail>(tail)...);
}

void YN(bool flg) { cout << ((flg) ? "YES" : "NO") << endl; }

void Yn(bool flg) { cout << ((flg) ? "Yes" : "No") << endl; }

void yn(bool flg) { cout << ((flg) ? "yes" : "no") << endl; }

const int MAXN = 10;
int N, K;
V<V<int>> dist(MAXN, V<int>(MAXN, -1));
V<V<int>> edge(MAXN);

void bfs(int s) {
  queue<int> q;
  q.push(s);
  dist[s][s] = 0;
  while (!q.empty()) {
    int from = q.front();
    q.pop();
    for (auto to : edge[from]) {
      if (dist[s][to] == -1) {
        q.push(to);
        dist[s][to] = dist[s][from] + 1;
      }
    }
  }
  return;
}

int main() {
  cin >> N >> K;
  REP(i, N - 1) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    edge[A].push_back(B);
    edge[B].push_back(A);
  }
  REP(i, N) bfs(i);
  if (K % 2) {
    // odd
    V<int> cnt((2 * (N - 1)), 0);
    int idx = 0;
    REP(i, N) {
      for (auto j : edge[i]) {
        REP(k, N) {
          int l = dist[i][k], r = dist[j][k];
          if (l > K / 2 && r > K / 2)
            cnt[idx]++;
        }
        idx++;
      }
    }
    cout << *min_element(ALL(cnt)) << endl;
  } else {
    // even
    V<int> cnt(N, 0);
    REP(i, N) REP(j, N) if (dist[i][j] > K / 2) cnt[i]++;
    cout << *min_element(ALL(cnt)) << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()

const ll MOD = (ll)1e9 + 7;
const ll HINF = (ll)1e18;
const ll LINF = (ll)1e9;
const long double PI = 3.1415926535897932384626433;

template <class T> void corner(bool flg, T hoge) {
  if (flg) {
    cout << hoge << endl;
    exit(0);
  } else
    return;
}

template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto &x : obj)
    o << " {" << x.first << " : " << x.second << "}"
      << ",";
  o << " }";
  return o;
}

template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}

template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
  o << "{";
  for (int i = 0; i < (int)obj.size(); ++i)
    o << (i > 0 ? ", " : "") << obj[i];
  o << "}";
  return o;
}

template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
  o << "{" << obj.first << ", " << obj.second << "}";
  return o;
}

template <template <class tmp> class T, class U>
ostream &operator<<(ostream &o, const T<U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}

void print(void) { cout << endl; }

template <class Head> void print(Head &&head) {
  cout << head;
  print();
}

template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  cout << head << " ";
  print(forward<Tail>(tail)...);
}

void YN(bool flg) { cout << ((flg) ? "YES" : "NO") << endl; }

void Yn(bool flg) { cout << ((flg) ? "Yes" : "No") << endl; }

void yn(bool flg) { cout << ((flg) ? "yes" : "no") << endl; }

const int MAXN = 2000;
int N, K;
V<V<int>> dist(MAXN, V<int>(MAXN, -1));
V<V<int>> edge(MAXN);

void bfs(int s) {
  queue<int> q;
  q.push(s);
  dist[s][s] = 0;
  while (!q.empty()) {
    int from = q.front();
    q.pop();
    for (auto to : edge[from]) {
      if (dist[s][to] == -1) {
        q.push(to);
        dist[s][to] = dist[s][from] + 1;
      }
    }
  }
  return;
}

int main() {
  cin >> N >> K;
  REP(i, N - 1) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    edge[A].push_back(B);
    edge[B].push_back(A);
  }
  REP(i, N) bfs(i);
  if (K % 2) {
    // odd
    V<int> cnt((2 * (N - 1)), 0);
    int idx = 0;
    REP(i, N) {
      for (auto j : edge[i]) {
        REP(k, N) {
          int l = dist[i][k], r = dist[j][k];
          if (l > K / 2 && r > K / 2)
            cnt[idx]++;
        }
        idx++;
      }
    }
    cout << *min_element(ALL(cnt)) << endl;
  } else {
    // even
    V<int> cnt(N, 0);
    REP(i, N) REP(j, N) if (dist[i][j] > K / 2) cnt[i]++;
    cout << *min_element(ALL(cnt)) << endl;
  }
  return 0;
}