Problem p03441 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long ll;
typedef double dd;
#define For(i, j, k) for (int i = j; i <= k; ++i)
#define Forr(i, j, k) for (int i = j; i >= k; --i)
#define Set(a, p) memset(a, p, sizeof(a))
using namespace std;

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 maxn = 100000 + 100;
struct node {
  int to, next;
} e[maxn];
int n, tot;
int head[maxn], cnt[maxn], dp[maxn];

inline void read(int &x) {
  x = 0;
  int p = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - '0');
    c = getchar();
  }
  x *= p;
}

inline void file() {
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
}

inline void add(int from, int to) {
  tot++;
  e[tot].to = to;
  e[tot].next = head[from];
  head[from] = tot;
}

void dfs(int u, int fa) {
  int flag = 0;
  for (int i = head[u]; i != 0; i = e[i].next) {
    int v = e[i].to;
    if (v == fa)
      continue;
    dfs(v, u);
    dp[u] += dp[v];
    if (!dp[v]) {
      if (!flag)
        flag = 1;
      else
        dp[u]++;
    }
  }
}

int main() {
  //	file();
  read(n);
  For(i, 1, n - 1) {
    int x, y;
    read(x);
    read(y);
    add(x, y);
    add(y, x);
    cnt[x]++;
    cnt[y]++;
  }
  int root = -1;
  For(i, 0, n - 1) if (cnt[i] >= 3) root = i;
  if (root == -1)
    printf("1");
  else {
    dfs(root, -1);
    printf("%d\n", dp[root]);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long ll;
typedef double dd;
#define For(i, j, k) for (int i = j; i <= k; ++i)
#define Forr(i, j, k) for (int i = j; i >= k; --i)
#define Set(a, p) memset(a, p, sizeof(a))
using namespace std;

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 maxn = 100000 + 100;
struct node {
  int to, next;
} e[maxn << 1];
int n, tot;
int head[maxn], cnt[maxn], dp[maxn];

inline void read(int &x) {
  x = 0;
  int p = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - '0');
    c = getchar();
  }
  x *= p;
}

inline void file() {
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
}

inline void add(int from, int to) {
  tot++;
  e[tot].to = to;
  e[tot].next = head[from];
  head[from] = tot;
}

void dfs(int u, int fa) {
  int flag = 0;
  for (int i = head[u]; i != 0; i = e[i].next) {
    int v = e[i].to;
    if (v == fa)
      continue;
    dfs(v, u);
    dp[u] += dp[v];
    if (!dp[v]) {
      if (!flag)
        flag = 1;
      else
        dp[u]++;
    }
  }
}

int main() {
  //	file();
  read(n);
  For(i, 1, n - 1) {
    int x, y;
    read(x);
    read(y);
    add(x, y);
    add(y, x);
    cnt[x]++;
    cnt[y]++;
  }
  int root = -1;
  For(i, 0, n - 1) if (cnt[i] >= 3) root = i;
  if (root == -1)
    printf("1");
  else {
    dfs(root, -1);
    printf("%d\n", dp[root]);
  }
  return 0;
}