Problem p03441 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, x, n) for (int i = x; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(X) (X).begin(), (X).end()
#define X first
#define Y second
#define pb push_back
#define eb emplace_back

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class A, size_t N, class T> void Fill(A (&a)[N], const T &v) {
  fill((T *)a, (T *)(a + N), v);
}

const ll INF = 1e9 + 7;

vector<int> edge[1000005];
map<int, int> memo[1000005];
int dfs(int n, int b) {
  if (memo[n].count(b))
    return memo[n][b];
  if (b >= 0 && edge[n].size() == 1)
    return -1;
  int ret = 0, cnt = 0;
  for (auto t : edge[n])
    if (t != b) {
      auto r = dfs(t, n);
      if (r < 0)
        cnt++;
      else
        ret += r;
    }
  // cout << "dfs: " << n << " " << ret << " " << cnt << endl;
  if (ret == 0 && cnt == 1)
    return memo[n][b] = -1;
  if (cnt > 0)
    ret += cnt - 1;
  return memo[n][b] = ret;
}

int main() {
  ios_base::sync_with_stdio(false);
  int N;

  cin >> N;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }

  int ans = INF;
  rep(i, N) if (edge[i].size() == 1) {
    int ret = 0, cnt = 0;
    for (auto t : edge[i]) {
      auto r = dfs(t, i);
      if (r < 0)
        cnt++;
      else
        ret += r;
    }
    if (edge[i].size() <= 1 && ret > 0 && cnt == 0)
      ret++;
    if (ret == 0 && cnt == 1)
      ret = 1;
    else if (cnt > 1)
      ret += cnt - 1;
    else if (ret > 0 && cnt == 1)
      ret += cnt;
    else if (cnt > 0)
      ret += cnt - 1;
    // cout << i << "=" << ret << endl;
    chmin(ans, ret);
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, x, n) for (int i = x; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(X) (X).begin(), (X).end()
#define X first
#define Y second
#define pb push_back
#define eb emplace_back

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class A, size_t N, class T> void Fill(A (&a)[N], const T &v) {
  fill((T *)a, (T *)(a + N), v);
}

const ll INF = 1e9 + 7;

vector<int> edge[1000005];
map<int, int> memo[1000005];
int dfs(int n, int b) {
  if (memo[n].count(b))
    return memo[n][b];
  if (b >= 0 && edge[n].size() == 1)
    return -1;
  int ret = 0, cnt = 0;
  for (auto t : edge[n])
    if (t != b) {
      auto r = dfs(t, n);
      if (r < 0)
        cnt++;
      else
        ret += r;
    }
  // cout << "dfs: " << n << " " << ret << " " << cnt << endl;
  if (ret == 0 && cnt == 1)
    return memo[n][b] = -1;
  if (cnt > 0)
    ret += cnt - 1;
  return memo[n][b] = ret;
}

int main() {
  ios_base::sync_with_stdio(false);
  int N;

  cin >> N;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }

  int ans = INF;
  rep(i, N) if (i == 0 || edge[i].size() != 1) {
    int ret = 0, cnt = 0;
    for (auto t : edge[i]) {
      auto r = dfs(t, i);
      if (r < 0)
        cnt++;
      else
        ret += r;
    }
    if (edge[i].size() <= 1 && ret > 0 && cnt == 0)
      ret++;
    if (ret == 0 && cnt == 1)
      ret = 1;
    else if (cnt > 1)
      ret += cnt - 1;
    else if (ret > 0 && cnt == 1)
      ret += cnt;
    else if (cnt > 0)
      ret += cnt - 1;
    // cout << i << "=" << ret << endl;
    chmin(ans, ret);
  }

  cout << ans << endl;

  return 0;
}