Problem p03656 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, root1, root2, ne1[N], fi1[N], zz1[N], tot, cnt, ne2[N], zz2[N], fi2[N],
    fi[N];
int zz[N], ne[N], Stack[N], num1[N], num2[N], x, flag[N];
void jb1(int x, int y) {
  ne1[++tot] = fi1[x];
  fi1[x] = tot;
  zz1[tot] = y;
}
void jb2(int x, int y) {
  ne2[++tot] = fi2[x];
  fi2[x] = tot;
  zz2[tot] = y;
}
void jb(int x, int y) {
  ne[++tot] = fi[x];
  fi[x] = tot;
  zz[tot] = y;
}
void dfs(int x) {
  for (int i = fi[x]; i; i = ne[i])
    if (!flag[zz[i]]) {
      flag[zz[i]] = -flag[x];
      dfs(zz[i]);
    }
}
void dfs1(int x) {
  for (int i = fi1[x]; i; i = ne1[i])
    dfs1(zz1[i]);
  if (num1[x])
    Stack[++cnt] = x;
  while (cnt >= 2) {
    jb(Stack[cnt], Stack[cnt - 1]);
    jb(Stack[cnt - 1], Stack[cnt]);
    cnt -= 2;
  }
}
void dfs2(int x) {
  for (int i = fi2[x]; i; i = ne2[i])
    dfs2(zz2[i]);
  if (num2[x])
    Stack[++cnt] = x;
  while (cnt >= 2) {
    jb(Stack[cnt], Stack[cnt - 1]);
    jb(Stack[cnt - 1], Stack[cnt]);
    cnt -= 2;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    num1[i] = num2[i] = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (x != -1)
      num1[x] ^= 1, jb1(x, i);
    else
      root1 = i;
  }
  tot = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (x != -1)
      num2[x] ^= 1, jb2(x, i);
    else
      root2 = i;
  }
  tot = 0;
  for (int i = 1; i <= n; i++)
    if (num1[i] != num2[i]) {
      puts("IMPOSSIBLE");
      return 0;
    }
  puts("POSSIBLE");
  dfs1(root1);
  cnt = 0;
  dfs2(root2);
  for (int i = 1; i <= n; i++)
    if (num1[i] && !flag[i])
      flag[i] = 1, dfs(i);
  for (int i = 1; i <= n; i++)
    assert(!num1[i] || !flag[i]);
  for (int i = 1; i <= n; i++)
    printf("%d ", flag[i]);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, root1, root2, ne1[N], fi1[N], zz1[N], tot, cnt, ne2[N], zz2[N], fi2[N],
    fi[N];
int zz[N], ne[N], Stack[N], num1[N], num2[N], x, flag[N];
void jb1(int x, int y) {
  ne1[++tot] = fi1[x];
  fi1[x] = tot;
  zz1[tot] = y;
}
void jb2(int x, int y) {
  ne2[++tot] = fi2[x];
  fi2[x] = tot;
  zz2[tot] = y;
}
void jb(int x, int y) {
  ne[++tot] = fi[x];
  fi[x] = tot;
  zz[tot] = y;
}
void dfs(int x) {
  for (int i = fi[x]; i; i = ne[i])
    if (!flag[zz[i]]) {
      flag[zz[i]] = -flag[x];
      dfs(zz[i]);
    }
}
void dfs1(int x) {
  for (int i = fi1[x]; i; i = ne1[i])
    dfs1(zz1[i]);
  if (num1[x])
    Stack[++cnt] = x;
  while (cnt >= 2) {
    jb(Stack[cnt], Stack[cnt - 1]);
    jb(Stack[cnt - 1], Stack[cnt]);
    cnt -= 2;
  }
}
void dfs2(int x) {
  for (int i = fi2[x]; i; i = ne2[i])
    dfs2(zz2[i]);
  if (num2[x])
    Stack[++cnt] = x;
  while (cnt >= 2) {
    jb(Stack[cnt], Stack[cnt - 1]);
    jb(Stack[cnt - 1], Stack[cnt]);
    cnt -= 2;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    num1[i] = num2[i] = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (x != -1)
      num1[x] ^= 1, jb1(x, i);
    else
      root1 = i;
  }
  tot = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (x != -1)
      num2[x] ^= 1, jb2(x, i);
    else
      root2 = i;
  }
  tot = 0;
  for (int i = 1; i <= n; i++)
    if (num1[i] != num2[i]) {
      puts("IMPOSSIBLE");
      return 0;
    }
  puts("POSSIBLE");
  dfs1(root1);
  cnt = 0;
  dfs2(root2);
  for (int i = 1; i <= n; i++)
    if (num1[i] && !flag[i])
      flag[i] = 1, dfs(i);
  for (int i = 1; i <= n; i++)
    printf("%d ", flag[i]);
}