Problem p03441 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    for (int W = 0; W < (n); W++)                                              \
      cerr << v[W] << ' ';                                                     \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, i, j)                                                        \
  {                                                                            \
    for (int aaa = 0; aaa < i; aaa++) {                                        \
      for (int bbb = 0; bbb < j; bbb++)                                        \
        cerr << v[aaa][bbb] << ' ';                                            \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(10)

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

vector<vector<int>> v(111111);
ll ans, n;
ll ret = 1000000;

bool dfs(int now, int prev) {
  if (v[now].size() == 1 && prev != -1) {
    return false;
  }

  int tmp = 0;
  REP(i, v[now].size()) {
    if (v[now][i] == prev)
      continue;

    if (dfs(v[now][i], now)) {
      tmp++;
    }
  }

  // cout << now << ' ' << v[now].size() - 1 << ' ' << tmp << endl;
  if ((int)v[now].size() - 2 > tmp) {
    // cout << ' ' << v[now].size()-2-tmp << endl;;
    ans += v[now].size() - 2 - tmp;
    tmp++;
  }

  if (tmp)
    return true;
  else
    return false;
}

int main() {
  cin >> n;

  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    v[a].PB(b);
    v[b].PB(a);
  }

  REP(i, min(1000LL, n)) {
    //	cout << i << endl;
    ans = 0;
    dfs(i, -1);
    ret = min(ret, ans);
    //	cout << ret << endl;
  }

  cout << ret + 1 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    for (int W = 0; W < (n); W++)                                              \
      cerr << v[W] << ' ';                                                     \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, i, j)                                                        \
  {                                                                            \
    for (int aaa = 0; aaa < i; aaa++) {                                        \
      for (int bbb = 0; bbb < j; bbb++)                                        \
        cerr << v[aaa][bbb] << ' ';                                            \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(10)

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

vector<vector<int>> v(111111);
ll ans, n;
ll ret = 1000000;

bool dfs(int now, int prev) {
  if (v[now].size() == 1 && prev != -1) {
    return false;
  }

  int tmp = 0;
  REP(i, v[now].size()) {
    if (v[now][i] == prev)
      continue;

    if (dfs(v[now][i], now)) {
      tmp++;
    }
  }

  // cout << now << ' ' << v[now].size() - 1 << ' ' << tmp << endl;
  if ((int)v[now].size() - 2 > tmp) {
    // cout << ' ' << v[now].size()-2-tmp << endl;;
    ans += v[now].size() - 2 - tmp;
    tmp++;
  }

  if (tmp)
    return true;
  else
    return false;
}

int main() {
  cin >> n;

  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    v[a].PB(b);
    v[b].PB(a);
  }

  REP(i, min(10LL, n)) {
    //	cout << i << endl;
    ans = 0;
    dfs(i, -1);
    ret = min(ret, ans);
    //	cout << ret << endl;
  }

  cout << ret + 1 << endl;

  return 0;
}