Problem p03656 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>

#define For(i, x, y) for (int i = x; i < y; i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lf else if

#define dprintf(...) fprintf(stderr, __VA_ARGS__)
using namespace std;

typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> Vi;

int IN() {
  int c, f, x;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0');
  while (isdigit(c = getchar()))
    x = (x << 1) + (x << 3) + c - '0';
  return !f ? x : -x;
}

const int N = 800000 + 19;

struct Edge {
  int y, nxt;
} E[N * 2];
int las[N], A[N], B[N], chk[N], val[N], vis[N];
int n, x, cnt;

void Link(int x, int y) {
  E[cnt] = (Edge){y, las[x]};
  las[x] = cnt++;
  E[cnt] = (Edge){x, las[y]};
  las[y] = cnt++;
}
void dfs(int x) {
  for (int i = las[x]; ~i; i = E[i].nxt) {
    las[x] = E[i].nxt;
    if (!vis[i / 2]) {
      vis[i / 2] = 1;
      chk[i] = 1;
      dfs(E[i].y);
    }
  }
}

int main() {
  memset(las, -1, sizeof(las));
  n = IN();
  For(i, 1, n + 1) {
    x = IN();
    if (x == -1) {
      Link(i, 0);
    } else {
      A[x]++;
      Link(x, i);
    }
  }
  For(i, 1, n + 1) {
    x = IN();
    if (x == -1) {
      Link(i + n, 0);
    } else {
      B[x]++;
      Link(x + n, i + n);
    }
  }
  For(i, 1, n + 1) if (A[i] % 2 != B[i] % 2) {
    puts("IMPOSSIBLE");
    return 0;
  }
  puts("POSSIBLE");
  For(i, 1, n + 1) if (A[i] % 2 == 0) {
    val[i] = cnt;
    Link(i, i + n);
  }
  dfs(1);
  For(i, 1, n + 1) {
    if (A[i] % 2 == 1) {
      printf("0 ");
    }
    lf(chk[val[i]]) { printf("1 "); }
    else {
      printf("-1 ");
    }
  }
  puts("");
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>

#define For(i, x, y) for (int i = x; i < y; i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lf else if

#define dprintf(...) fprintf(stderr, __VA_ARGS__)
using namespace std;

typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> Vi;

int IN() {
  int c, f, x;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0');
  while (isdigit(c = getchar()))
    x = (x << 1) + (x << 3) + c - '0';
  return !f ? x : -x;
}

const int N = 800000 + 19;

struct Edge {
  int y, nxt;
} E[N * 2];
int las[N], A[N], B[N], chk[N], val[N], vis[N];
int n, x, cnt;

void Link(int x, int y) {
  E[cnt] = (Edge){y, las[x]};
  las[x] = cnt++;
  E[cnt] = (Edge){x, las[y]};
  las[y] = cnt++;
}
void dfs(int x) {
  for (int i = las[x]; ~i; i = las[x]) {
    las[x] = E[i].nxt;
    if (!vis[i / 2]) {
      vis[i / 2] = 1;
      chk[i] = 1;
      dfs(E[i].y);
    }
  }
}

int main() {
  memset(las, -1, sizeof(las));
  n = IN();
  For(i, 1, n + 1) {
    x = IN();
    if (x == -1) {
      Link(i, 0);
    } else {
      A[x]++;
      Link(x, i);
    }
  }
  For(i, 1, n + 1) {
    x = IN();
    if (x == -1) {
      Link(i + n, 0);
    } else {
      B[x]++;
      Link(x + n, i + n);
    }
  }
  For(i, 1, n + 1) if (A[i] % 2 != B[i] % 2) {
    puts("IMPOSSIBLE");
    return 0;
  }
  puts("POSSIBLE");
  For(i, 1, n + 1) if (A[i] % 2 == 0) {
    val[i] = cnt;
    Link(i, i + n);
  }
  dfs(1);
  For(i, 1, n + 1) {
    if (A[i] % 2 == 1) {
      printf("0 ");
    }
    lf(chk[val[i]]) { printf("1 "); }
    else {
      printf("-1 ");
    }
  }
  puts("");
}