Submission #3921022


Source Code Expand

Copy
#include <bits/stdc++.h>
#define MAX_N 200000
using namespace std;
template <class T> inline void read(T &x) {
	x = 0; int c = getchar(), f = 1;
	for (; !isdigit(c); c = getchar()) if (c == 45) f = -1;
	for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0');
}
int n, a[MAX_N+5], b[MAX_N+5];
int into[MAX_N+5], outo[MAX_N+5];
int s, t, f[MAX_N+5], pre[MAX_N+5];
queue <int> que; bool mrk[MAX_N+5];
vector <int> G[MAX_N+5], R[MAX_N+5];
bool chk() {
	for (int u = s, v = pre[u]; ; u = v, v = pre[u])
		{if (f[u] == f[v]) return false; if (u == t) break;}
	return true;
}
int main() {
	read(n);
	for (int i = 1, fa; i <= n; i++)
		read(fa), 
		G[fa].push_back(i), outo[fa]++, 
		R[i].push_back(fa), into[i]++;
	for (int i = 1; i <= n; i++)
		if (!outo[i]) que.push(i);
	while (!que.empty()) {
		int u = que.front(); que.pop();
		memset(mrk, false, sizeof mrk);
		for (int v : G[u]) mrk[f[v]] = true;
		while (mrk[f[u]]) f[u]++;
		for (int v : R[u]) if (!--outo[v]) que.push(v);
	}
	for (int u = 1; u <= n; u++) if (outo[u]) {
		memset(mrk, false, sizeof mrk), s = u;
		for (int v : G[u]) if (!outo[v]) mrk[f[v]] = true; else pre[v] = u;
		while (mrk[a[u]]) a[u]++; b[u] = a[u]+1; while (mrk[b[u]]) b[u]++;
	}
	f[s] = a[s];
	for (int u = s, v = pre[u]; ; u = v, v = pre[u])
		{if (v == s) {t = u; break;} f[v] = f[u] == a[v] ? b[v] : a[v];}
	if (f[t] != a[s] && chk()) return puts("POSSIBLE"), 0;
	f[s] = b[s], t = 0;
	for (int u = s, v = pre[u]; ; u = v, v = pre[u])
		{if (v == s) {t = u; break;} f[v] = f[u] == a[v] ? b[v] : a[v];}
	if (f[t] == a[s] && chk()) return puts("POSSIBLE"), 0;
	return puts("IMPOSSIBLE"), 0;
}

Submission Info

Submission Time
Task F - Namori Grundy
User Azrael_Death
Language C++14 (GCC 5.4.1)
Score 800
Code Size 1670 Byte
Status AC
Exec Time 1296 ms
Memory 25984 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 4
AC × 68
Set Name Test Cases
Sample example0, example1, example2, example3
All example0, example1, example2, example3, loop0, loop1, loop10, loop11, loop12, loop13, loop14, loop15, loop16, loop17, loop18, loop19, loop2, loop3, loop4, loop5, loop6, loop7, loop8, loop9, loopex0, loopex1, loopex10, loopex11, loopex12, loopex13, loopex14, loopex15, loopex16, loopex17, loopex18, loopex19, loopex2, loopex20, loopex21, loopex22, loopex23, loopex3, loopex4, loopex5, loopex6, loopex7, loopex8, loopex9, rand0, rand1, rand2, rand3, rand4, rand5, rand6, rand7, rand8, rand9, small0, small1, small2, small3, small4, small5, small6, small7, small8, small9
Case Name Status Exec Time Memory
example0 AC 5 ms 12544 KB
example1 AC 5 ms 12544 KB
example2 AC 5 ms 12544 KB
example3 AC 5 ms 12544 KB
loop0 AC 1214 ms 23156 KB
loop1 AC 1201 ms 23156 KB
loop10 AC 1214 ms 22520 KB
loop11 AC 1198 ms 23160 KB
loop12 AC 1191 ms 23156 KB
loop13 AC 1179 ms 23808 KB
loop14 AC 1194 ms 23156 KB
loop15 AC 1197 ms 23160 KB
loop16 AC 1196 ms 22520 KB
loop17 AC 1186 ms 23160 KB
loop18 AC 1194 ms 23156 KB
loop19 AC 1181 ms 23808 KB
loop2 AC 1190 ms 22520 KB
loop3 AC 1184 ms 23156 KB
loop4 AC 1201 ms 23156 KB
loop5 AC 1194 ms 23156 KB
loop6 AC 1208 ms 22520 KB
loop7 AC 1185 ms 23808 KB
loop8 AC 1187 ms 22524 KB
loop9 AC 1191 ms 23156 KB
loopex0 AC 1214 ms 23404 KB
loopex1 AC 1197 ms 23152 KB
loopex10 AC 1186 ms 23160 KB
loopex11 AC 1185 ms 23932 KB
loopex12 AC 1194 ms 23028 KB
loopex13 AC 1200 ms 23156 KB
loopex14 AC 1197 ms 23028 KB
loopex15 AC 1204 ms 23156 KB
loopex16 AC 1219 ms 23148 KB
loopex17 AC 1191 ms 23928 KB
loopex18 AC 1230 ms 23528 KB
loopex19 AC 1187 ms 23156 KB
loopex2 AC 1186 ms 23156 KB
loopex20 AC 1186 ms 23156 KB
loopex21 AC 1214 ms 23276 KB
loopex22 AC 1213 ms 24048 KB
loopex23 AC 1197 ms 23156 KB
loopex3 AC 1199 ms 23032 KB
loopex4 AC 1193 ms 23156 KB
loopex5 AC 1219 ms 23404 KB
loopex6 AC 1188 ms 22264 KB
loopex7 AC 1202 ms 23280 KB
loopex8 AC 1210 ms 23280 KB
loopex9 AC 1226 ms 23404 KB
rand0 AC 345 ms 15616 KB
rand1 AC 519 ms 17152 KB
rand2 AC 130 ms 13824 KB
rand3 AC 1020 ms 21888 KB
rand4 AC 1296 ms 25984 KB
rand5 AC 597 ms 17792 KB
rand6 AC 244 ms 14848 KB
rand7 AC 68 ms 13184 KB
rand8 AC 841 ms 20352 KB
rand9 AC 57 ms 13056 KB
small0 AC 5 ms 12544 KB
small1 AC 5 ms 12544 KB
small2 AC 5 ms 12544 KB
small3 AC 5 ms 12544 KB
small4 AC 5 ms 12544 KB
small5 AC 5 ms 12544 KB
small6 AC 5 ms 12544 KB
small7 AC 5 ms 12544 KB
small8 AC 5 ms 12544 KB
small9 AC 6 ms 12544 KB