Language: C++

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
using uint = unsigned int;
using ull = unsigned long long;

template<typename T> using uset = unordered_set<T>;
template<typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;

template<typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template<typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

template<typename T>
ostream& operator <<(ostream &os, vector<T> &v) {
	for (auto &u : v) os << u << el;
	return (os);
}

template<typename T>
istream& operator >>(istream &is, vector<T> &v) {
	for (auto &u : v) is >> u;
	return (is);
}

template<typename T1, typename T2>
istream& operator >>(istream &is, pair<T1, T2> &p) {
	return (is >> p.first >> p.second);
}

int main() {
	int N; cin >> N;
	vector<vint> g(N), rg(N);
	vint grundy(N, 0);
	bitset<200005> notloop;
	vint in(N, 0);

	for (int i = 0; i < N; i++) {
		int p; cin >> p; --p;
		g[p].push_back(i);
		rg[i].push_back(p);
		in[p]++;
	}
	queue<int> que;
	for (int i = 0; i < N; i++) if (in[i] == 0) que.push(i);
	while (!que.empty()) {
		int u = que.front(); que.pop();
		notloop[u] = true;
		set<int> st;
		for (auto &to : g[u]) st.insert(grundy[to]);
		while (st.count(grundy[u])) grundy[u]++;
		for (auto &r : rg[u]) if (--in[r] == 0) que.push(r);
	}
	
	int s = -1;
	for (int i = 0; i < N; i++) if (in[r] < (int)g[i].size() && !notloop[i]) { s = i; break; }
	if (s == -1) { cout << "POSSIBLE" << endl; return (0); }
	
	vector<int> tr;
	{
		set<int> st;
		for (auto &to : g[s]) if (notloop[to]) st.insert(grundy[to]);
		while (st.count(grundy[s])) grundy[s]++;
		tr.push_back(grundy[s]);
		grundy[s]++;
		while (st.count(grundy[s])) grundy[s]++;
		tr.push_back(grundy[s]);
	}

	for (auto &x : tr) {
		//cout << x << endl;
		bitset<200005> used = notloop;
		vint gr = grundy, in2 = in;
		gr[s] = x;

		used[s] = true;
		int u = s;
		for (auto &r : rg[s]) if (!used[r] && --in2[r] == 0) { u = r; break; }
		while (!used[u]) {
			used[u] = true;
			set<int> st;
			for (auto &to : g[u]) st.insert(gr[to]);
			while (st.count(gr[u])) gr[u]++;
			//cout << u << " " << gr[u] << endl;
			for (auto &r : rg[u]) if (!used[r] && --in2[r] == 0) { u = r; break; }
		}
		set<int>st;
		for (auto &to : g[u]) st.insert(gr[to]);
		st.insert(gr[s]);
		while (st.count(gr[u])) gr[u]++;
		if (gr[u] <= (int)g[u].size()) {
			cout << "POSSIBLE" << endl;
			return (0);
		}

	}
	cout << "IMPOSSIBLE" << endl;
	return (0);
}


Compiler output:
a.cc: In function 'int main()':
a.cc:69:44: error: 'r' was not declared in this scope
   69 |         for (int i = 0; i < N; i++) if (in[r] < (int)g[i].size() && !notloop[i]) { s = i; break; }
      |                                            ^