Problem p04010 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 1000010
using namespace std;
int num[N], fa[N];
int find(int x) { return x == fa[x] ? fa[x] : find(fa[x]); }
int nxt[N << 1], to[N << 1], head[N], cnt;
void add(int u, int v) {
  nxt[++cnt] = head[u];
  to[cnt] = v;
  head[u] = cnt;
}
int ton[N], tot;
void dfs(int u, int d) {
  num[u] = d;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == fa[u])
      continue;
    fa[v] = u;
    dfs(v, -d);
    num[u] += num[v];
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int ru = 1, rv = 1;
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    int hx = find(u), hy = find(v);
    if (hx == hy)
      ru = u, rv = v;
    else
      fa[fa[u]] = fa[v], add(u, v), add(v, u);
  }
  fa[ru] = 0;
  if (m == n - 1) {
    dfs(1, 1);
    int ans = 0;
    if (num[1]) {
      puts("-1");
      return 0;
    }
    for (register int i = 1; i <= n; i++)
      ans += abs(num[i]);
    printf("%d\n", ans);
    return 0;
  }
  dfs(ru, 1);
  int u = rv;
  while (u)
    ton[++tot] = num[u], u = fa[u];
  if (tot & 1) {
    int ans = 0;
    if (num[ru] & 1) {
      puts("-1");
      return 0;
    }
    for (int u = rv; u; u = fa[u])
      num[u] -= num[ru] >> 1;
    for (int i = 1; i <= n; i++)
      ans += abs(num[i]);
    printf("%d\n", ans);
    return 0;
  }
  if (num[ru]) {
    puts("-1");
    return 0;
  }
  sort(ton + 1, ton + tot + 1);
  for (register int u = rv; u; u = fa[u])
    num[u] -= ton[tot >> 1];
  int ans = 0;
  for (register int i = 1; i <= n; i++)
    ans += abs(num[i]);
  printf("%d", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 1000010
using namespace std;
int num[N], fa[N];
int find(int x) { return x == fa[x] ? fa[x] : fa[x] = find(fa[x]); }
int nxt[N << 1], to[N << 1], head[N], cnt;
void add(int u, int v) {
  nxt[++cnt] = head[u];
  to[cnt] = v;
  head[u] = cnt;
}
int ton[N], tot;
void dfs(int u, int d) {
  num[u] = d;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == fa[u])
      continue;
    fa[v] = u;
    dfs(v, -d);
    num[u] += num[v];
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int ru = 1, rv = 1;
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    int hx = find(u), hy = find(v);
    if (hx == hy)
      ru = u, rv = v;
    else
      fa[fa[u]] = fa[v], add(u, v), add(v, u);
  }
  fa[ru] = 0;
  if (m == n - 1) {
    dfs(1, 1);
    int ans = 0;
    if (num[1]) {
      puts("-1");
      return 0;
    }
    for (register int i = 1; i <= n; i++)
      ans += abs(num[i]);
    printf("%d\n", ans);
    return 0;
  }
  dfs(ru, 1);
  int u = rv;
  while (u)
    ton[++tot] = num[u], u = fa[u];
  if (tot & 1) {
    int ans = 0;
    if (num[ru] & 1) {
      puts("-1");
      return 0;
    }
    for (int u = rv; u; u = fa[u])
      num[u] -= num[ru] >> 1;
    for (int i = 1; i <= n; i++)
      ans += abs(num[i]);
    printf("%d\n", ans);
    return 0;
  }
  if (num[ru]) {
    puts("-1");
    return 0;
  }
  sort(ton + 1, ton + tot + 1);
  for (register int u = rv; u; u = fa[u])
    num[u] -= ton[tot >> 1];
  int ans = 0;
  for (register int i = 1; i <= n; i++)
    ans += abs(num[i]);
  printf("%d", ans);
  return 0;
}