Problem p00372 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, n) FOR(i, 0, n)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
typedef vector<int> vi;
#define PB push_back
#define MP make_pair
#define YES() printf("yes\\n"), exit(0);
#define NO() printf("no\\n"), exit(0);

#define N 112345
int uf_par[N], uf_siz[N];
int grp(int a) {
  if (uf_par[a] == a)
    return a;
  else
    return uf_par[a] = grp(uf_par[a]);
}
void merg(int a, int b) {
  a = grp(a);
  b = grp(b);
  if (uf_siz[a] < uf_siz[b])
    swap(a, b);
  uf_par[b] = a;
  uf_siz[a] += uf_siz[b];
}

int n, m;
int d[N];
vi path[N];
int dfs(int a, int p) {
  d[a] = 1;
  int cnt = 0;
  REP(j, path[a].size()) {
    int b = path[a][j];
    if (b == p)
      continue;
    if (d[b] == -1)
      continue;
    if (d[b]) {
      // printf("%d=>%d\\n",a,b);
      d[b]++;
      cnt++;
    } else {
      // printf("%d->%d\\n",a,b);
      cnt += dfs(b, a);
    }
  }
  cnt -= d[a] - 1;
  if (cnt)
    merg(a, p);
  d[a] = -1;
  return cnt;
}
vi stick[N];
int memo[N][2];
int DP(int, int, bool);
int dp(int a, int p, bool av) {
  if (~memo[a][av])
    return memo[a][av];
  else
    return memo[a][av] = DP(a, p, av);
}
int DP(int a, int p, bool av) {
  int x = 0, y = INT_MIN;
  REP(i, stick[a].size()) {
    int b = stick[a][i];
    if (b == p)
      continue;
    x += dp(b, a, 1);
  }
  if (av) {
    y = uf_siz[a];
    REP(i, stick[a].size()) {
      int b = stick[a][i];
      if (b == p)
        continue;
      y += dp(b, a, 0);
    }
  }
  // printf("dp(%d, %d)=max(%d, %d)\\n", a, av, x, y);
  return max(x, y);
}

int main() {
  scanf("%d%d", &n, &m);
  REP(i, m) {
    int s, t;
    scanf("%d%d", &s, &t);
    path[s].PB(t);
    path[t].PB(s);
  }

  fill(d, d + N, 0);
  // fill(vis, vis+N, false);
  REP(i, N) uf_par[i] = i;
  fill(uf_siz, uf_siz + N, 1);
  REP(i, n) if (!d[i]) dfs(i, -1);

  // REP(i, n) printf("%2d ", i);  printf("\\n");
  // REP(i, n) printf("%2d ", grp(i));printf("\\n");
  // REP(i, n) printf("%2d ", uf_siz[i]);printf("\\n");

  REP(i, n) {
    REP(j, path[i].size()) {
      int p = grp(i), q = grp(path[i][j]);
      if (i < path[i][j] && p != q) {
        stick[p].PB(q);
        stick[q].PB(p);
        // printf("%d-%d\\n", p, q);
      }
    }
  }
  REP(i, n) memo[i][0] = memo[i][1] = -1;
  int ans = 0;
  REP(i, n) {
    int j = grp(i);
    if (memo[j][1] == -1) {
      // printf("%d\\n", ans);
      ans += dp(j, -1, 1);
    }
  }
  printf("%d\\n", ans);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, n) FOR(i, 0, n)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
typedef vector<int> vi;
#define PB push_back
#define MP make_pair
#define YES() printf("yes\\n"), exit(0);
#define NO() printf("no\\n"), exit(0);

#define N 112345
int uf_par[N], uf_siz[N];
int grp(int a) {
  if (uf_par[a] == a)
    return a;
  else
    return uf_par[a] = grp(uf_par[a]);
}
void merg(int a, int b) {
  a = grp(a);
  b = grp(b);
  if (uf_siz[a] < uf_siz[b])
    swap(a, b);
  uf_par[b] = a;
  uf_siz[a] += uf_siz[b];
}

int n, m;
int d[N];
vi path[N];
int dfs(int a, int p) {
  d[a] = 1;
  int cnt = 0;
  REP(j, path[a].size()) {
    int b = path[a][j];
    if (b == p)
      continue;
    if (d[b] == -1)
      continue;
    if (d[b]) {
      // printf("%d=>%d\\n",a,b);
      d[b]++;
      cnt++;
    } else {
      // printf("%d->%d\\n",a,b);
      cnt += dfs(b, a);
    }
  }
  cnt -= d[a] - 1;
  if (cnt)
    merg(a, p);
  d[a] = -1;
  return cnt;
}
vi stick[N];
int memo[N][2];
int DP(int, int, bool);
int dp(int a, int p, bool av) {
  if (~memo[a][av])
    return memo[a][av];
  else
    return memo[a][av] = DP(a, p, av);
}
int DP(int a, int p, bool av) {
  int x = 0, y = INT_MIN;
  REP(i, stick[a].size()) {
    int b = stick[a][i];
    if (b == p)
      continue;
    x += dp(b, a, 1);
  }
  if (av) {
    y = uf_siz[a];
    REP(i, stick[a].size()) {
      int b = stick[a][i];
      if (b == p)
        continue;
      y += dp(b, a, 0);
    }
  }
  // printf("dp(%d, %d)=max(%d, %d)\\n", a, av, x, y);
  return max(x, y);
}

int main() {
  scanf("%d%d", &n, &m);
  REP(i, m) {
    int s, t;
    scanf("%d%d", &s, &t);
    path[s].PB(t);
    path[t].PB(s);
  }
  if (m % 3 != 1)
    printf("-1\\n");

  fill(d, d + N, 0);
  // fill(vis, vis+N, false);
  REP(i, N) uf_par[i] = i;
  fill(uf_siz, uf_siz + N, 1);
  dfs(0, -1);

  // REP(i, n) printf("%2d ", i);  printf("\\n");
  // REP(i, n) printf("%2d ", grp(i));printf("\\n");
  // REP(i, n) printf("%2d ", uf_siz[i]);printf("\\n");

  int r = -1;
  REP(i, n) {
    REP(j, path[i].size()) {
      int p = grp(i), q = grp(path[i][j]);
      if (i < path[i][j] && p != q) {
        stick[p].PB(q);
        stick[q].PB(p);
        // printf("%d-%d\\n", p, q);
      }
      r = p;
    }
  }
  REP(i, n) memo[i][0] = memo[i][1] = -1;
  printf("%d\\n", dp(r, -1, 1));
}