Problem p03656 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

#define mp(x, y) make_pair(x, y)
const int MAXN = 2e5 + 5;

int n, r1, r2, ecnt;
int ans[MAXN], siz[MAXN], ban[MAXN * 4];
int head[MAXN], cnt;
struct node {
  int v, id, nxt;
} E[MAXN * 4];

void addedge(int u, int v, int id) {
  E[++cnt] = (node){v, id, head[u]};
  head[u] = cnt;
}

void dfs(int u, int fa = 0) {
  for (int i = head[u]; i; i = E[i].nxt) {
    int v = E[i].v;
    if (v == fa)
      continue;
    dfs(v, u);
    siz[u]++;
  }
}

void sol(int u) {
  for (int i = head[u]; i; i = E[i].nxt) {
    int v = E[i].v, id = E[i].id;
    if (ban[id])
      continue;
    ban[id] = 1;
    if (v == u - n)
      ans[v] = -1;
    else if (v == u + n)
      ans[u] = 1;
    sol(v);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x == -1)
      r1 = i;
    else {
      addedge(x, i, ++ecnt);
      addedge(i, x, ecnt);
    }
  }
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x == -1)
      r2 = i + n;
    else {
      addedge(x + n, i + n, ++ecnt);
      addedge(i + n, x + n, ecnt);
    }
  }
  dfs(r1);
  dfs(r2);
  for (int i = 1; i <= n; i++) {
    if ((siz[i] & 1) != (siz[i + n] & 1))
      return puts("IMPOSSIBLE"), 0;
    if (!(siz[i] & 1)) {
      addedge(i, i + n, ++ecnt);
      addedge(i + n, i, ecnt);
    }
  }
  addedge(r1, 2 * n + 1, ++ecnt), addedge(2 * n + 1, r1, ecnt);
  addedge(r2, 2 * n + 1, ++ecnt), addedge(2 * n + 1, r2, ecnt);
  sol(2 * n + 1);
  puts("POSSIBLE");
  for (int i = 1; i <= n; i++)
    printf("%d ", ans[i]);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

#define mp(x, y) make_pair(x, y)
const int MAXN = 2e5 + 5;

int n, r1, r2, ecnt;
int ans[MAXN], siz[MAXN], ban[MAXN * 4];
int head[MAXN], cnt;
struct node {
  int v, id, nxt;
} E[MAXN * 4];

void addedge(int u, int v, int id) {
  E[++cnt] = (node){v, id, head[u]};
  head[u] = cnt;
}

void dfs(int u, int fa = 0) {
  for (int i = head[u]; i; i = E[i].nxt) {
    int v = E[i].v;
    if (v == fa)
      continue;
    dfs(v, u);
    siz[u]++;
  }
}

void sol(int u) {
  for (int &i = head[u]; i; i = E[i].nxt) {
    int v = E[i].v, id = E[i].id;
    if (ban[id])
      continue;
    ban[id] = 1;
    if (v == u - n)
      ans[v] = -1;
    else if (v == u + n)
      ans[u] = 1;
    sol(v);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x == -1)
      r1 = i;
    else {
      addedge(x, i, ++ecnt);
      addedge(i, x, ecnt);
    }
  }
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x == -1)
      r2 = i + n;
    else {
      addedge(x + n, i + n, ++ecnt);
      addedge(i + n, x + n, ecnt);
    }
  }
  dfs(r1);
  dfs(r2);
  for (int i = 1; i <= n; i++) {
    if ((siz[i] & 1) != (siz[i + n] & 1))
      return puts("IMPOSSIBLE"), 0;
    if (!(siz[i] & 1)) {
      addedge(i, i + n, ++ecnt);
      addedge(i + n, i, ecnt);
    }
  }
  addedge(r1, 2 * n + 1, ++ecnt), addedge(2 * n + 1, r1, ecnt);
  addedge(r2, 2 * n + 1, ++ecnt), addedge(2 * n + 1, r2, ecnt);
  sol(2 * n + 1);
  puts("POSSIBLE");
  for (int i = 1; i <= n; i++)
    printf("%d ", ans[i]);
}