Problem p04010 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define Abs(_A) (_A > 0 ? _A : -(_A))

const int MaxN = 100010;
namespace Steaunk {
int n, m, ans, s[MaxN], sta[MaxN], tot, a[MaxN];
int Point[MaxN], Next[MaxN << 1], To[MaxN << 1], q = 1;
bool vis[MaxN];
void Add(int u, int v) {
  Next[++q] = Point[u];
  Point[u] = q;
  To[q] = v;
  Next[++q] = Point[v];
  Point[v] = q;
  To[q] = u;
}
void dfs(int u, int from) {
  s[u] = 0;
  for (int j = Point[u]; j; j = Next[j])
    if (To[j] != from && !vis[To[j]])
      dfs(To[j], u);
  if (u != from) {
    s[from] += 1 - s[u];
    ans += Abs(1 - s[u]);
    s[u] = 1;
  }
}
int dfs1(int u, int from) {
  vis[u] = 1; // printf("(%d)\n", u);
  for (int j = Point[u]; j; j = Next[j])
    if (j != (from ^ 1)) {
      if (vis[To[j]]) {
        vis[sta[tot++] = u] = 0;
        return To[j];
      }
      int t;
      if (!vis[To[j]] && (t = dfs1(To[j], j))) {
        vis[u] = 0;
        // printf("<%d, %d>\n", t, u);
        if (~t)
          return (sta[tot++] = u) == t ? -1 : t;
        else
          return t;
      }
    }
  vis[u] = 0;
  return 0;
}
void main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i)
    Point[i] = 0;
  q = 1;
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    Add(u, v);
  }
  if (n & 1) {
    puts("-1");
    return;
  }
  ans = 0;
  if (m == n - 1) {
    dfs(1, 1);
    if (s[1] != 1)
      puts("-1");
    else
      printf("%d\n", ans);
  } else {
    tot = 0;
    dfs1(1, 0);
    for (int *i = sta; i < sta + tot; ++i)
      vis[*i] = 1;
    for (int *i = sta; i < sta + tot; ++i)
      dfs(*i, *i);
    for (int *i = sta; i < sta + tot; ++i)
      vis[*i] = 0;
    sta[tot] = sta[0];
    //	for(int i = 1; i <= n; ++i) printf("%d ", s[i]); puts("");
    //	for(int *i = sta; i < sta + tot; ++i) printf("[%d] ", *i); puts("");
    for (int i = 1; i < tot; ++i)
      a[i] = 1 - s[sta[i - 1]] - a[i - 1];
    a[tot] = 1 - s[sta[tot - 1]];
    //	for(int i = 0; i <= tot; ++i) printf("%d ", a[i]); puts("");
    if (tot & 1) {
      if (a[tot] + a[tot - 1] & 1)
        puts("-1");
      else {
        int t = (a[tot] - a[tot - 1]) / 2;
        //	printf("%d %d\n", ans, t);
        //	for(int i = 0; i < tot; ++i) printf("%d ", a[i]); puts("");
        for (int i = 0; i < tot; ++i)
          if (i & 1)
            ans += Abs(a[i] - t);
          else
            ans += Abs(a[i] + t);
        printf("%d\n", ans);
      }
    } else {
      if (a[tot] != a[tot - 1])
        puts("-1");
      else // median
      {
        // for(int *i = sta; i < sta + tot; ++i) printf("%d ", s[*i]); puts("");
        for (int i = 0; i < tot; ++i)
          if (i & 1)
            s[i] = a[i];
          else
            s[i] = -a[i];
        // for(int i = 0; i < tot; ++i) printf("%d ", a[i]); puts("");
        // for(int i = 0; i < tot; ++i) printf("%d ", s[i]); puts("");
        std::nth_element(s, s + (tot >> 1), s + tot);
        int t = s[tot >> 1];
        // printf("<%d>\n", t);
        for (int i = 0; i < tot; ++i)
          if (i & 1)
            ans += Abs(a[i] - t);
          else
            ans += Abs(a[i] + t);
        printf("%d\n", ans);
      }
    }
    // for(int i = 1; i <= tot; ++i) printf("%d ", a[i]); puts("\n");
  }
}
} // namespace Steaunk
int main() {
  int T;
  scanf("%d", &T);
  while (T--)
    Steaunk::main();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define Abs(_A) (_A > 0 ? _A : -(_A))

const int MaxN = 100010;
namespace Steaunk {
int n, m, ans, s[MaxN], sta[MaxN], tot, a[MaxN];
int Point[MaxN], Next[MaxN << 1], To[MaxN << 1], q = 1;
bool vis[MaxN];
void Add(int u, int v) {
  Next[++q] = Point[u];
  Point[u] = q;
  To[q] = v;
  Next[++q] = Point[v];
  Point[v] = q;
  To[q] = u;
}
void dfs(int u, int from) {
  s[u] = 0;
  for (int j = Point[u]; j; j = Next[j])
    if (To[j] != from && !vis[To[j]])
      dfs(To[j], u);
  if (u != from) {
    s[from] += 1 - s[u];
    ans += Abs(1 - s[u]);
    s[u] = 1;
  }
}
int dfs1(int u, int from) {
  vis[u] = 1; // printf("(%d)\n", u);
  for (int j = Point[u]; j; j = Next[j])
    if (j != (from ^ 1)) {
      if (vis[To[j]]) {
        vis[sta[tot++] = u] = 0;
        return To[j];
      }
      int t;
      if (!vis[To[j]] && (t = dfs1(To[j], j))) {
        vis[u] = 0;
        // printf("<%d, %d>\n", t, u);
        if (~t)
          return (sta[tot++] = u) == t ? -1 : t;
        else
          return t;
      }
    }
  vis[u] = 0;
  return 0;
}
void main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i)
    Point[i] = 0;
  q = 1;
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    Add(u, v);
  }
  if (n & 1) {
    puts("-1");
    return;
  }
  ans = 0;
  if (m == n - 1) {
    dfs(1, 1);
    if (s[1] != 1)
      puts("-1");
    else
      printf("%d\n", ans);
  } else {
    tot = 0;
    dfs1(1, 0);
    for (int *i = sta; i < sta + tot; ++i)
      vis[*i] = 1;
    for (int *i = sta; i < sta + tot; ++i)
      dfs(*i, *i);
    for (int *i = sta; i < sta + tot; ++i)
      vis[*i] = 0;
    sta[tot] = sta[0];
    //	for(int i = 1; i <= n; ++i) printf("%d ", s[i]); puts("");
    //	for(int *i = sta; i < sta + tot; ++i) printf("[%d] ", *i); puts("");
    for (int i = 1; i < tot; ++i)
      a[i] = 1 - s[sta[i - 1]] - a[i - 1];
    a[tot] = 1 - s[sta[tot - 1]];
    //	for(int i = 0; i <= tot; ++i) printf("%d ", a[i]); puts("");
    if (tot & 1) {
      if (a[tot] + a[tot - 1] & 1)
        puts("-1");
      else {
        int t = (a[tot] - a[tot - 1]) / 2;
        //	printf("%d %d\n", ans, t);
        //	for(int i = 0; i < tot; ++i) printf("%d ", a[i]); puts("");
        for (int i = 0; i < tot; ++i)
          if (i & 1)
            ans += Abs(a[i] - t);
          else
            ans += Abs(a[i] + t);
        printf("%d\n", ans);
      }
    } else {
      if (a[tot] != a[tot - 1])
        puts("-1");
      else // median
      {
        // for(int *i = sta; i < sta + tot; ++i) printf("%d ", s[*i]); puts("");
        for (int i = 0; i < tot; ++i)
          if (i & 1)
            s[i] = a[i];
          else
            s[i] = -a[i];
        // for(int i = 0; i < tot; ++i) printf("%d ", a[i]); puts("");
        // for(int i = 0; i < tot; ++i) printf("%d ", s[i]); puts("");
        std::nth_element(s, s + (tot >> 1), s + tot);
        int t = s[tot >> 1];
        // printf("<%d>\n", t);
        for (int i = 0; i < tot; ++i)
          if (i & 1)
            ans += Abs(a[i] - t);
          else
            ans += Abs(a[i] + t);
        printf("%d\n", ans);
      }
    }
    // for(int i = 1; i <= tot; ++i) printf("%d ", a[i]); puts("\n");
  }
}
} // namespace Steaunk
int main() {
  int T;
  // scanf("%d", &T);
  // while(T--)
  Steaunk::main();
  return 0;
}