Problem p04010 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 5e5;
int f[N], fst[N], nxt[N], to[N], vis[N], col[N], stack[N], top,
    root = 1, rcnt, ring[N], n, m, mm, tmp[N];
int abs(int x) { return x > 0 ? x : -x; }
void dfs(int u, int fa) {
  f[u] = col[u] ? 1 : -1;
  for (int i = fst[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == fa)
      continue;
    if ((u == ring[1] && v == ring[rcnt]) || (u == ring[rcnt] && v == ring[1]))
      continue;
    dfs(v, u);
    f[u] += f[v];
  }
}
void prework(int u, int c, int fa) {
  vis[u] = 1, col[u] = c;
  stack[++top] = u;
  for (int i = fst[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == fa)
      continue;
    if (u == ring[rcnt] && v == ring[1])
      continue;
    if (vis[v]) {
      while (stack[top + 1] != v)
        ring[++rcnt] = stack[top--];
      root = u;
      continue;
    }
    prework(v, c ^ 1, u);
  }
  --top;
}
void ade(int u, int v) { to[++mm] = v, nxt[mm] = fst[u], fst[u] = mm; }
int main() {
  scanf("%d%d", &n, &m);
  if (n & 1) {
    puts("-1");
    return 0;
  }
  for (int i = 1, u, v; i <= m; i++)
    scanf("%d%d", &u, &v), ade(u, v), ade(v, u);
  prework(1, 0, 0);
  dfs(root, 0);
  if (m == n) {
    if (rcnt & 1) {

      if (f[root] & 1) {
        puts("-1");
        return 0;
      }
      int t = f[root] >> 1;
      for (int i = 1; i <= rcnt; i++)
        f[ring[i]] -= t;
    } else {
      if (f[root]) {
        puts("-1");
        return 0;
      }
      for (int i = 2; i <= rcnt; i++)
        tmp[i] = f[ring[i]];
      tmp[1] = 0;
      sort(tmp + 1, tmp + rcnt + 1);
      int t = tmp[rcnt >> 1];
      for (int i = 1; i <= rcnt; i++)
        f[ring[i]] -= t;
    }
  } else if (f[root]) {
    puts("-1");
    return 0;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans += abs(f[i]);
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 5e5;
int f[N], fst[N], nxt[N], to[N], vis[N], col[N], stack[N], top,
    root = 1, rcnt, ring[N], n, m, mm, tmp[N];
int abs(int x) { return x > 0 ? x : -x; }
void dfs(int u, int fa) {
  f[u] = col[u] ? 1 : -1;
  for (int i = fst[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == fa)
      continue;
    if ((u == ring[1] && v == ring[rcnt]) || (u == ring[rcnt] && v == ring[1]))
      continue;
    dfs(v, u);
    f[u] += f[v];
  }
}
void prework(int u, int c, int fa) {
  vis[u] = 1, col[u] = c;
  stack[++top] = u;
  for (int i = fst[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == fa)
      continue;
    if (u == ring[rcnt] && v == ring[1])
      continue;
    if (vis[v]) {
      while (stack[top + 1] != v)
        ring[++rcnt] = stack[top--];
      top += rcnt;
      root = u;
      continue;
    }
    prework(v, c ^ 1, u);
  }
  --top;
}
void ade(int u, int v) { to[++mm] = v, nxt[mm] = fst[u], fst[u] = mm; }
int main() {
  scanf("%d%d", &n, &m);
  if (n & 1) {
    puts("-1");
    return 0;
  }
  for (int i = 1, u, v; i <= m; i++)
    scanf("%d%d", &u, &v), ade(u, v), ade(v, u);
  prework(1, 0, 0);
  dfs(root, 0);
  if (m == n) {
    if (rcnt & 1) {

      if (f[root] & 1) {
        puts("-1");
        return 0;
      }
      int t = f[root] >> 1;
      for (int i = 1; i <= rcnt; i++)
        f[ring[i]] -= t;
    } else {
      if (f[root]) {
        puts("-1");
        return 0;
      }
      for (int i = 2; i <= rcnt; i++)
        tmp[i] = f[ring[i]];
      tmp[1] = 0;
      sort(tmp + 1, tmp + rcnt + 1);
      int t = tmp[rcnt >> 1];
      for (int i = 1; i <= rcnt; i++)
        f[ring[i]] -= t;
    }
  } else if (f[root]) {
    puts("-1");
    return 0;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans += abs(f[i]);
  cout << ans << endl;
}