Problem p03656 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define fi first
#define sc second
#define cmin(a, b) (a > b ? a = b : a)
#define cmax(a, b) (a < b ? a = b : a)
#define mem(a, k) memset(a, k, sizeof(a))
#define lop(i, s, t) for (int i = s; i < (t); ++i)
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define fore(i, v)                                                             \
  for (int i = g[v], d = es[i].d; i; i = es[i].nxt, d = es[i].d)
using namespace std;

#define Pr(f, ...) // printf(f,##__VA_ARGS__),fflush(stdout)

typedef long long ll;

const int N = 1e5 + 50, oo = 0x3f3f3f3f;

int n, fail, ans[N], sz1[N], sz2[N], qwq, rt1, rt2, ptr[N];
vector<int> g1[N], g2[N], g3[N];
typedef vector<int> G[N];
void add(int s, int d, G g) { g[s].pb(d), g[d].pb(s); }

void dfs1(int v, int p) {
  sz1[v] = 1;
  for (int d : g1[v])
    if (d != p)
      dfs1(d, v), sz1[v] ^= 1;
  Pr("sz1[%d]=%d\n", v, sz1[v]);
}

void dfs2(int v, int p) {
  sz2[v] = 1;
  for (int d : g2[v])
    if (d != p)
      dfs2(d, v), sz2[v] ^= 1;
  Pr("sz2[%d]=%d\n", v, sz2[v]);
}

typedef pair<int, int> P;
set<P> del;
bool chk(int x, int y) {
  if (x > y)
    swap(x, y);
  P t = mp(x, y);
  if (del.count(t))
    return false;
  else {
    del.insert(t);
    return true;
  }
}

void dfs3(int v) {
  for (; ptr[v] < g3[v].size(); ++ptr[v]) {
    int d = g3[v][ptr[v]];
    if (chk(v, d)) {
      Pr("from %d to %d\n", v, d);
      if (v != qwq && d != qwq) {
        if (v <= n && d > n)
          ans[v] = 1;
        if (v > n && d <= n)
          ans[v - n] = -1;
      }
      dfs3(d);
    }
  }
}

int main(int argc, char *argv[]) {
#ifdef CURIOUSCAT
  freopen("dat.in", "r", stdin);
  // freopen("my.out","w",stdout);
#endif
  scanf("%d", &n);
  qwq = n + n + 1;
  rep(i, 1, n) {
    int p;
    scanf("%d", &p);
    if (p != -1)
      add(p, i, g1), add(p, i, g3);
    else
      rt1 = i, add(i, qwq, g3);
    ;
  }
  rep(i, 1, n) {
    int p;
    scanf("%d", &p);
    if (p != -1)
      add(p, i, g2), add(n + p, n + i, g3);
    else
      rt2 = i, add(n + i, qwq, g3);
  }
  dfs1(rt1, 0);
  dfs2(rt2, 0);
  rep(i, 1, n) {
    if (sz1[i] % 2 != sz2[i] % 2)
      fail = true;
    else if (sz1[i] % 2 == 1) {
      add(i, n + i, g3);
      Pr("Link %d,%d\n", i, n + i);
    }
  }
  dfs3(qwq);
  if (fail)
    puts("IMPOSSIBLE");
  else {
    puts("POSSIBLE");
    rep(i, 1, n) printf("%d%c", ans[i], " \n"[i == n]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define fi first
#define sc second
#define cmin(a, b) (a > b ? a = b : a)
#define cmax(a, b) (a < b ? a = b : a)
#define mem(a, k) memset(a, k, sizeof(a))
#define lop(i, s, t) for (int i = s; i < (t); ++i)
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define fore(i, v)                                                             \
  for (int i = g[v], d = es[i].d; i; i = es[i].nxt, d = es[i].d)
using namespace std;

#define Pr(f, ...) // printf(f,##__VA_ARGS__),fflush(stdout)

typedef long long ll;

const int N = 2e5 + 50, oo = 0x3f3f3f3f;

int n, fail, ans[N], sz1[N], sz2[N], qwq, rt1, rt2, ptr[N];
vector<int> g1[N], g2[N], g3[N];
typedef vector<int> G[N];
void add(int s, int d, G g) { g[s].pb(d), g[d].pb(s); }

void dfs1(int v, int p) {
  sz1[v] = 1;
  for (int d : g1[v])
    if (d != p)
      dfs1(d, v), sz1[v] ^= 1;
  Pr("sz1[%d]=%d\n", v, sz1[v]);
}

void dfs2(int v, int p) {
  sz2[v] = 1;
  for (int d : g2[v])
    if (d != p)
      dfs2(d, v), sz2[v] ^= 1;
  Pr("sz2[%d]=%d\n", v, sz2[v]);
}

typedef pair<int, int> P;
set<P> del;
bool chk(int x, int y) {
  if (x > y)
    swap(x, y);
  P t = mp(x, y);
  if (del.count(t))
    return false;
  else {
    del.insert(t);
    return true;
  }
}

void dfs3(int v) {
  for (; ptr[v] < g3[v].size(); ++ptr[v]) {
    int d = g3[v][ptr[v]];
    if (chk(v, d)) {
      Pr("from %d to %d\n", v, d);
      if (v != qwq && d != qwq) {
        if (v <= n && d > n)
          ans[v] = 1;
        if (v > n && d <= n)
          ans[v - n] = -1;
      }
      dfs3(d);
    }
  }
}

int main(int argc, char *argv[]) {
#ifdef CURIOUSCAT
  freopen("dat.in", "r", stdin);
  // freopen("my.out","w",stdout);
#endif
  scanf("%d", &n);
  qwq = n + n + 1;
  rep(i, 1, n) {
    int p;
    scanf("%d", &p);
    if (p != -1)
      add(p, i, g1), add(p, i, g3);
    else
      rt1 = i, add(i, qwq, g3);
    ;
  }
  rep(i, 1, n) {
    int p;
    scanf("%d", &p);
    if (p != -1)
      add(p, i, g2), add(n + p, n + i, g3);
    else
      rt2 = i, add(n + i, qwq, g3);
  }
  dfs1(rt1, 0);
  dfs2(rt2, 0);
  rep(i, 1, n) {
    if (sz1[i] % 2 != sz2[i] % 2)
      fail = true;
    else if (sz1[i] % 2 == 1) {
      add(i, n + i, g3);
      Pr("Link %d,%d\n", i, n + i);
    }
  }
  dfs3(qwq);
  if (fail)
    puts("IMPOSSIBLE");
  else {
    puts("POSSIBLE");
    rep(i, 1, n) printf("%d%c", ans[i], " \n"[i == n]);
  }
  return 0;
}