Problem p03656 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define rep(i, a, b) for (i = a; i <= b; i++)
using namespace std;
// const int N=500010;
const int N = 100;

struct node {
  int to, next, id;
};
node edge[N * 2];
int graph[N], tot;
int c[N], id[N];
int vis[N];
int sp[N];
int con[N];
int ID;
int rt1, rt2;

void addedge(int i, int x, int y, int id) {
  edge[i].to = y;
  edge[i].next = graph[x];
  graph[x] = i;
  edge[i].id = id;
}

void dfs(int x) {
  int i, y, id;
  for (i = graph[x]; i; i = graph[x]) {
    y = edge[i].to;
    id = edge[i].id;
    graph[x] = edge[i].next;
    if (vis[id])
      continue;
    if (!sp[id]) {
      c[x]--;
      c[y]++;
    }
    vis[id] = 1;
    dfs(y);
  }
}

int main() {
  //	freopen("a.in","r",stdin);
  //	freopen("a.out","w",stdout);
  int i, n, x;
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%d", &x);
    if (x == -1) {
      rt1 = i;
      con[i]++;
    } else {
      ID++;
      tot++;
      addedge(tot, i, x, ID);
      tot++;
      addedge(tot, x, i, ID);
      con[i]++;
      con[x]++;
    }
  }
  rep(i, 1, n) if (con[i] & 1) {
    ID++;
    sp[ID] = 1;
    tot++;
    addedge(tot, i, i + n, ID);
    tot++;
    addedge(tot, i + n, i, ID);
  }
  rep(i, 1, n) {
    scanf("%d", &x);
    if (x == -1) {
      rt2 = i + n;
      con[i]++;
    } else {
      ID++;
      tot++;
      addedge(tot, i + n, x + n, ID);
      tot++;
      addedge(tot, x + n, i + n, ID);
      con[i]++;
      con[x]++;
    }
  }
  ID++;
  tot++;
  addedge(tot, rt1, rt2, ID);
  tot++;
  addedge(tot, rt2, rt1, ID);
  rep(i, 1, n) if (con[i] & 1) {
    printf("IMPOSSIBLE\n");
    return 0;
  }
  printf("POSSIBLE\n");
  dfs(1);
  rep(i, 1, n) printf("%d ", c[i]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define rep(i, a, b) for (i = a; i <= b; i++)
using namespace std;
const int N = 500010;
// const int N=100;

struct node {
  int to, next, id;
};
node edge[N * 2];
int graph[N], tot;
int c[N], id[N];
int vis[N];
int sp[N];
int con[N];
int ID;
int rt1, rt2;

void addedge(int i, int x, int y, int id) {
  edge[i].to = y;
  edge[i].next = graph[x];
  graph[x] = i;
  edge[i].id = id;
}

void dfs(int x) {
  int i, y, id;
  for (i = graph[x]; i; i = graph[x]) {
    y = edge[i].to;
    id = edge[i].id;
    graph[x] = edge[i].next;
    if (vis[id])
      continue;
    if (!sp[id]) {
      c[x]--;
      c[y]++;
    }
    vis[id] = 1;
    dfs(y);
  }
}

int main() {
  //	freopen("a.in","r",stdin);
  //	freopen("a.out","w",stdout);
  int i, n, x;
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%d", &x);
    if (x == -1) {
      rt1 = i;
      con[i]++;
    } else {
      ID++;
      tot++;
      addedge(tot, i, x, ID);
      tot++;
      addedge(tot, x, i, ID);
      con[i]++;
      con[x]++;
    }
  }
  rep(i, 1, n) if (con[i] & 1) {
    ID++;
    sp[ID] = 1;
    tot++;
    addedge(tot, i, i + n, ID);
    tot++;
    addedge(tot, i + n, i, ID);
  }
  rep(i, 1, n) {
    scanf("%d", &x);
    if (x == -1) {
      rt2 = i + n;
      con[i]++;
    } else {
      ID++;
      tot++;
      addedge(tot, i + n, x + n, ID);
      tot++;
      addedge(tot, x + n, i + n, ID);
      con[i]++;
      con[x]++;
    }
  }
  ID++;
  tot++;
  addedge(tot, rt1, rt2, ID);
  tot++;
  addedge(tot, rt2, rt1, ID);
  rep(i, 1, n) if (con[i] & 1) {
    printf("IMPOSSIBLE\n");
    return 0;
  }
  printf("POSSIBLE\n");
  dfs(1);
  rep(i, 1, n) printf("%d ", c[i]);
  return 0;
}