Problem p04010 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define gt getchar()
#define ll long long
#define File(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
typedef std::pair<int, int> P;
#define mk std::make_pair
#define fr first
#define sc second
inline int in() {
  int k = 0;
  char ch = gt;
  bool p = 1;
  while (ch < '-')
    ch = gt;
  if (ch == '-')
    ch = gt, p = 0;
  while (ch > '-')
    k = k * 10 + ch - '0', ch = gt;
  return p ? k : -k;
}
const int N = 2e5 + 5;
int fa[N], head[N], to[N << 1], nxt[N << 1], cnt, n, m;
inline void add(int u, int v) {
  to[++cnt] = v, nxt[cnt] = head[u], head[u] = cnt;
  to[++cnt] = u, nxt[cnt] = head[v], head[v] = cnt;
}
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline int abs(int x) { return x < 0 ? -x : x; }
namespace w1 {
int f[N];
ll ans;
void dfs(int u, int pa = 0, int d = 1) {
  f[u] = d & 1 ? 1 : -1;
  for (int i = head[u]; i; i = nxt[i])
    if (to[i] != pa)
      dfs(to[i], u, d + 1), f[u] += f[to[i]];
  if (u != 1)
    ans += abs(f[u]);
}
int work() {
  for (int i = 2; i <= n; ++i)
    add(in(), in());
  ans = 0;
  dfs(1);
  if (f[1])
    puts("-1");
  else
    printf("%lld\n", ans);
  return 0;
}
} // namespace w1
int f[N], dep[N], st[N], tot;
void dfs(int u, int pa = 0, int d = 1) {
  f[u] = d & 1 ? 1 : -1;
  dep[u] = d, fa[u] = pa;
  for (int i = head[u]; i; i = nxt[i])
    if (to[i] != pa)
      dfs(to[i], u, d + 1), f[u] += f[to[i]];
}
int main() {
  freopen("1.in", "r", stdin);
  n = in(), m = in();
  if (n & 1)
    return puts("-1"), 0;
  if (m == n - 1)
    return w1::work();
  int Eu, Ev;
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1; i <= n; ++i) {
    int u = in(), v = in();
    if (find(u) == find(v)) {
      Eu = u, Ev = v;
      continue;
    }
    fa[find(u)] = find(v);
    add(u, v);
  }
  dfs(Eu);
  if (dep[Ev] & 1) {
    if (f[Eu] & 1)
      return puts("-1"), 0;
    ll ans = 0;
    int res = f[Eu] >> 1, u = Ev;
    while (u)
      f[u] -= res, u = fa[u];
    for (int i = 1; i <= n; ++i)
      ans += abs(f[i]);
    printf("%lld\n", ans);
  } else {
    if (f[Eu])
      return puts("-1"), 0;
    int u = Ev;
    ll ans = 0;
    while (u != Eu)
      st[++tot] = f[u], u = fa[u];
    std::sort(st + 1, st + tot + 1);
    int x = st[tot + 1 >> 1];
    u = Ev;
    while (u)
      f[u] -= x, u = fa[u];
    for (int i = 1; i <= n; ++i)
      ans += abs(f[i]);
    printf("%lld\n", ans);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define gt getchar()
#define ll long long
#define File(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
typedef std::pair<int, int> P;
#define mk std::make_pair
#define fr first
#define sc second
inline int in() {
  int k = 0;
  char ch = gt;
  bool p = 1;
  while (ch < '-')
    ch = gt;
  if (ch == '-')
    ch = gt, p = 0;
  while (ch > '-')
    k = k * 10 + ch - '0', ch = gt;
  return p ? k : -k;
}
const int N = 2e5 + 5;
int fa[N], head[N], to[N << 1], nxt[N << 1], cnt, n, m;
inline void add(int u, int v) {
  to[++cnt] = v, nxt[cnt] = head[u], head[u] = cnt;
  to[++cnt] = u, nxt[cnt] = head[v], head[v] = cnt;
}
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline int abs(int x) { return x < 0 ? -x : x; }
namespace w1 {
int f[N];
ll ans;
void dfs(int u, int pa = 0, int d = 1) {
  f[u] = d & 1 ? 1 : -1;
  for (int i = head[u]; i; i = nxt[i])
    if (to[i] != pa)
      dfs(to[i], u, d + 1), f[u] += f[to[i]];
  if (u != 1)
    ans += abs(f[u]);
}
int work() {
  for (int i = 2; i <= n; ++i)
    add(in(), in());
  ans = 0;
  dfs(1);
  if (f[1])
    puts("-1");
  else
    printf("%lld\n", ans);
  return 0;
}
} // namespace w1
int f[N], dep[N], st[N], tot;
void dfs(int u, int pa = 0, int d = 1) {
  f[u] = d & 1 ? 1 : -1;
  dep[u] = d, fa[u] = pa;
  for (int i = head[u]; i; i = nxt[i])
    if (to[i] != pa)
      dfs(to[i], u, d + 1), f[u] += f[to[i]];
}
int main() {
  n = in(), m = in();
  if (n & 1)
    return puts("-1"), 0;
  if (m == n - 1)
    return w1::work();
  int Eu, Ev;
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1; i <= n; ++i) {
    int u = in(), v = in();
    if (find(u) == find(v)) {
      Eu = u, Ev = v;
      continue;
    }
    fa[find(u)] = find(v);
    add(u, v);
  }
  dfs(Eu);
  if (dep[Ev] & 1) {
    if (f[Eu] & 1)
      return puts("-1"), 0;
    ll ans = 0;
    int res = f[Eu] >> 1, u = Ev;
    while (u)
      f[u] -= res, u = fa[u];
    for (int i = 1; i <= n; ++i)
      ans += abs(f[i]);
    printf("%lld\n", ans);
  } else {
    if (f[Eu])
      return puts("-1"), 0;
    int u = Ev;
    ll ans = 0;
    while (u != Eu)
      st[++tot] = f[u], u = fa[u];
    std::sort(st + 1, st + tot + 1);
    int x = st[tot + 1 >> 1];
    u = Ev;
    while (u)
      f[u] -= x, u = fa[u];
    for (int i = 1; i <= n; ++i)
      ans += abs(f[i]);
    printf("%lld\n", ans);
  }
  return 0;
}