QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#455084#8686. Zoo ManagementNKheyuxiangTL 9ms38636kbC++142.7kb2024-06-25 19:41:432024-06-25 19:41:43

Judging History

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

  • [2024-06-25 19:41:43]
  • 评测
  • 测评结果:TL
  • 用时:9ms
  • 内存:38636kb
  • [2024-06-25 19:41:43]
  • 提交

answer

#include<bits/stdc++.h>
#define N 1000005
using namespace std;
bool flag=1;
int n,m,h[N],to[N],nxt[N],cnt; 
void jb(int u,int v){
	++cnt;
	to[cnt]=v;
	nxt[cnt]=h[u];
	h[u]=cnt;
}
int low[N],dfn[N],id[N],cc,c1,c2,hd,st[N];
void tarjan1(int u,int fa){
	low[u]=dfn[u]=++cc;
	st[++hd]=u;
	for(int i=h[u];i!=0;i=nxt[i]){
		int v=to[i];
		if(v==fa) continue;
		if(!dfn[v]){
			tarjan1(v,u);
			low[u]=min(low[u],low[v]);
		}
		else low[u]=min(low[u],dfn[v]);
	}
	if(low[u]>dfn[fa]){
		c1++;
		while(1){
			int x=st[hd--];
			id[x]=c1;
			if(x==u) break;
		}
	}
}
int bel[N],sz[N],es[N];
vector<int > vec[N];
void tarjan2(int u,int fa){
	low[u]=dfn[u]=++cc;
	st[++hd]=u;
	for(int i=h[u];i!=0;i=nxt[i]){
		int v=to[i];
		if(v==fa||id[v]!=id[u]) continue;
		if(!dfn[v]){
			tarjan2(v,u);
			if(low[v]>=dfn[u]){
				bel[u]=u;sz[++c2]=1;
				while(1){
					int x=st[hd--];
					bel[x]=u;sz[c2]++;
					for(int j=h[x];j!=0;j=nxt[j])
						if(bel[to[j]]==u) es[c2]++;
					if(x==v) break;
				}
			}
			low[u]=min(low[u],low[v]);
		}
		else low[u]=min(low[u],dfn[v]);
	}
}
int b[N],c[N],col[N],go[N];
bool tag[N];
int S[N],T[N],fail[N];
int f[N];
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d%d",&b[i],&c[i]);
	for(int i=1;i<=m;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		jb(u,v);jb(v,u);
	}
	for(int i=1;i<=n;i++) if(dfn[i]==0) tarjan1(i,0);
	for(int i=1;i<=n;i++) dfn[i]=low[i]=0,vec[id[i]].push_back(i);
	for(int i=1;i<=n&&flag;i++){
		if(dfn[i]!=0) continue;
		hd=cc=c2=0;tarjan2(i,0);
		int p=id[i];
		bool bol=0;
		if(c2==1&&sz[c2]==es[c2]){
			int len=0;
			for(int v:vec[p]) S[++len]=b[v],T[len]=c[v];
			for(int i=len+1;i<len*2;i++) S[i]=S[i-len];
			fail[0]=fail[1]=0;
			for(int i=2,k=0;i<=len;i++){
				while(k>0&&T[k+1]!=T[i]) k=fail[k];
				if(T[k+1]==T[i]) k++;
				fail[i]=k;
			}
			for(int i=1,k=0;i<2*len;i++){
				while(k>0&&T[k+1]!=S[i]) k=fail[k];
				if(T[k+1]==S[i]) k++;
				if(k==len){
					bol=1;
					break;
				}
			}
			if(bol==0){
				flag=0;
				while(1);
				break;
			}
		}
		else{
			for(int v:vec[p]) col[b[v]]=0;
			for(int v:vec[p]) col[b[v]]++,col[c[v]]--;
			for(int v:vec[p]){
				if(col[b[v]]!=0){
					flag=0;
					break;
				}
			}
			for(int v:vec[p]){
				if(col[b[v]]>0) bol=1;
				col[b[v]]++;
			}
			for(int j=1;j<=c2;j++)
				if(sz[j]<es[j]||sz[j]%2==0) bol=1;
			if(bol==1) continue;
			for(int v:vec[p]) col[c[v]]=v;
			for(int v:vec[p]) go[v]=col[b[v]];
			int yy=0;
			for(int v:vec[p]){
				if(tag[v]) continue;
				int len=1,x=go[v];tag[v]=1;
				while(x!=v) tag[x]=1,len++,x=go[x];
				yy+=(len%2==0);
			}
			if(yy%2==1) flag=0;
		}
	}
	if(flag==0) printf("im");
	printf("possible");
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 9ms
memory: 36476kb

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: 8ms
memory: 34488kb

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

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:

possible

result:

ok single line: 'possible'

Test #4:

score: 0
Accepted
time: 0ms
memory: 38636kb

input:

4 5
1 2
2 3
3 4
4 1
1 2
2 3
1 3
2 4
1 4

output:

possible

result:

ok single line: 'possible'

Test #5:

score: 0
Accepted
time: 9ms
memory: 38524kb

input:

26 31
70 170
210 230
160 130
180 110
40 200
140 120
90 30
220 70
230 140
190 80
30 180
80 60
170 50
50 90
200 20
10 10
100 210
150 150
110 220
20 160
60 190
120 40
130 100
1234 1234
666 666
88888 88888
1 2
2 3
3 4
4 5
5 6
6 7
1 7
2 8
8 9
2 9
3 10
10 11
3 11
10 12
12 13
13 14
14 15
10 15
3 16
16 17
3...

output:

possible

result:

ok single line: 'possible'

Test #6:

score: 0
Accepted
time: 0ms
memory: 28936kb

input:

23 29
70 170
210 230
160 130
180 110
40 200
140 120
90 30
220 70
230 140
190 80
30 180
80 60
170 50
50 90
200 20
10 10
100 210
150 150
110 160
20 220
60 190
120 40
130 100
1 2
2 3
3 4
4 5
5 6
6 7
1 7
2 8
8 9
2 9
3 10
10 11
3 11
10 12
12 13
13 14
14 15
10 15
3 16
16 17
3 17
3 18
18 19
19 20
20 21
3 2...

output:

impossible

result:

ok single line: 'impossible'

Test #7:

score: 0
Accepted
time: 4ms
memory: 29044kb

input:

23 29
70 170
210 230
160 130
180 110
40 200
140 120
90 30
30 70
230 140
190 80
30 180
80 60
170 50
50 90
200 20
10 10
100 210
150 150
110 160
20 30
60 190
120 40
130 100
1 2
2 3
3 4
4 5
5 6
6 7
1 7
2 8
8 9
2 9
3 10
10 11
3 11
10 12
12 13
13 14
14 15
10 15
3 16
16 17
3 17
3 18
18 19
19 20
20 21
3 21
...

output:

possible

result:

ok single line: 'possible'

Test #8:

score: 0
Accepted
time: 4ms
memory: 27400kb

input:

27 31
70 170
210 230
160 130
180 110
40 200
140 120
90 30
220 70
230 140
190 80
30 180
80 60
170 50
50 90
200 20
10 10
100 210
150 150
110 220
20 160
60 190
120 40
130 100
1234 1234
666 666
88888 88887
88887 88888
1 2
2 3
3 4
4 5
5 6
6 7
1 7
2 8
8 9
2 9
3 10
10 11
3 11
10 12
12 13
13 14
14 15
10 15
...

output:

impossible

result:

ok single line: 'impossible'

Test #9:

score: 0
Accepted
time: 4ms
memory: 28392kb

input:

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

output:

possible

result:

ok single line: 'possible'

Test #10:

score: 0
Accepted
time: 4ms
memory: 28388kb

input:

26 31
70 170
210 230
160 130
180 110
40 200
140 120
90 30
220 70
230 140
190 80
30 180
80 60
170 50
50 90
200 20
10 10
100 210
150 150
110 220
20 160
60 190
120 40
130 100
1234 1234
666 666
88888 88888
1 2
2 3
3 4
4 5
5 6
6 7
1 7
2 8
8 9
2 9
3 10
10 11
3 11
10 12
12 13
13 14
14 15
12 15
3 16
16 17
3...

output:

impossible

result:

ok single line: 'impossible'

Test #11:

score: 0
Accepted
time: 3ms
memory: 28560kb

input:

1 0
1000000 1000000

output:

possible

result:

ok single line: 'possible'

Test #12:

score: 0
Accepted
time: 0ms
memory: 27288kb

input:

2 0
1000000 987654
987654 1000000

output:

impossible

result:

ok single line: 'impossible'

Test #13:

score: -100
Time Limit Exceeded

input:

9 11
10 20
20 10
30 30
40 40
50 50
60 60
70 70
80 80
90 90
1 2
2 9
1 9
3 9
3 4
4 5
3 5
6 9
6 7
7 8
6 8

output:


result: