QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#426436#8686. Zoo Managementkkkgjyismine4WA 6ms40700kbC++143.0kb2024-05-31 11:09:092024-05-31 11:09:10

Judging History

你现在查看的是最新测评结果

  • [2024-05-31 11:09:10]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:40700kb
  • [2024-05-31 11:09:09]
  • 提交

answer

#include<bits/stdc++.h>
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
int n,m,tt,a[400050],b[400050],U[400500],V[400500];
struct BIT{
	int ct[400050];
	void init(){for(int i=1;i<=tt;++i)ct[i]=0;}
	void add(int p){for(int i=p;i<=tt;i+=(i&-i))++ct[i];}
    int qry(int p){
    	int ans=0;
    	for(int i=p;i;i&=(i-1))ans+=ct[i];
    	return ans;
	}
}fwk;
int bl[400050],dfn[400050],low[400050],tot,stk[400050],tail,col;
int hd[400050],nxt[800050],tt1=1,to[800050];
void add(int u,int v){
	nxt[++tt1]=hd[u],hd[u]=tt1,to[tt1]=v;
	nxt[++tt1]=hd[v],hd[v]=tt1,to[tt1]=u;
}
int vis[400050];
vector<int>vec[400500];
vector<pii>e[400050];
void dfs(int u,int from){
	dfn[u]=low[u]=++tot,stk[++tail]=u;
	for(int i=hd[u],v;i;i=nxt[i]){
		v=to[i];if((i^1)==from)continue;
		if(!dfn[v])dfs(v,i),low[u]=min(low[u],low[v]);
		else low[u]=min(low[u],dfn[v]);
	}
	if(dfn[u]==low[u]){
		++col;
		while(1){
			int p=stk[tail--];bl[p]=col,vec[col].pb(p);
			if(p==u)break;
		}
	}
}
vector<int>va,vb,vc,vd;
int cir[400500],dep[400500],fa[400050];
void dfs1(int u){
	vis[u]=1;va.pb(a[u]),vb.pb(b[u]);
	for(int i=hd[u],v;i;i=nxt[i]){
		v=to[i];
		if(vis[v]||bl[v]!=bl[u])continue;
		dep[v]=dep[u]+1,fa[v]=u;
		dfs1(v);
	}
}
int Fl;
void dfs2(int u){
	for(int i=hd[u],v;i;i=nxt[i]){
		v=to[i];
		if(dep[v]<dep[u]||bl[u]!=bl[v])continue;
		dfs2(v),cir[u]+=cir[v];
	}
	if(fa[u]&&cir[u]>1)Fl=1;
}
void No(){
	puts("impossible");
	exit(0);
}
void Yes(){
	puts("possible");
	exit(0);
}
int N,A[400050];
void cg(vector<int>&a){
	N=a.size();
	for(int i=0;i<N;++i)A[i]=a[i];
	int i=0,j=1,k=0;
	while(i<N&&j<N&&k<N){
		if(A[(i+k)%N]==A[(j+k)%N])++k;
		else{
			if(A[(i+k)%N]>A[(j+k)%N])i+=k+1;else j+=k+1;
			if(i==j)++j;k=0;
		}
	}
	for(int p=0;p<N;++p)a[p]=A[(i+p)%N];
}
int id[1005000];
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;++i)scanf("%d%d",&a[i],&b[i]);
	for(int i=1;i<=m;++i){
		int u,v;scanf("%d%d",&u,&v);
		add(u,v),U[i]=u,V[i]=v;
	}
	for(int i=1;i<=n;++i)if(!dfn[i])dfs(i,-1);
	for(int i=1;i<=m;++i)if(bl[U[i]]==bl[V[i]])e[bl[U[i]]].pb(make_pair(U[i],V[i]));
	for(int i=1;i<=col;++i){
		va.clear(),vb.clear(),dfs1(vec[i][0]);int ct=0;
		vc=va,vd=vb;
		sort(va.begin(),va.end());
		sort(vb.begin(),vb.end());  
		for(int j=0;j<va.size();++j)if(va[j]!=vb[j])No();
		va=vc,vb=vd;
		for(auto v:e[i])
		  if(abs(dep[v.fi]-dep[v.se])>1){
		  	int a=v.fi,b=v.se;
		  	if(dep[a]<dep[b])swap(a,b);
		  	++cir[a],--cir[b];
		  	++ct;
		  }
		Fl=0,dfs2(vec[i][0]);
		if(!ct||Fl)continue;
		if(ct==1){
			cg(va),cg(vb);
			for(int j=0;j<va.size();++j)if(va[j]!=vb[j])No();
			continue;
		}
		vc=va,sort(va.begin(),va.end());
		for(int j=0;j+1<va.size();++j)if(va[j]==va[j+1]){Fl=1;break;}
		if(Fl)continue;va=vc;
		for(int j=0;j<vb.size();++j)id[vb[j]]=j+1;
		tt=va.size();fwk.init();
		int ans=0;
		for(int j=0;j<va.size();++j){
			ans^=(fwk.qry(tt)-fwk.qry(id[va[j]]));
			fwk.add(id[va[j]]);
		}
		if(ans&1)No();
	}Yes();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6ms
memory: 40700kb

input:

6 7
1 1
2 2
3 3
1 2
2 3
3 1
1 2
2 3
1 3
3 4
4 5
5 6
4 6

output:

possible

result:

ok single line: 'possible'

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 36588kb

input:

5 6
10 10
20 20
30 30
40 50
50 40
1 2
2 3
1 3
3 4
3 5
4 5

output:

possible

result:

wrong answer 1st lines differ - expected: 'impossible', found: 'possible'