Problem p03650 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

int IT_MAX = 1 << 19;
int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;
#define szz(x) (int)(x).size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define Se second
#define Fi first

const int MX = 200005;
const int MM = 1000000007;

int N, M;
int nxt[MX], deg[MX], ans[MX];
vector<int> G[MX];

int get_number(int p) {
  vector<int> L;
  for (int c : G[p])
    L.push_back(ans[c]);
  sort(L.begin(), L.end());
  L.resize(unique(L.begin(), L.end()) - L.begin());
  L.push_back(1e9);
  for (int i = 0; i < L.size(); i++) {
    if (L[i] != i)
      return i;
  }
  return 1e9;
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", nxt + i);
    deg[nxt[i]]++;
    G[nxt[i]].push_back(i);
  }
  vector<int> Q;
  for (int i = 1; i <= N; i++) {
    if (deg[i] == 0)
      Q.push_back(i);
  }
  for (int i = 1; i <= N; i++)
    ans[i] = 1e9;
  for (int i = 0; i < Q.size(); i++) {
    int p = Q[i];
    ans[p] = get_number(p);
    if (--deg[nxt[p]] == 0)
      Q.push_back(p);
  }

  for (int i = 1; i <= N; i++) {
    if (!deg[i])
      continue;
    int st = i, mn = 1e9;
    vector<int> L;
    do {
      deg[st] = 0;
      L.push_back(st);
      st = nxt[st];
    } while (st != i);
    for (int c : L)
      mn = min(mn, get_number(c));
    int ch = L.size() % 2;
    for (int c : L)
      if (mn != get_number(c))
        ch = 0;
    if (ch)
      return !printf("IMPOSSIBLE\n");
  }
  printf("POSSIBLE\n");
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

int IT_MAX = 1 << 19;
int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;
#define szz(x) (int)(x).size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define Se second
#define Fi first

const int MX = 200005;
const int MM = 1000000007;

int N, M;
int nxt[MX], deg[MX], ans[MX];
vector<int> G[MX];

int get_number(int p) {
  vector<int> L;
  for (int c : G[p])
    L.push_back(ans[c]);
  sort(L.begin(), L.end());
  L.resize(unique(L.begin(), L.end()) - L.begin());
  L.push_back(1e9);
  for (int i = 0; i < L.size(); i++) {
    if (L[i] != i)
      return i;
  }
  return 1e9;
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", nxt + i);
    deg[nxt[i]]++;
    G[nxt[i]].push_back(i);
  }
  vector<int> Q;
  for (int i = 1; i <= N; i++) {
    if (deg[i] == 0)
      Q.push_back(i);
  }
  for (int i = 1; i <= N; i++)
    ans[i] = 1e9;
  for (int i = 0; i < Q.size(); i++) {
    int p = Q[i];
    ans[p] = get_number(p);
    if (--deg[nxt[p]] == 0)
      Q.push_back(nxt[p]);
  }

  for (int i = 1; i <= N; i++) {
    if (!deg[i])
      continue;
    int st = i, mn = 1e9;
    vector<int> L;
    do {
      deg[st] = 0;
      L.push_back(st);
      st = nxt[st];
    } while (st != i);
    for (int c : L)
      mn = min(mn, get_number(c));
    int ch = L.size() % 2;
    for (int c : L)
      if (mn != get_number(c))
        ch = 0;
    if (ch)
      return !printf("IMPOSSIBLE\n");
  }
  printf("POSSIBLE\n");
}