Problem p04010 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
namespace mine {
typedef long long ll;
#define pr pair<int, int>
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll << (x))
#define GG(x)                                                                  \
  if (x) {                                                                     \
    puts("error");                                                             \
    exit(666);                                                                 \
  }
#define fo(i, l, r) for (int i = (l), I = (r); i <= I; i++)
#define fd(i, r, l) for (int i = (r), I = (l); i >= I; i--)
ll qread() {
  ll ans = 0, f = 1;
  char c = getchar();
  while (c < '0' or c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ('0' <= c and c <= '9')
    ans = ans * 10 + c - '0', c = getchar();
  return ans * f;
}
void write(ll num) {
  if (num < 0)
    putchar('-'), num = -num;
  if (num >= 10)
    write(num / 10);
  putchar('0' + num % 10);
}
void write1(ll num) {
  write(num);
  putchar(' ');
}
void write2(ll num) {
  write(num);
  putchar('\n');
}
template <typename T> void chmax(T &x, const T y) { x = (x > y ? x : y); }
template <typename T> void chmin(T &x, const T y) { x = (x < y ? x : y); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
inline int mm(const int x) { return x >= MOD ? x - MOD : x; }
template <typename T> inline void add(T &x, const T y) { x = mm(x + y); }
inline ll qpower(ll x, ll e) {
  ll ans = 1;
  GG(e < 0);
  while (e) {
    if (e & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    e >>= 1;
  }
  return ans;
}
ll invm(ll x) { return qpower(x, MOD - 2); }
const int N = 1e3 + 10;

vc<int> to[N];
int dep[N];
int ln, S, T;
int use[N], ct[N], sum;
void calc(int x, int fa) {
  dep[x] = dep[fa] + 1;
  use[x] = (dep[x] & 1 ? 1 : -1);
  sum += use[x];
  fo(t, 0, sz(to[x]) - 1) {
    int y = to[x][t];
    if (y == fa)
      continue;
    if (dep[y]) {
      if (!ln)
        ln = dep[x] - dep[y] + 1, S = x, T = y;
    } else
      calc(y, x);
  }
}
int ans;
int pp[N], cnt;
void solve(int x, int fa) {
  fo(t, 0, sz(to[x]) - 1) {
    int y = to[x][t];
    if (y == fa or (x == S and y == T) or (x == T and y == S))
      continue;
    solve(y, x);
    use[x] += use[y];
    ct[x] += ct[y];
  }
  if (!ct[x])
    ans += abs(use[x]);
  else
    pp[++cnt] = use[x] * ct[x]; // 反转绝对值
}
void gg() {
  puts("-1");
  exit(0);
}
void main() {
  int n = qread(), m = qread();
  fo(i, 1, m) {
    int x = qread(), y = qread();
    to[x].PB(y);
    to[y].PB(x);
  }
  calc(1, 0);
  if (m == n - 1) {
    if (sum != 0)
      gg();
  } else if (ln & 1) {
    if (sum & 1)
      gg();
    ans += abs(sum / 2);
    use[S] -= sum / 2, use[T] -= sum / 2;
  } else {
    if (sum != 0)
      gg();
    ct[S]++, ct[T]--;
  }
  solve(1, 0);
  pp[++cnt] = 0;
  sort(pp + 1, pp + cnt + 1);
  fo(i, 1, cnt) ans += abs(pp[i] - pp[(cnt + 1) / 2]);
  write(ans);
}
}; // namespace mine
signed main() {
  srand(time(0));
  mine::main();
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
namespace mine {
typedef long long ll;
#define pr pair<int, int>
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll << (x))
#define GG(x)                                                                  \
  if (x) {                                                                     \
    puts("error");                                                             \
    exit(666);                                                                 \
  }
#define fo(i, l, r) for (int i = (l), I = (r); i <= I; i++)
#define fd(i, r, l) for (int i = (r), I = (l); i >= I; i--)
ll qread() {
  ll ans = 0, f = 1;
  char c = getchar();
  while (c < '0' or c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ('0' <= c and c <= '9')
    ans = ans * 10 + c - '0', c = getchar();
  return ans * f;
}
void write(ll num) {
  if (num < 0)
    putchar('-'), num = -num;
  if (num >= 10)
    write(num / 10);
  putchar('0' + num % 10);
}
void write1(ll num) {
  write(num);
  putchar(' ');
}
void write2(ll num) {
  write(num);
  putchar('\n');
}
template <typename T> void chmax(T &x, const T y) { x = (x > y ? x : y); }
template <typename T> void chmin(T &x, const T y) { x = (x < y ? x : y); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
inline int mm(const int x) { return x >= MOD ? x - MOD : x; }
template <typename T> inline void add(T &x, const T y) { x = mm(x + y); }
inline ll qpower(ll x, ll e) {
  ll ans = 1;
  GG(e < 0);
  while (e) {
    if (e & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    e >>= 1;
  }
  return ans;
}
ll invm(ll x) { return qpower(x, MOD - 2); }
const int N = 1e6 + 10;

vc<int> to[N];
int dep[N];
int ln, S, T;
int use[N], ct[N], sum;
void calc(int x, int fa) {
  dep[x] = dep[fa] + 1;
  use[x] = (dep[x] & 1 ? 1 : -1);
  sum += use[x];
  fo(t, 0, sz(to[x]) - 1) {
    int y = to[x][t];
    if (y == fa)
      continue;
    if (dep[y]) {
      if (!ln)
        ln = dep[x] - dep[y] + 1, S = x, T = y;
    } else
      calc(y, x);
  }
}
int ans;
int pp[N], cnt;
void solve(int x, int fa) {
  fo(t, 0, sz(to[x]) - 1) {
    int y = to[x][t];
    if (y == fa or (x == S and y == T) or (x == T and y == S))
      continue;
    solve(y, x);
    use[x] += use[y];
    ct[x] += ct[y];
  }
  if (!ct[x])
    ans += abs(use[x]);
  else
    pp[++cnt] = use[x] * ct[x]; // 反转绝对值
}
void gg() {
  puts("-1");
  exit(0);
}
void main() {
  int n = qread(), m = qread();
  fo(i, 1, m) {
    int x = qread(), y = qread();
    to[x].PB(y);
    to[y].PB(x);
  }
  calc(1, 0);
  if (m == n - 1) {
    if (sum != 0)
      gg();
  } else if (ln & 1) {
    if (sum & 1)
      gg();
    ans += abs(sum / 2);
    use[S] -= sum / 2, use[T] -= sum / 2;
  } else {
    if (sum != 0)
      gg();
    ct[S]++, ct[T]--;
  }
  solve(1, 0);
  pp[++cnt] = 0;
  sort(pp + 1, pp + cnt + 1);
  fo(i, 1, cnt) ans += abs(pp[i] - pp[(cnt + 1) / 2]);
  write(ans);
}
}; // namespace mine
signed main() {
  srand(time(0));
  mine::main();
}