Problem p03656 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;

int n, m;

struct _edge {
  int x, i;
  _edge(int x, int i) : x(x), i(i) {}
};

vector<_edge> edge[200001];
int ps[200001];
int deg[200001];
int used[500000];
int ans[100001];

int main() {
  freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  int rt;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    if (x < 0)
      ++x, rt = i;
    else {
      edge[x].emplace_back(i, m);
      edge[i].emplace_back(x, m);
      ++m;
    }
    ++deg[i];
    ++deg[x];
  }
  for (int i = n + 1; i <= n + n; ++i) {
    int x;
    cin >> x;
    x += n;
    if (x < n)
      x = 0;
    else {
      edge[x].emplace_back(i, m);
      edge[i].emplace_back(x, m);
      ++m;
    }
    ++deg[i];
    ++deg[x];
  }

  for (int i = 1; i <= n; ++i) {
    if ((deg[i] ^ deg[i + n]) & 1) {
      printf("IMPOSSIBLE\n");
      return 0;
    }
    for (int j = 2 | (deg[i] & 1); j--;) {
      edge[i].emplace_back(i + n, m);
      edge[i + n].emplace_back(i, m);
      ++m;
    }
  }

  for (int it = 0; it <= n + n; ++it) {
    int t = it ? it : rt;
    while (ps[t] < edge[t].size()) {
      int x = t;
      while (1) {
        bool b = 1;
        for (int &i = ps[x]; i < edge[x].size(); ++i) {
          int p = edge[x][i].x;
          int j = edge[x][i].i;
          if (used[j])
            continue;
          b = 0;
          used[j] = 1;
          if (x <= n && p > n)
            ++ans[x];
          if (x > n && p <= n)
            --ans[p];
          x = p;
          ++i;
          break;
        }
        if (b)
          break;
      }
    }
  }
  printf("POSSIBLE\n");
  for (int i = 1; i <= n; ++i) {
    printf("%d%c", ans[i], "\n "[i < n]);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;

int n, m;

struct _edge {
  int x, i;
  _edge(int x, int i) : x(x), i(i) {}
};

vector<_edge> edge[200001];
int ps[200001];
int deg[200001];
int used[500000];
int ans[100001];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  int rt;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    if (x < 0)
      ++x, rt = i;
    else {
      edge[x].emplace_back(i, m);
      edge[i].emplace_back(x, m);
      ++m;
    }
    ++deg[i];
    ++deg[x];
  }
  for (int i = n + 1; i <= n + n; ++i) {
    int x;
    cin >> x;
    x += n;
    if (x < n)
      x = 0;
    else {
      edge[x].emplace_back(i, m);
      edge[i].emplace_back(x, m);
      ++m;
    }
    ++deg[i];
    ++deg[x];
  }

  for (int i = 1; i <= n; ++i) {
    if ((deg[i] ^ deg[i + n]) & 1) {
      printf("IMPOSSIBLE\n");
      return 0;
    }
    for (int j = 2 | (deg[i] & 1); j--;) {
      edge[i].emplace_back(i + n, m);
      edge[i + n].emplace_back(i, m);
      ++m;
    }
  }

  for (int it = 0; it <= n + n; ++it) {
    int t = it ? it : rt;
    while (ps[t] < edge[t].size()) {
      int x = t;
      while (1) {
        bool b = 1;
        for (int &i = ps[x]; i < edge[x].size(); ++i) {
          int p = edge[x][i].x;
          int j = edge[x][i].i;
          if (used[j])
            continue;
          b = 0;
          used[j] = 1;
          if (x <= n && p > n)
            ++ans[x];
          if (x > n && p <= n)
            --ans[p];
          x = p;
          ++i;
          break;
        }
        if (b)
          break;
      }
    }
  }
  printf("POSSIBLE\n");
  for (int i = 1; i <= n; ++i) {
    printf("%d%c", ans[i], "\n "[i < n]);
  }
  return 0;
}