Problem p03656 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define LL long long
#define mp(x, y) make_pair(x, y)
#define pll pair<long long, long long>
#define pii pair<int, int>
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int stack[20];
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int top = 0;
  while (x)
    stack[++top] = x % 10, x /= 10;
  while (top)
    putchar(stack[top--] + '0');
}
template <typename T> inline void pr1(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void pr2(T x) {
  write(x);
  putchar('\n');
}
const int MAXN = 100005;
struct edge {
  int x, y, next;
};
vector<int> nxt[MAXN];
int n;
struct Graph {
  edge a[2 * MAXN];
  int len, last[MAXN];
  int du[MAXN];
  void ins(int x, int y) {
    len++;
    a[len].x = x;
    a[len].y = y;
    a[len].next = last[x];
    last[x] = len;
  }
  int init() {
    int ret;
    for (int i = 1; i <= n; i++) {
      int x = read();
      if (x == -1)
        ret = x;
      else
        ins(x, i), ++du[x];
    }
    return ret;
  }
  vector<int> vec[MAXN];
  int dfs(int x) {
    for (int k = last[x]; k; k = a[k].next) {
      int y = a[k].y;
      int u = dfs(y);
      vec[x].push_back(u);
    }
    if (!(du[x] & 1))
      vec[x].push_back(x);
    for (int i = 0; i < vec[x].size() - 1; i += 2) {
      int u = vec[x][i], v = vec[x][i + 1];
      nxt[u].push_back(v);
      nxt[v].push_back(u);
    }
    return vec[x].back();
  }
} G1, G2;
int col[MAXN];
void dfs(int x) {
  for (int k = 0; k < nxt[x].size(); k++)
    if (col[nxt[x][k]] == -1)
      col[nxt[x][k]] = col[x] ^ 1, dfs(nxt[x][k]);
}
int main() {
  n = read();
  int r1 = G1.init();
  int r2 = G2.init();
  for (int i = 1; i <= n; i++)
    if ((G1.du[i] & 1) != (G2.du[i] & 1))
      return puts("IMPOSSIBLE"), 0;
  G1.dfs(r1);
  G2.dfs(r2);
  puts("POSSIBLE");
  memset(col, -1, sizeof(col));
  for (int i = 1; i <= n; i++)
    if (col[i] == -1 && !(G1.du[i] & 1))
      col[i] = 0, dfs(i);
  for (int i = 1; i <= n; i++)
    pr1(col[i] == -1 ? 0 : (col[i] == 1 ? 1 : -1));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define LL long long
#define mp(x, y) make_pair(x, y)
#define pll pair<long long, long long>
#define pii pair<int, int>
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int stack[20];
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int top = 0;
  while (x)
    stack[++top] = x % 10, x /= 10;
  while (top)
    putchar(stack[top--] + '0');
}
template <typename T> inline void pr1(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void pr2(T x) {
  write(x);
  putchar('\n');
}
const int MAXN = 100005;
struct edge {
  int x, y, next;
};
vector<int> nxt[MAXN];
int n;
struct Graph {
  edge a[2 * MAXN];
  int len, last[MAXN];
  int du[MAXN];
  void ins(int x, int y) {
    len++;
    a[len].x = x;
    a[len].y = y;
    a[len].next = last[x];
    last[x] = len;
  }
  int init() {
    int ret;
    for (int i = 1; i <= n; i++) {
      int x = read();
      if (x == -1)
        ret = i;
      else
        ins(x, i), ++du[x];
    }
    return ret;
  }
  vector<int> vec[MAXN];
  int dfs(int x) {
    for (int k = last[x]; k; k = a[k].next) {
      int y = a[k].y;
      int u = dfs(y);
      vec[x].push_back(u);
    }
    if (!(du[x] & 1))
      vec[x].push_back(x);
    for (int i = 0; i < vec[x].size() - 1; i += 2) {
      int u = vec[x][i], v = vec[x][i + 1];
      nxt[u].push_back(v);
      nxt[v].push_back(u);
    }
    return vec[x].back();
  }
} G1, G2;
int col[MAXN];
void dfs(int x) {
  for (int k = 0; k < nxt[x].size(); k++)
    if (col[nxt[x][k]] == -1)
      col[nxt[x][k]] = col[x] ^ 1, dfs(nxt[x][k]);
}
int main() {
  n = read();
  int r1 = G1.init();
  int r2 = G2.init();
  for (int i = 1; i <= n; i++)
    if ((G1.du[i] & 1) != (G2.du[i] & 1))
      return puts("IMPOSSIBLE"), 0;
  G1.dfs(r1);
  G2.dfs(r2);
  puts("POSSIBLE");
  memset(col, -1, sizeof(col));
  for (int i = 1; i <= n; i++)
    if (col[i] == -1 && !(G1.du[i] & 1))
      col[i] = 0, dfs(i);
  for (int i = 1; i <= n; i++)
    pr1(col[i] == -1 ? 0 : (col[i] == 1 ? 1 : -1));
  return 0;
}