Submission #1688385


Source Code Expand

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//                       _oo0oo_                         //
//                      o8888888o                        //
//                      88" . "88      ------ hzt1       //
//                      (| -_- |)                        //
//                      0\  =  /0                        //
//                    ___/`---'\___                      //
//                  .' \|     |// '.                     //
//                 / \|||  :  |||// \                    //
//                / _||||| -:- |||||- \                  //
//               |   | \  -  /// |     |                 //
//               | \_|  ''\---/''  |_/ |                 //
//               \  .-\__  '-'  ___/-. /                 //
//             ___'. .'  /--.--\  `. .'___               //
//          ."" '<  `.___\_<|>_/___.' >' "".             //
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |           //
//         \  \ `_.   \_ __\ /__ _/   .-` /  /           //
//     =====`-.____`.___ \_____/___.-`___.-'=====        //
//                       `=---='                         //
//                                                       //
//                                                       //
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~       //
//                                                       //
//                 God-He Bless All.                     //
//           This Code Will Never Explode.               //
//                                                       //
//                                                       //
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<vector>
#include<cassert>
#define rep(i,a,b) for(int i=(a),i##_end_=(b);i<=i##_end_;i++)
#define dwn(i,a,b) for(int i=(a),i##_end_=(b);i>=i##_end_;i--)
using namespace std;
const int Size=1<<16;
char buffer[Size],*head,*tail;
inline char Getchar() {
    if(head==tail) {
        int l=fread(buffer,1,Size,stdin);
        tail=(head=buffer)+l;
    }
    if(head==tail) return -1;
    return *head++;
}
inline int read() {
    int x=0,f=1;char c=Getchar();
    for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
    return x*f;
}
typedef long long ll;
const int maxn=200010;
vector<int>G[maxn];
int n,p[maxn],vis[maxn],A[maxn],B[maxn],in[maxn],first[maxn],nxt[maxn],to[maxn],e;
void AddEdge(int u,int v) {G[v].push_back(u);in[v]++;to[++e]=v;nxt[e]=first[u];first[u]=e;}
int Q[maxn],S[maxn];
int work(int x) {
	memset(S,0,sizeof(S));
	int y=x;
	do {
		y=p[y];
		if(B[y]>=0) break;
		rep(i,0,G[y].size()-1) S[B[G[y][i]]]++;
		B[y]=0;while(S[B[y]]) B[y]++;
		rep(i,0,G[y].size()-1) S[B[G[y][i]]]--;
	}while(y!=x);
	rep(x,1,n) {
		rep(i,0,G[x].size()-1) S[B[G[x][i]]]++;
		rep(i,0,B[x]-1) if(!S[i]) return 0;
		if(S[B[x]]) return 0;
		rep(i,0,G[x].size()-1) S[B[G[x][i]]]--;
	}
//	rep(i,1,n) printf("%d%c",B[i],i==n?'\n':' ');
	return 1;
}
int solve() {
	memset(A,-1,sizeof(A));
	int l=1,r=0;
	rep(i,1,n) if(!in[i]) Q[++r]=i,A[i]=0;
	while(l<=r) {
		int x=Q[l++];vis[x]=1;
		for(int i=first[x];i;i=nxt[i]) if(!(--in[to[i]])) Q[++r]=to[i];
		rep(i,0,G[x].size()-1) S[A[G[x][i]]]++;
		A[x]=0;while(S[A[x]]) A[x]++;
		rep(i,0,G[x].size()-1) S[A[G[x][i]]]--;
	}
	rep(i,1,n) if(!vis[i]) {
		rep(j,0,G[i].size()-1) S[A[G[i][j]]]++;
		int v=0;while(S[v]) v++;
		int v2=v+1;while(S[v2]) v2++;
		rep(j,0,G[i].size()-1) S[A[G[i][j]]]--;
		rep(j,1,n) B[j]=A[j];
		B[i]=v;if(work(i)) return 1;
		rep(j,1,n) B[j]=A[j];
		B[i]=v2;if(work(i)) return 1;
		return 0;
	}
}
int main() {
	n=read();
	rep(i,1,n) AddEdge(i,p[i]=read());
	puts(solve()?"POSSIBLE":"IMPOSSIBLE");
	return 0;
}

Submission Info

Submission Time
Task F - Namori Grundy
User wzj52501
Language C++14 (GCC 5.4.1)
Score 800
Code Size 3995 Byte
Status AC
Exec Time 36 ms
Memory 17408 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 4 ms 11520 KB
example1 AC 4 ms 11520 KB
example2 AC 4 ms 11520 KB
example3 AC 4 ms 11520 KB
loop0 AC 26 ms 15616 KB
loop1 AC 26 ms 15616 KB
loop10 AC 27 ms 15616 KB
loop11 AC 27 ms 15616 KB
loop12 AC 27 ms 15616 KB
loop13 AC 28 ms 15616 KB
loop14 AC 27 ms 15616 KB
loop15 AC 28 ms 15616 KB
loop16 AC 26 ms 15616 KB
loop17 AC 26 ms 15616 KB
loop18 AC 31 ms 15616 KB
loop19 AC 29 ms 15616 KB
loop2 AC 27 ms 15616 KB
loop3 AC 26 ms 15616 KB
loop4 AC 28 ms 15616 KB
loop5 AC 26 ms 15616 KB
loop6 AC 26 ms 15616 KB
loop7 AC 27 ms 15616 KB
loop8 AC 33 ms 15616 KB
loop9 AC 29 ms 15616 KB
loopex0 AC 30 ms 15744 KB
loopex1 AC 32 ms 15616 KB
loopex10 AC 29 ms 15616 KB
loopex11 AC 27 ms 15616 KB
loopex12 AC 30 ms 15616 KB
loopex13 AC 31 ms 15616 KB
loopex14 AC 29 ms 15616 KB
loopex15 AC 30 ms 15616 KB
loopex16 AC 30 ms 15744 KB
loopex17 AC 30 ms 15616 KB
loopex18 AC 31 ms 15744 KB
loopex19 AC 30 ms 15616 KB
loopex2 AC 30 ms 15616 KB
loopex20 AC 29 ms 15616 KB
loopex21 AC 30 ms 15744 KB
loopex22 AC 29 ms 15744 KB
loopex23 AC 29 ms 15616 KB
loopex3 AC 29 ms 15616 KB
loopex4 AC 30 ms 15616 KB
loopex5 AC 32 ms 15744 KB
loopex6 AC 31 ms 15616 KB
loopex7 AC 30 ms 15616 KB
loopex8 AC 29 ms 15744 KB
loopex9 AC 31 ms 15744 KB
rand0 AC 11 ms 12800 KB
rand1 AC 16 ms 13440 KB
rand2 AC 6 ms 12160 KB
rand3 AC 26 ms 15232 KB
rand4 AC 36 ms 17408 KB
rand5 AC 17 ms 13696 KB
rand6 AC 9 ms 12544 KB
rand7 AC 5 ms 11776 KB
rand8 AC 24 ms 14720 KB
rand9 AC 5 ms 11776 KB
small0 AC 4 ms 11520 KB
small1 AC 4 ms 11520 KB
small2 AC 4 ms 11520 KB
small3 AC 4 ms 11520 KB
small4 AC 4 ms 11520 KB
small5 AC 4 ms 11520 KB
small6 AC 4 ms 11520 KB
small7 AC 4 ms 11520 KB
small8 AC 4 ms 11520 KB
small9 AC 4 ms 11520 KB