Problem p04010 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#define rep(i, j, k) for (i = j; i <= k; i++)
#define edge(j) for (j = fst[x]; j; j = nxt[j])
using namespace std;
const int N = 1e5;
int n, m, i, j, u, v, from, trans;
int odd, even, ans, delta;
int pn, p[N], s[N], bw[2], cnt[2];
int En, fst[N], vis[N], col[N], nxt[N * 2], to[N * 2], have[N];
void add(int u, int v) {
  En++;
  nxt[En] = fst[u];
  fst[u] = En;
  to[En] = v;
}
void dfs(int x, int fa, int flg) {
  int j, v;
  vis[x] = 1;
  col[x] = flg;
  cnt[flg]++;
  edge(j) {
    v = to[j];
    if (v == fa)
      continue;
    if (vis[v]) {
      trans = v;
      from = x;
      if (col[x] == col[v])
        odd = 1;
      else
        even = 1;
      continue;
    }
    dfs(to[j], x, flg ^ 1);
  }
}
void dfs2(int x, int fa) {
  int j, v;
  if (trans == x && even)
    have[x] = 1;
  edge(j) {
    v = to[j];
    if (x == from && v == trans)
      continue;
    if (x == trans && v == from)
      continue;
    if (v == fa)
      continue;
    dfs2(v, x);
    if (have[v])
      have[x] = 1;
    s[x] += s[v];
  }
  if (x != from) {
    if (have[x])
      p[++pn] = s[x];
    else
      ans += abs(s[x]);
  }
}
int main() {
  // freopen("reverse.in","r",stdin);
  // freopen("reverse.out","w",stdout);
  scanf("%d%d", &n, &m);
  rep(i, 1, m) {
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  dfs(1, 1, 0);
  bw[0] = 0;
  bw[1] = 1;
  if (cnt[0] < cnt[1])
    swap(bw[0], bw[1]); // to ensure num[black]<=num[white]
  rep(i, 1, n) {
    col[i] = bw[col[i]];
    if (col[i])
      s[i] = 1;
    else
      s[i] = -1;
  }

  if (odd) {
    delta = abs(cnt[0] - cnt[1]);
    if (delta % 2) {
      printf("-1\n");
      return 0;
    }
    ans += delta / 2;
    s[trans] += delta / 2;
  } else if (cnt[0] != cnt[1]) {
    printf("-1\n");
    return 0;
  }

  if (!from)
    from = 1;
  dfs2(from, from);

  if (even) {
    p[++pn] = 0; // abs(x) should be counted
    sort(p + 1, p + 1 + pn);
    rep(i, 1, pn) ans += abs(p[i] - p[pn / 2 + 1]);
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#define rep(i, j, k) for (i = j; i <= k; i++)
#define edge(j) for (j = fst[x]; j; j = nxt[j])
using namespace std;
const int N = 1e5 + 5;
int n, m, i, j, u, v, from, trans;
int odd, even, ans, delta;
int pn, p[N], s[N], bw[2], cnt[2];
int En, fst[N], vis[N], col[N], nxt[N * 2], to[N * 2], have[N];
void add(int u, int v) {
  En++;
  nxt[En] = fst[u];
  fst[u] = En;
  to[En] = v;
}
void dfs(int x, int fa, int flg) {
  int j, v;
  vis[x] = 1;
  col[x] = flg;
  cnt[flg]++;
  edge(j) {
    v = to[j];
    if (v == fa)
      continue;
    if (vis[v]) {
      trans = v;
      from = x;
      if (col[x] == col[v])
        odd = 1;
      else
        even = 1;
      continue;
    }
    dfs(to[j], x, flg ^ 1);
  }
}
void dfs2(int x, int fa) {
  int j, v;
  if (trans == x && even)
    have[x] = 1;
  edge(j) {
    v = to[j];
    if (x == from && v == trans)
      continue;
    if (x == trans && v == from)
      continue;
    if (v == fa)
      continue;
    dfs2(v, x);
    if (have[v])
      have[x] = 1;
    s[x] += s[v];
  }
  if (x != from) {
    if (have[x])
      p[++pn] = s[x];
    else
      ans += abs(s[x]);
  }
}
int main() {
  // freopen("reverse.in","r",stdin);
  // freopen("reverse.out","w",stdout);
  scanf("%d%d", &n, &m);
  rep(i, 1, m) {
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  dfs(1, 1, 0);
  bw[0] = 0;
  bw[1] = 1;
  if (cnt[0] < cnt[1])
    swap(bw[0], bw[1]); // to ensure num[black]<=num[white]
  rep(i, 1, n) {
    col[i] = bw[col[i]];
    if (col[i])
      s[i] = 1;
    else
      s[i] = -1;
  }

  if (odd) {
    delta = abs(cnt[0] - cnt[1]);
    if (delta % 2) {
      printf("-1\n");
      return 0;
    }
    ans += delta / 2;
    s[trans] += delta / 2;
  } else if (cnt[0] != cnt[1]) {
    printf("-1\n");
    return 0;
  }

  if (!from)
    from = 1;
  dfs2(from, from);

  if (even) {
    p[++pn] = 0; // abs(x) should be counted
    sort(p + 1, p + 1 + pn);
    rep(i, 1, pn) ans += abs(p[i] - p[pn / 2 + 1]);
  }
  printf("%d\n", ans);
  return 0;
}