Problem p03441 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vecint;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
const int inf = 1000000000;
const ll INF = 9000000000000000000;
const double EPS = 10e-9;
int n, root;
vecint G[100001];
int num[100001];
int res;
vecint edge;
int dfs(int v, int p) {
  // cout << "v " << v << endl;
  int num0 = 0;
  rep(i, G[v].size()) {
    if (G[v][i] == p)
      continue;
    if (G[G[v][i]].size() == 1)
      num0++;
    else {
      int chi = dfs(G[v][i], v);
      // cout << "chi " << chi << endl;
      if (chi == 0)
        num0++;
      else
        num[v] += chi;
    }
  }
  // cout << "num0 " << num0 << endl;
  if (num0)
    num[v] += (num0 - 1);
  // cout << "numv " << num[v] << endl << endl;
  return num[v];
}
void dfs1(int v, int p) {
  if (p == -1 && G[v].size() == 1)
    edge.push_back(v);
  rep(i, G[v].size()) {
    if (G[v][i] == p)
      continue;
    if (G[G[v][i]].size() == 1) {
      edge.push_back(G[v][i]);
    } //{ root = G[v][i]; break; }
    else
      dfs1(G[v][i], v);
    // if (root >= 0) break;
  }
}
int main() {
  cin >> n;
  res = inf;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  root = -1;
  dfs1(0, -1);
  // cout << "root" << root << endl;
  rep(i, min(325, (int)edge.size())) {
    memset(num, 0, sizeof(num));
    res = min(res, dfs(edge[i], -1));
  }
  // int res = dfs(root, -1);
  res++;
  cout << res << endl;
  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vecint;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
const int inf = 1000000000;
const ll INF = 9000000000000000000;
const double EPS = 10e-9;
int n, root;
vecint G[100001];
int num[100001];
int res;
vecint edge;
int dfs(int v, int p) {
  // cout << "v " << v << endl;
  int num0 = 0;
  rep(i, G[v].size()) {
    if (G[v][i] == p)
      continue;
    if (G[G[v][i]].size() == 1)
      num0++;
    else {
      int chi = dfs(G[v][i], v);
      // cout << "chi " << chi << endl;
      if (chi == 0)
        num0++;
      else
        num[v] += chi;
    }
  }
  // cout << "num0 " << num0 << endl;
  if (num0)
    num[v] += (num0 - 1);
  // cout << "numv " << num[v] << endl << endl;
  return num[v];
}
void dfs1(int v, int p) {
  if (p == -1 && G[v].size() == 1)
    edge.push_back(v);
  rep(i, G[v].size()) {
    if (G[v][i] == p)
      continue;
    if (G[G[v][i]].size() == 1) {
      edge.push_back(G[v][i]);
    } //{ root = G[v][i]; break; }
    else
      dfs1(G[v][i], v);
    // if (root >= 0) break;
  }
}
int main() {
  cin >> n;
  res = inf;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  root = -1;
  dfs1(0, -1);
  // cout << "root" << root << endl;
  rep(i, min(310, (int)edge.size())) {
    memset(num, 0, sizeof(num));
    res = min(res, dfs(edge[i], -1));
  }
  // int res = dfs(root, -1);
  res++;
  cout << res << endl;
  return 0;
}