Problem p03656 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); i--)
#define ITER(it, a) for (typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a, value) memset(a, value, sizeof(a))

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const LL INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;

const int MOD = 1000 * 1000 * 1000 + 7;
const int MAX = 2 * 1000 * 100 + 47;

int A[MAX];
int B[MAX];
VI g[MAX];
VI I[MAX];
int U[MAX];
int P[MAX];
VI V;

int DA[MAX];
int DB[MAX];
int R[MAX];
int e;
void addEdge(int a, int b) {
  g[a].push_back(b);
  I[a].push_back(e);

  g[b].push_back(a);
  I[b].push_back(e);
  e++;
}

void euler(int v) {
  for (; P[v] < SZ(g[v]); P[v]++) {
    int to = g[v][P[v]];
    int e = I[v][P[v]];
    if (U[e])
      continue;
    U[e] = 1;
    euler(to);
  }

  V.push_back(v);
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  FOR(i, 0, n) {
    cin >> A[i];
    if (A[i] != -1) {
      A[i]--;
      DA[A[i]] ^= 1;
    }
  }

  FOR(i, 0, n) {
    cin >> B[i];
    if (B[i] != -1) {
      B[i]--;
      DB[B[i]] ^= 1;
    }
  }

  FOR(i, 0, n) {
    if (DA[i] != DB[i]) {
      cout << "IMPOSSIBLE" << endl;
      return 0;
    }
  }

  // FOR(i, 0, n)
  //{
  //	cout << DA[i] << " " << DB[i] << endl;
  // }

  int sz = 2 * n + 1;
  FOR(i, 0, n) {
    if (A[i] == -1) {
      addEdge(i, sz - 1);
      continue;
    }

    addEdge(i, A[i]);
  }

  FOR(i, 0, n) {
    if (B[i] == -1) {
      addEdge(i + n, sz - 1);
      continue;
    }

    addEdge(i + n, B[i] + n);
  }

  FOR(i, 0, n) {
    if (DA[i] == 1)
      continue;
    addEdge(i, i + n);
  }

  euler(sz - 1);
  // FOR(i, 0, SZ(V))
  //{
  //	if (V[i] == sz - 1)
  //	{
  //		cout << "R ";
  //		continue;
  //	}

  //	if (V[i] < n)
  //	{
  //		cout << "A" << V[i] + 1 << " ";
  //		continue;
  //	}

  //	cout << "B" << V[i] - n + 1 << " ";
  //}
  // cout << endl;
  FOR(i, 0, SZ(V) - 1) {
    int a = V[i];
    int b = V[i + 1];
    if (a == b - n) {
      R[a] = 1;
    }

    if (b == a - n) {
      R[b] = -1;
    }
  }

  cout << "POSSIBLE" << endl;
  FOR(i, 0, n) { cout << R[i] << " "; }

  cout << endl;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); i--)
#define ITER(it, a) for (typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a, value) memset(a, value, sizeof(a))

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const LL INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;

const int MOD = 1000 * 1000 * 1000 + 7;
const int MAX = 4 * 1000 * 100 + 47;

int A[MAX];
int B[MAX];
VI g[MAX];
VI I[MAX];
int U[MAX];
int P[MAX];
VI V;

int DA[MAX];
int DB[MAX];
int R[MAX];
int e;
void addEdge(int a, int b) {
  g[a].push_back(b);
  I[a].push_back(e);

  g[b].push_back(a);
  I[b].push_back(e);
  e++;
}

void euler(int v) {
  for (; P[v] < SZ(g[v]); P[v]++) {
    int to = g[v][P[v]];
    int e = I[v][P[v]];
    if (U[e])
      continue;
    U[e] = 1;
    euler(to);
  }

  V.push_back(v);
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  FOR(i, 0, n) {
    cin >> A[i];
    if (A[i] != -1) {
      A[i]--;
      DA[A[i]] ^= 1;
    }
  }

  FOR(i, 0, n) {
    cin >> B[i];
    if (B[i] != -1) {
      B[i]--;
      DB[B[i]] ^= 1;
    }
  }

  FOR(i, 0, n) {
    if (DA[i] != DB[i]) {
      cout << "IMPOSSIBLE" << endl;
      return 0;
    }
  }

  // FOR(i, 0, n)
  //{
  //	cout << DA[i] << " " << DB[i] << endl;
  // }

  int sz = 2 * n + 1;
  FOR(i, 0, n) {
    if (A[i] == -1) {
      addEdge(i, sz - 1);
      continue;
    }

    addEdge(i, A[i]);
  }

  FOR(i, 0, n) {
    if (B[i] == -1) {
      addEdge(i + n, sz - 1);
      continue;
    }

    addEdge(i + n, B[i] + n);
  }

  FOR(i, 0, n) {
    if (DA[i] == 1)
      continue;
    addEdge(i, i + n);
  }

  euler(sz - 1);
  // FOR(i, 0, SZ(V))
  //{
  //	if (V[i] == sz - 1)
  //	{
  //		cout << "R ";
  //		continue;
  //	}

  //	if (V[i] < n)
  //	{
  //		cout << "A" << V[i] + 1 << " ";
  //		continue;
  //	}

  //	cout << "B" << V[i] - n + 1 << " ";
  //}
  // cout << endl;
  FOR(i, 0, SZ(V) - 1) {
    int a = V[i];
    int b = V[i + 1];
    if (a == b - n) {
      R[a] = 1;
    }

    if (b == a - n) {
      R[b] = -1;
    }
  }

  cout << "POSSIBLE" << endl;
  FOR(i, 0, n) { cout << R[i] << " "; }

  cout << endl;
}