Problem p04010 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 2e5 + 10;
int tt;
int head[N], to[N * 2], nxt[N * 2], sz[N], fa[N], g[N], k[N], v[N];
inline int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
inline void dfs(int x, int f, int d) {
  sz[x] = g[x] = d, fa[x] = f;
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (y != f)
      dfs(y, x, -d), sz[x] += sz[y];
  }
}
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  freopen("A.in", "r", stdin);
  freopen("A.out", "w", stdout);
  int n, m, A, B;
  long long ans = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int x = gi(), y = gi();
    if (find(x) == find(y))
      A = x, B = y;
    else {
      fa[find(x)] = find(y);
      to[++tt] = y, nxt[tt] = head[x], head[x] = tt;
      to[++tt] = x, nxt[tt] = head[y], head[y] = tt;
    }
  }
  dfs(1, 0, 1);
  if (m == n - 1) {
    if (sz[1])
      return puts("-1"), 0;
  } else if (g[A] == g[B]) {
    if (sz[1] & 1)
      return puts("-1"), 0;
    int t = sz[1] / 2;
    ans += abs(t);
    for (int i = A; i; i = fa[i])
      sz[i] -= t;
    for (int i = B; i; i = fa[i])
      sz[i] -= t;
  } else {
    if (sz[1])
      return puts("-1"), 0;
    for (int i = A; i; i = fa[i])
      ++k[i];
    for (int i = B; i; i = fa[i])
      --k[i];
    int tp = 1;
    for (int i = 1; i <= n; i++) {
      if (k[i] == 1)
        v[++tp] = -sz[i];
      if (k[i] == -1)
        v[++tp] = sz[i];
    }
    sort(v + 1, v + 1 + tp);
    int t = v[(tp + 1) / 2];
    ans += abs(t);
    for (int i = 1; i <= n; i++)
      sz[i] += k[i] * t;
  }
  for (int i = 1; i <= n; i++)
    ans += abs(sz[i]);
  cout << ans;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 2e5 + 10;
int tt;
int head[N], to[N * 2], nxt[N * 2], sz[N], fa[N], g[N], k[N], v[N];
inline int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
inline void dfs(int x, int f, int d) {
  sz[x] = g[x] = d, fa[x] = f;
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (y != f)
      dfs(y, x, -d), sz[x] += sz[y];
  }
}
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  int n, m, A, B;
  long long ans = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int x = gi(), y = gi();
    if (find(x) == find(y))
      A = x, B = y;
    else {
      fa[find(x)] = find(y);
      to[++tt] = y, nxt[tt] = head[x], head[x] = tt;
      to[++tt] = x, nxt[tt] = head[y], head[y] = tt;
    }
  }
  dfs(1, 0, 1);
  if (m == n - 1) {
    if (sz[1])
      return puts("-1"), 0;
  } else if (g[A] == g[B]) {
    if (sz[1] & 1)
      return puts("-1"), 0;
    int t = sz[1] / 2;
    ans += abs(t);
    for (int i = A; i; i = fa[i])
      sz[i] -= t;
    for (int i = B; i; i = fa[i])
      sz[i] -= t;
  } else {
    if (sz[1])
      return puts("-1"), 0;
    for (int i = A; i; i = fa[i])
      ++k[i];
    for (int i = B; i; i = fa[i])
      --k[i];
    int tp = 1;
    for (int i = 1; i <= n; i++) {
      if (k[i] == 1)
        v[++tp] = -sz[i];
      if (k[i] == -1)
        v[++tp] = sz[i];
    }
    sort(v + 1, v + 1 + tp);
    int t = v[(tp + 1) / 2];
    ans += abs(t);
    for (int i = 1; i <= n; i++)
      sz[i] += k[i] * t;
  }
  for (int i = 1; i <= n; i++)
    ans += abs(sz[i]);
  cout << ans;
  return 0;
}