Submission #1469070


Source Code Expand

Copy
#include <bits/stdc++.h>

#define x first
#define y second

#ifdef ONLINE_JUDGE
#define DEBUG(x)
#else
#define DEBUG(x) cerr << #x << ": " << x << endl;
#endif

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;

const int mod=1000000000+7;

int addm(int& a,int b) {return (a+=b)<mod?a:a-=mod;}

template<class T,class U> bool smin(T& a,U b) {return a>b?(a=b,1):0;}
template<class T,class U> bool smax(T& a,U b) {return a<b?(a=b,1):0;}

vi al[200000];
int n;
bool act[200000],vis[200000];
int mex[200000];
vi cyc;

vi seen;

int mx(int i,int o=-1) {
	if (mex[i]!=-1) return mex[i];
	seen.clear();
	for (int j:al[i]) if (j!=o) seen.push_back(mex[j]);
	sort(seen.begin(),seen.end());
	seen.resize(unique(seen.begin(),seen.end())-seen.begin());
	for (int c=0;c<seen.size();c++) if (seen[c]!=c) {
		return mex[i]=c;
	}
	return mex[i]=seen.size();
}

bool tmx(int i) {
	int v=mex[i];
	mex[i]=-1;
	bool res=mx(i)==v;
	mex[i]=v;
	return res;
}

bool consist() {
	for (int i:cyc) if (!tmx(i)) return 0;
	return 1;
}


bool dfs(int i) {
	if (vis[i]) return 0;
	if (act[i]) return 1;
	act[i]=1;
	bool incyc=0;
	for (int j:al[i]) {
		incyc=incyc||dfs(j);
	}
	vis[i]=1;
	if (incyc) {
		cyc.push_back(i);
		return 1;
	}

	mx(i);
	return 0;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	for (int i=0;i<n;i++) {
		int u;cin >> u;u--;
		al[u].push_back(i);
	}

	fill(mex,mex+n,-1);

	for (int i=0;i<n;i++) if (!vis[i]) {
		dfs(i);
		if (cyc.size()) {
			mex[cyc[0]]=-1;
			int mx0=mx(cyc[0],cyc.back());
			for (int j=1;j<cyc.size();j++) mx(cyc[j]);
			if (!consist()) {
				mex[cyc.back()]=mx0;
				for (int j=0;j+1<cyc.size();j++) {
					mex[cyc[j]]=-1;
					mx(cyc[j]);
				}
				if (!consist()) {
					cout << "IMPOSSIBLE\n";
					return 0;
				}
			}
			cyc.clear();
		}
	}

	cout << "POSSIBLE\n";

}

Submission Info

Submission Time
Task F - Namori Grundy
User Whalanator
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2041 Byte
Status WA
Exec Time 69 ms
Memory 20344 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 800
Status
AC × 4
AC × 51
WA × 17
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 3 ms 4992 KB
example1 AC 3 ms 4992 KB
example2 AC 3 ms 4992 KB
example3 AC 3 ms 4992 KB
loop0 WA 41 ms 8960 KB
loop1 WA 41 ms 9088 KB
loop10 WA 41 ms 9088 KB
loop11 WA 41 ms 8960 KB
loop12 WA 41 ms 8960 KB
loop13 WA 42 ms 9088 KB
loop14 WA 42 ms 8960 KB
loop15 WA 42 ms 8960 KB
loop16 WA 42 ms 9088 KB
loop17 WA 41 ms 8960 KB
loop18 AC 41 ms 8960 KB
loop19 AC 41 ms 8960 KB
loop2 WA 42 ms 9088 KB
loop3 WA 41 ms 8960 KB
loop4 WA 41 ms 8960 KB
loop5 WA 41 ms 8960 KB
loop6 WA 42 ms 9088 KB
loop7 WA 41 ms 9088 KB
loop8 AC 42 ms 9088 KB
loop9 AC 41 ms 8960 KB
loopex0 AC 42 ms 9088 KB
loopex1 AC 42 ms 8960 KB
loopex10 AC 41 ms 8960 KB
loopex11 WA 41 ms 8960 KB
loopex12 AC 41 ms 8960 KB
loopex13 AC 42 ms 8960 KB
loopex14 AC 41 ms 8960 KB
loopex15 AC 43 ms 8960 KB
loopex16 AC 44 ms 9088 KB
loopex17 AC 41 ms 8960 KB
loopex18 AC 43 ms 9088 KB
loopex19 AC 41 ms 8960 KB
loopex2 AC 41 ms 8960 KB
loopex20 AC 41 ms 8960 KB
loopex21 AC 42 ms 9088 KB
loopex22 AC 43 ms 9088 KB
loopex23 AC 41 ms 8960 KB
loopex3 AC 41 ms 8960 KB
loopex4 AC 41 ms 8960 KB
loopex5 AC 43 ms 9088 KB
loopex6 AC 41 ms 8960 KB
loopex7 AC 42 ms 8960 KB
loopex8 AC 42 ms 8960 KB
loopex9 AC 42 ms 9088 KB
rand0 AC 16 ms 7040 KB
rand1 AC 22 ms 8192 KB
rand2 AC 8 ms 6784 KB
rand3 AC 43 ms 11904 KB
rand4 AC 69 ms 20344 KB
rand5 AC 24 ms 8320 KB
rand6 AC 13 ms 7424 KB
rand7 AC 6 ms 5504 KB
rand8 AC 36 ms 11520 KB
rand9 AC 5 ms 5632 KB
small0 AC 3 ms 4992 KB
small1 AC 3 ms 4992 KB
small2 AC 3 ms 4992 KB
small3 AC 3 ms 4992 KB
small4 AC 3 ms 4992 KB
small5 AC 3 ms 4992 KB
small6 AC 3 ms 4992 KB
small7 AC 3 ms 4992 KB
small8 AC 3 ms 4992 KB
small9 AC 3 ms 4992 KB