Problem p04049 (C++)

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

typedef long long LL;
typedef pair<int, int> pii;

#define fst first
#define snd second
#define pb push_back
#define eb emplace_back
#define REP(i, a, b) for (int i = (a), i##end = (b); i < i##end; ++i)
#define DREP(i, a, b) for (int i = (a - 1), i##end = (b); i >= i##end; --i)

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

const int oo = 0x3f3f3f3f;
const int maxn = 2000 + 5;

template <typename T> T read() {
  T n(0), f(1);
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    n = n * 10 + ch - 48;
  return n * f;
}

vector<int> G[maxn];

int n, k;
int dep[maxn], ans = INT_MAX;

int res = 0;
void dfs(int u, int f = 0) {
  dep[u] = dep[f] + 1;
  if (dep[u] > k / 2 + 1)
    ++res;

  for (int i = 0; i < int(G[u].size()); ++i) {
    int v = G[u][i];
    if (v == f)
      continue;
    dfs(v, u);
  }
}

int main() {
  freopen("c.in", "r", stdin);
  freopen("c.out", "w", stdout);

  n = read<int>();
  k = read<int>();
  for (int i = 1; i < n; ++i) {
    static int u, v;
    u = read<int>();
    v = read<int>();
    G[u].pb(v);
    G[v].pb(u);
  }

  if ((k & 1) == 0) {
    for (int i = 1; i <= n; ++i) {
      res = 0;
      dfs(i);
      chkmin(ans, res);
    }
  } else {
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j < int(G[i].size()); ++j) {
        res = 0;

        dep[G[i][j]] = 0;
        dfs(i, G[i][j]);
        dep[i] = 0;
        dfs(G[i][j], i);

        chkmin(ans, res);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}


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

typedef long long LL;
typedef pair<int, int> pii;

#define fst first
#define snd second
#define pb push_back
#define eb emplace_back
#define REP(i, a, b) for (int i = (a), i##end = (b); i < i##end; ++i)
#define DREP(i, a, b) for (int i = (a - 1), i##end = (b); i >= i##end; --i)

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

const int oo = 0x3f3f3f3f;
const int maxn = 2000 + 5;

template <typename T> T read() {
  T n(0), f(1);
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    n = n * 10 + ch - 48;
  return n * f;
}

vector<int> G[maxn];

int n, k;
int dep[maxn], ans = INT_MAX;

int res = 0;
void dfs(int u, int f = 0) {
  dep[u] = dep[f] + 1;
  if (dep[u] > k / 2 + 1)
    ++res;

  for (int i = 0; i < int(G[u].size()); ++i) {
    int v = G[u][i];
    if (v == f)
      continue;
    dfs(v, u);
  }
}

int main() {
  // freopen("c.in", "r", stdin);
  // freopen("c.out", "w", stdout);

  n = read<int>();
  k = read<int>();
  for (int i = 1; i < n; ++i) {
    static int u, v;
    u = read<int>();
    v = read<int>();
    G[u].pb(v);
    G[v].pb(u);
  }

  if ((k & 1) == 0) {
    for (int i = 1; i <= n; ++i) {
      res = 0;
      dfs(i);
      chkmin(ans, res);
    }
  } else {
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j < int(G[i].size()); ++j) {
        res = 0;

        dep[G[i][j]] = 0;
        dfs(i, G[i][j]);
        dep[i] = 0;
        dfs(G[i][j], i);

        chkmin(ans, res);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}