Submission #1470316


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=dfs(j)||incyc;
	}
	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()) {
			//if (cyc.size()%2) cout << "IMPOSSIBLE\n";
			//else cout << "POSSIBLE\n";
			//return 0;
			mex[cyc[0]]=-1;
			int mx0=mx(cyc[0],cyc.back());
			for (int j=1;j<cyc.size();j++) mex[cyc[j]]=-1,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 800
Code Size 2153 Byte
Status AC
Exec Time 71 ms
Memory 22996 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 3 ms 4992 KB
example1 AC 3 ms 4992 KB
example2 AC 3 ms 4992 KB
example3 AC 3 ms 4992 KB
loop0 AC 41 ms 8960 KB
loop1 AC 42 ms 8960 KB
loop10 AC 41 ms 9088 KB
loop11 AC 43 ms 9088 KB
loop12 AC 43 ms 9088 KB
loop13 AC 42 ms 9088 KB
loop14 AC 42 ms 9088 KB
loop15 AC 42 ms 9088 KB
loop16 AC 43 ms 9088 KB
loop17 AC 42 ms 8960 KB
loop18 AC 41 ms 8960 KB
loop19 AC 42 ms 9088 KB
loop2 AC 42 ms 9088 KB
loop3 AC 42 ms 9088 KB
loop4 AC 42 ms 9088 KB
loop5 AC 42 ms 9088 KB
loop6 AC 49 ms 9088 KB
loop7 AC 65 ms 9088 KB
loop8 AC 41 ms 9088 KB
loop9 AC 42 ms 8960 KB
loopex0 AC 43 ms 9088 KB
loopex1 AC 42 ms 8960 KB
loopex10 AC 41 ms 8960 KB
loopex11 AC 42 ms 8960 KB
loopex12 AC 41 ms 8960 KB
loopex13 AC 42 ms 8960 KB
loopex14 AC 41 ms 8960 KB
loopex15 AC 42 ms 8960 KB
loopex16 AC 44 ms 9088 KB
loopex17 AC 42 ms 8960 KB
loopex18 AC 43 ms 9088 KB
loopex19 AC 43 ms 9088 KB
loopex2 AC 41 ms 8960 KB
loopex20 AC 43 ms 8960 KB
loopex21 AC 42 ms 9088 KB
loopex22 AC 42 ms 9088 KB
loopex23 AC 42 ms 8960 KB
loopex3 AC 41 ms 8960 KB
loopex4 AC 41 ms 8960 KB
loopex5 AC 42 ms 9088 KB
loopex6 AC 50 ms 8960 KB
loopex7 AC 42 ms 8960 KB
loopex8 AC 44 ms 9088 KB
loopex9 AC 43 ms 9088 KB
rand0 AC 15 ms 7296 KB
rand1 AC 22 ms 8576 KB
rand2 AC 9 ms 7040 KB
rand3 AC 59 ms 12800 KB
rand4 AC 71 ms 22996 KB
rand5 AC 25 ms 8704 KB
rand6 AC 13 ms 7936 KB
rand7 AC 5 ms 5632 KB
rand8 AC 37 ms 12544 KB
rand9 AC 5 ms 5760 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