Problem p04049 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <iterator>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <time.h>
#include <utility>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define FILL(A, val) memset(A, val, sizeof(A))
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair

typedef long long Int;
typedef unsigned long long UInt;
typedef vector<int> VI;
typedef pair<int, int> PII;

const double Pi = acos(-1.0);
const int INF = 1000000000;

vector<int> g[2000];

int dfs(int x, int p, int d) {
  if (d == 0)
    return 0;

  int res = 1;
  FOR(i, 0, SZ(g[x])) {
    int to = g[x][i];
    if (to == p)
      continue;

    res += dfs(to, x, d - 1);
  }

  return res;
}

int main() {
  freopen("in.txt", "r", stdin);

  int n, m;
  scanf("%d%d", &n, &m);
  FOR(i, 0, n - 1) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y--;

    g[x].push_back(y);
    g[y].push_back(x);
  }

  int mm = 0;
  FOR(i, 0, n) {
    if (m % 2 == 0) {
      int c = 0;
      FOR(k, 0, SZ(g[i])) { c += dfs(g[i][k], i, m / 2); }

      //	cout << "*" << i << ' ' << c << endl;

      mm = max(mm, c + 1);
    } else {
      vector<pair<int, int>> a;
      FOR(k, 0, SZ(g[i])) {
        a.push_back(MP(dfs(g[i][k], i, m / 2), dfs(g[i][k], i, m / 2 + 1)));
      }

      int s = 0;
      FOR(k, 0, SZ(a)) { s += a[k].first; }

      FOR(k, 0, SZ(a)) {
        int c = s - a[k].first + a[k].second;

        mm = max(mm, c + 1);
      }
    }
  }

  cout << n - mm << endl;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <iterator>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <time.h>
#include <utility>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define FILL(A, val) memset(A, val, sizeof(A))
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair

typedef long long Int;
typedef unsigned long long UInt;
typedef vector<int> VI;
typedef pair<int, int> PII;

const double Pi = acos(-1.0);
const int INF = 1000000000;

vector<int> g[2000];

int dfs(int x, int p, int d) {
  if (d == 0)
    return 0;

  int res = 1;
  FOR(i, 0, SZ(g[x])) {
    int to = g[x][i];
    if (to == p)
      continue;

    res += dfs(to, x, d - 1);
  }

  return res;
}

int main() {
  //	freopen("in.txt","r",stdin);

  int n, m;
  scanf("%d%d", &n, &m);
  FOR(i, 0, n - 1) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y--;

    g[x].push_back(y);
    g[y].push_back(x);
  }

  int mm = 0;
  FOR(i, 0, n) {
    if (m % 2 == 0) {
      int c = 0;
      FOR(k, 0, SZ(g[i])) { c += dfs(g[i][k], i, m / 2); }

      //	cout << "*" << i << ' ' << c << endl;

      mm = max(mm, c + 1);
    } else {
      vector<pair<int, int>> a;
      FOR(k, 0, SZ(g[i])) {
        a.push_back(MP(dfs(g[i][k], i, m / 2), dfs(g[i][k], i, m / 2 + 1)));
      }

      int s = 0;
      FOR(k, 0, SZ(a)) { s += a[k].first; }

      FOR(k, 0, SZ(a)) {
        int c = s - a[k].first + a[k].second;

        mm = max(mm, c + 1);
      }
    }
  }

  cout << n - mm << endl;
}