Problem p03656 (C++)

Buggy submission (status: Runtime Error):
/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 1e5;

int n, npas;
int nsonA[N + 3], nsonB[N + 3], pas[N * 2 + 10], ans[N + 3];
bool vis[3 * N + 3];

struct EDGE {
  int to, id;
  EDGE *nxt;
};
struct GRAPH {
  EDGE edg[N * 6 + 3], *head[2 * N + 3], *ncnt;
  int nedg;
  GRAPH() {
    ncnt = edg;
    nedg = 0;
  }
  void AddEdge(int u, int v) {
    EDGE *p = ++ncnt, *q = ++ncnt;
    nedg++;
    *p = (EDGE){v, nedg, head[u]};
    head[u] = p;
    *q = (EDGE){u, nedg, head[v]};
    head[v] = q;
  }
  EDGE *&operator[](int id) { return head[id]; }
};
GRAPH Gr;

void DFS(int u) {
  for (; Gr[u]; Gr[u] = Gr[u]->nxt) {
    EDGE *now = Gr[u];
    int v = now->to;
    if (vis[now->id])
      continue;
    vis[now->id] = true;
    DFS(v);
  }
  pas[++npas] = u;
}
int main() {
  scanf("%d", &n);
  for (int i = 1, fa; i <= n; i++) {
    scanf("%d", &fa);
    if (fa == -1)
      Gr.AddEdge(2 * n + 1, i);
    else
      nsonA[fa]++, Gr.AddEdge(fa, i);
  }
  for (int i = 1, fa; i <= n; i++) {
    scanf("%d", &fa);
    if (fa == -1)
      Gr.AddEdge(2 * n + 1, i + n);
    else
      nsonB[fa]++, Gr.AddEdge(fa + n, i + n);
  }
  for (int i = 1; i <= n; i++) {
    if ((nsonA[i] & 1) ^ (nsonB[i] & 1)) {
      printf("IMPOSSIBLE\n");
      return 0;
    }
    if (!(nsonA[i] & 1))
      Gr.AddEdge(i, i + n);
  }
  DFS(2 * n + 1);
  // printf("%d",pas[1]);
  for (int i = 2; i <= npas; i++) {
    if (pas[i - 1] == pas[i] + n)
      ans[pas[i]] = -1;
    if (pas[i - 1] + n == pas[i])
      ans[pas[i - 1]] = 1;
    // printf(" -> %d",pas[i]);
  }
  printf("\n");
  printf("POSSIBLE\n");
  for (int i = 1; i <= n; i++) {
    printf("%d", ans[i]);
    if (i == n)
      putchar('\n');
    else
      putchar(' ');
  }
  return 0;
}


Fixed submission:
/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 1e5;

int n, npas;
int nsonA[N + 3], nsonB[N + 3], pas[N * 2 + 10], ans[N + 3];
bool vis[3 * N + 3];

struct EDGE {
  int to, id;
  EDGE *nxt;
};
struct GRAPH {
  EDGE edg[N * 6 + 3], *head[2 * N + 3], *ncnt;
  int nedg;
  GRAPH() {
    ncnt = edg;
    nedg = 0;
  }
  void AddEdge(int u, int v) {
    EDGE *p = ++ncnt, *q = ++ncnt;
    nedg++;
    *p = (EDGE){v, nedg, head[u]};
    head[u] = p;
    *q = (EDGE){u, nedg, head[v]};
    head[v] = q;
  }
  EDGE *&operator[](int id) { return head[id]; }
};
GRAPH Gr;

void DFS(int u) {
  for (; Gr[u]; Gr[u] = Gr[u]->nxt) {
    EDGE *now = Gr[u];
    int v = now->to;
    if (vis[now->id])
      continue;
    vis[now->id] = true;
    DFS(v);
    if (!Gr[u])
      break;
  }
  pas[++npas] = u;
}
int main() {
  scanf("%d", &n);
  for (int i = 1, fa; i <= n; i++) {
    scanf("%d", &fa);
    if (fa == -1)
      Gr.AddEdge(2 * n + 1, i);
    else
      nsonA[fa]++, Gr.AddEdge(fa, i);
  }
  for (int i = 1, fa; i <= n; i++) {
    scanf("%d", &fa);
    if (fa == -1)
      Gr.AddEdge(2 * n + 1, i + n);
    else
      nsonB[fa]++, Gr.AddEdge(fa + n, i + n);
  }
  for (int i = 1; i <= n; i++) {
    if ((nsonA[i] & 1) ^ (nsonB[i] & 1)) {
      printf("IMPOSSIBLE\n");
      return 0;
    }
    if (!(nsonA[i] & 1))
      Gr.AddEdge(i, i + n);
  }
  DFS(2 * n + 1);
  // printf("%d",pas[1]);
  for (int i = 2; i <= npas; i++) {
    if (pas[i - 1] == pas[i] + n)
      ans[pas[i]] = -1;
    if (pas[i - 1] + n == pas[i])
      ans[pas[i - 1]] = 1;
    // printf(" -> %d",pas[i]);
  }
  printf("\n");
  printf("POSSIBLE\n");
  for (int i = 1; i <= n; i++) {
    printf("%d", ans[i]);
    if (i == n)
      putchar('\n');
    else
      putchar(' ');
  }
  return 0;
}