QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#426437#8686. Zoo Managementkkkgjyismine4WA 4ms34544kbC++143.0kb2024-05-31 11:14:522024-05-31 11:14:52

Judging History

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

  • [2024-05-31 11:14:52]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:34544kb
  • [2024-05-31 11:14:52]
  • 提交

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;
int vis1[400500];
void dfs2(int u){
	vis1[u]=1;
	for(int i=hd[u],v;i;i=nxt[i]){
		v=to[i];
		if(vis1[v]||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[va[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[vb[j]]));
			fwk.add(id[vb[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: 4ms
memory: 34544kb

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: 0
Accepted
time: 0ms
memory: 22468kb

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:

impossible

result:

ok single line: 'impossible'

Test #3:

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

input:

25 32
10 10
20 30
30 20
40 40
50 60
60 70
70 50
80 90
90 130
100 100
110 120
120 110
130 80
140 160
150 170
160 140
170 150
180 190
190 180
200 200
200 200
220 220
230 230
240 240
250 250
1 25
1 3
2 25
2 3
3 25
3 4
4 5
5 6
5 7
6 7
6 10
8 9
8 10
9 10
10 11
11 13
10 12
12 13
10 14
14 15
15 16
16 17
14...

output:

impossible

result:

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