QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#549433#8686. Zoo Managementucup-team052WA 2ms15960kbC++233.2kb2024-09-06 15:30:152024-09-06 15:30:15

Judging History

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

  • [2024-09-06 15:30:15]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:15960kb
  • [2024-09-06 15:30:15]
  • 提交

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();
		}
		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]){
					vector<int>v;
					int cnt_e=0;
					do{
						v.pb(st[top]);
						for(auto&x:e[st[top]]){
							if(dfn[x]<dfn[st[top]]){
								++cnt_e;
							}
						}
					}while(st[top--]!=x);
					v.pb(u);
					if(SZ(v)%2==0||cnt_e>SZ(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;
}
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){
				vector<int>v3(v2);
				v3.pop_back();
				v3.insert(v3.begin(),v2.back());
				vector<int>v4(v2);
				v4.erase(v4.begin());
				v4.pb(v2[0]);
				if(v2!=v1&&v3!=v1&&v4!=v1){
					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);
	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: 2ms
memory: 11856kb

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: 15952kb

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

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'