QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#549452#8686. Zoo Managementucup-team052WA 6ms21428kbC++233.6kb2024-09-06 15:45:362024-09-06 15:45:36

Judging History

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

  • [2024-09-06 15:45:36]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:21428kb
  • [2024-09-06 15:45:36]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
using namespace std;
const int N=400005,maxN=1000005;
namespace solver{
	int n;
	vector<int>e[N];
	int dfn[N],low[N],idx;
	int st[N],top;
	void init(int n0){
		rep(i,1,n){
			dfn[i]=low[i]=0;
			e[i].clear();
		}
		idx=0;
		top=0;
		n=n0;
	}
	void link(int u,int v){
		e[u].pb(v),e[v].pb(u);
	}
	int flag;
	void tarjan(int u){
		dfn[u]=low[u]=++idx,st[++top]=u;
		for(auto&x:e[u]){
			if(!dfn[x]){
				tarjan(x),low[u]=min(low[u],low[x]);
				if(low[x]>=dfn[u]){
					int cnt_v=0;
					int cnt_e=0;
					do{
						++cnt_v;
						for(auto&x:e[st[top]]){
							if(dfn[x]<dfn[st[top]]){
								++cnt_e;
							}
						}
					}while(st[top--]!=x);
					++cnt_v;
					if(cnt_v%2==0||cnt_e>cnt_v){
						flag=1;
					}
				}
			}
			else low[u]=min(low[u],dfn[x]);
		}
		
	}
	bool solve(){
		flag=0;
		rep(i,1,n)if(!dfn[i])tarjan(i);
		return flag;
	}
}
struct fenwick{
	int tr[maxN];
	void add(int x,int y){for(int i=x;i<maxN;i+=i&-i)tr[i]+=y;}
	int query(int x){int y=0;for(int i=x;i;i&=i-1)y+=tr[i];return y;}
}fwk;
int n,m,b0[N],b1[N];
vector<int>e[N];
int dfn[N],low[N],st[N],top,idx;
int seq[N],len,bel[N],cnt;
int rk[N];
void GG(){puts("impossible"),exit(0);}
int calc(const vector<int>&v){
	int ret=0;
	per(i,SZ(v)-1,0){
		ret^=fwk.query(v[i])&1;
		fwk.add(v[i],1);
	}
	rep(i,0,SZ(v)-1){
		fwk.add(v[i],-1);
	}
	return ret;
}
const int B=19260817;
const int P=998244853;
int pw[N];
vector<int>get_h(const vector<int>&v){
	vector<int>h(SZ(v)+1);
	per(i,SZ(v)-1,0){
		h[i]=(1LL*h[i+1]*B+v[i])%P;
	}
	return h;
}
int ghs(vector<int>&h,int l,int r){
	return (h[l]-1LL*h[r+1]*pw[r-l+1]%P+P)%P;
}
void tarjan(int u,int fa){
	dfn[u]=low[u]=++idx,st[++top]=u;
	for(auto&x:e[u])if(x!=fa){
		if(!dfn[x])tarjan(x,u),low[u]=min(low[u],low[x]);
		else low[u]=min(low[u],dfn[x]);
	}
	if(dfn[u]==low[u]){
		++cnt;
		len=0;
		do{
			bel[st[top]]=cnt;
			seq[++len]=st[top];
			rk[st[top]]=len;
		}while(st[top--]!=u);
		solver::init(len);
		int cnt_e=0;
		rep(i,1,len)for(auto&x:e[seq[i]]){
			if(x<seq[i]){
				++cnt_e;
				solver::link(rk[x],rk[seq[i]]);
			}
		}
		if(len==1){
			if(b0[seq[1]]!=b1[seq[1]]){
				GG();
			}
		}else{
			vector<int>v1,v2;
			rep(i,1,len){
				v1.pb(b0[seq[i]]);
				v2.pb(b1[seq[i]]);
			}
			if(cnt_e==len){
				auto h1=get_h(v1);
				auto h2=get_h(v2);
				bool same=0;
				rep(i,0,SZ(v1)-1){
					if(ghs(h1,0,i)==ghs(h2,SZ(v2)-1-i,SZ(v2)-1)&&ghs(h1,i+1,SZ(v1)-1)==ghs(h2,0,SZ(v2)-1-i-1)){
						same=1;
						break;
					}
				}
				if(!same)GG();
			}else{
				if(solver::solve()){
					sort(v1.begin(),v1.end());
					sort(v2.begin(),v2.end());
					if(v1!=v2)GG();
				}else{
					if(calc(v1)!=calc(v2)){
						sort(v1.begin(),v1.end());
						sort(v2.begin(),v2.end());
						if(v1!=v2)GG();
						bool flag=0;
						rep(i,1,SZ(v1)-1){
							if(v1[i]==v1[i-1]){
								flag=1;
							}
						}
						if(!flag){
							GG();
						}
					}else{
						sort(v1.begin(),v1.end());
						sort(v2.begin(),v2.end());
						if(v1!=v2)GG();
					}
				}
			}
		}
	}
}
int main(){
#ifdef xay5421
	freopen("a.in","r",stdin);
#endif
	ios::sync_with_stdio(0),cin.tie(0);
	pw[0]=1;
	rep(i,1,N-1)pw[i]=1LL*pw[i-1]*B%P;
	cin>>n>>m;
	rep(i,1,n){
		cin>>b0[i]>>b1[i];
	}
	rep(i,1,m){
		int u,v;
		cin>>u>>v;
		e[u].pb(v);
		e[v].pb(u);
	}
	rep(i,1,n)if(!dfn[i])tarjan(i,0);
	puts("possible");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 19272kb

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: 6ms
memory: 19544kb

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: 3ms
memory: 19716kb

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: 5ms
memory: 15492kb

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: 3ms
memory: 19708kb

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: 3ms
memory: 21428kb

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: 6ms
memory: 18948kb

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: 0ms
memory: 21204kb

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: 5ms
memory: 16680kb

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: 2ms
memory: 17292kb

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: 5ms
memory: 16220kb

input:

1 0
1000000 1000000

output:

possible

result:

ok single line: 'possible'

Test #12:

score: 0
Accepted
time: 5ms
memory: 16648kb

input:

2 0
1000000 987654
987654 1000000

output:

impossible

result:

ok single line: 'impossible'

Test #13:

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

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:

possible

result:

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