Problem p04010 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define RG register
#define IL inline
#define int long long
#define DB double
using namespace std;

IL int gi() {
  char ch = getchar();
  int x = 0, q = 0;
  while (ch < '0' || ch > '9')
    q = ch == '-' ? 1 : q, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return q ? -x : x;
}

const int N = 2e5 + 10;

int n, m, ans, tot, cnt, k[N], s[N], fa[N], flag[N], head[N], size[N];

struct Edge {
  int next, to;
} e[N << 1];
IL void make(int a, int b) {
  e[++tot] = (Edge){head[a], b}, head[a] = tot;
  e[++tot] = (Edge){head[b], a}, head[b] = tot;
}

int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }

void dfs(int x, int fx, int fl) {
  RG int i, y;
  fa[x] = fx, size[x] = flag[x] = fl;
  for (i = head[x]; i; i = e[i].next)
    if ((y = e[i].to) != fx)
      dfs(y, x, -fl), size[x] += size[y];
}

signed main() {
  freopen("A.in", "r", stdin);
  freopen("A.out", "w", stdout);
  RG int i, x, y, fx, fy, Fr, To, now;
  n = gi(), m = gi();
  for (i = 1; i <= n; ++i)
    fa[i] = i;
  for (i = 1; i <= m; ++i) {
    x = gi(), y = gi(), fx = getfa(x), fy = getfa(y);
    if (fx == fy)
      Fr = x, To = y;
    else
      fa[fx] = fy, make(x, y);
  }
  dfs(1, 0, 1);
  if (m == n - 1) {
    if (size[1])
      return puts("-1"), 0;
  } else if (flag[Fr] == flag[To]) {
    if (size[1] & 1)
      return puts("-1"), 0;
    now = size[1] / 2, ans += abs(now);
    for (i = Fr; i; i = fa[i])
      size[i] -= now;
    for (i = To; i; i = fa[i])
      size[i] -= now;
  } else {
    /*if (size[1]) return puts("-1"),0;
    s[cnt=1]=0;
    for (i=Fr;fa[i];i=fa[i]) s[++cnt]=-size[i];
    for (i=To;fa[i];i=fa[i]) s[++cnt]=size[i];
    sort(s+1,s+cnt+1);
    now=s[cnt/2],ans+=abs(now);
    for (i=Fr;fa[i];i=fa[i]) size[i]+=now;
    for (i=To;fa[i];i=fa[i]) size[i]-=now;*/
    if (size[1])
      return puts("-1"), 0;
    for (i = Fr; i; i = fa[i])
      ++k[i];
    for (i = To; i; i = fa[i])
      --k[i];
    int tp = 1;
    for (i = 1; i <= n; i++) {
      if (k[i] == 1)
        s[++tp] = -size[i];
      if (k[i] == -1)
        s[++tp] = size[i];
    }
    sort(s + 1, s + 1 + tp);
    int t = s[(tp + 1) / 2];
    ans += abs(t);
    for (i = 1; i <= n; i++)
      size[i] += k[i] * t;
  }
  for (i = 1; i <= n; ++i)
    ans += abs(size[i]);
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define RG register
#define IL inline
#define int long long
#define DB double
using namespace std;

IL int gi() {
  char ch = getchar();
  int x = 0, q = 0;
  while (ch < '0' || ch > '9')
    q = ch == '-' ? 1 : q, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return q ? -x : x;
}

const int N = 2e5 + 10;

int n, m, ans, tot, cnt, k[N], s[N], fa[N], flag[N], head[N], size[N];

struct Edge {
  int next, to;
} e[N << 1];
IL void make(int a, int b) {
  e[++tot] = (Edge){head[a], b}, head[a] = tot;
  e[++tot] = (Edge){head[b], a}, head[b] = tot;
}

int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }

void dfs(int x, int fx, int fl) {
  RG int i, y;
  fa[x] = fx, size[x] = flag[x] = fl;
  for (i = head[x]; i; i = e[i].next)
    if ((y = e[i].to) != fx)
      dfs(y, x, -fl), size[x] += size[y];
}

signed main() {
  RG int i, x, y, fx, fy, Fr, To, now;
  n = gi(), m = gi();
  for (i = 1; i <= n; ++i)
    fa[i] = i;
  for (i = 1; i <= m; ++i) {
    x = gi(), y = gi(), fx = getfa(x), fy = getfa(y);
    if (fx == fy)
      Fr = x, To = y;
    else
      fa[fx] = fy, make(x, y);
  }
  dfs(1, 0, 1);
  if (m == n - 1) {
    if (size[1])
      return puts("-1"), 0;
  } else if (flag[Fr] == flag[To]) {
    if (size[1] & 1)
      return puts("-1"), 0;
    now = size[1] / 2, ans += abs(now);
    for (i = Fr; i; i = fa[i])
      size[i] -= now;
    for (i = To; i; i = fa[i])
      size[i] -= now;
  } else {
    /*if (size[1]) return puts("-1"),0;
    s[cnt=1]=0;
    for (i=Fr;fa[i];i=fa[i]) s[++cnt]=-size[i];
    for (i=To;fa[i];i=fa[i]) s[++cnt]=size[i];
    sort(s+1,s+cnt+1);
    now=s[cnt/2],ans+=abs(now);
    for (i=Fr;fa[i];i=fa[i]) size[i]+=now;
    for (i=To;fa[i];i=fa[i]) size[i]-=now;*/
    if (size[1])
      return puts("-1"), 0;
    for (i = Fr; i; i = fa[i])
      ++k[i];
    for (i = To; i; i = fa[i])
      --k[i];
    int tp = 1;
    for (i = 1; i <= n; i++) {
      if (k[i] == 1)
        s[++tp] = -size[i];
      if (k[i] == -1)
        s[++tp] = size[i];
    }
    sort(s + 1, s + 1 + tp);
    int t = s[(tp + 1) / 2];
    ans += abs(t);
    for (i = 1; i <= n; i++)
      size[i] += k[i] * t;
  }
  for (i = 1; i <= n; ++i)
    ans += abs(size[i]);
  printf("%lld\n", ans);
  return 0;
}