QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#437476#8686. Zoo Managementmonster_hunterWA 3ms31120kbC++143.3kb2024-06-09 11:20:222024-06-09 11:20:22

Judging History

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

  • [2024-06-09 11:20:22]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:31120kb
  • [2024-06-09 11:20:22]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define LL long long
struct edge{
	LL to,nt;
}a[800005],b[1600005];
LL nxt[800005],nxt1[800005];
LL n,i,j,k,m,dfs_clock=0,cnt=0,cnt1=0,cnt2=0,x,y;
LL num[400005],val[400005],dfn[400005],low[400005],deg[400005],deg1[400005];
LL ans[400005],tmp[400005];
LL useless[1000005];
stack<LL> st;
bool vis[400005],flag[800005],flag1[400005];
LL tmp2[1000005];
const LL mod=19260817;
void add1(LL x,LL y){
//	printf("%lld %lld\n",x,y);
	b[++cnt2].to=y;b[cnt2].nt=nxt1[x];nxt1[x]=cnt2;
}
void dfs(LL father,LL x){
	dfn[x]=low[x]=++dfs_clock;
	vis[x]=true;
	st.push(x);
	for(LL i=nxt[x];i;i=a[i].nt){
		if(vis[a[i].to]==false){
			dfs(x,a[i].to);
			low[x]=min(low[x],low[a[i].to]);
		}
		else if(a[i].to!=father) low[x]=min(low[x],dfn[a[i].to]);
	}
	//printf("%lld %lld %lld\n",x,dfn[x],low[x]);
	if(dfn[father]==low[x]){
		cnt1++;
		while(!st.empty() && st.top()!=x){
			deg[cnt1]++;
			add1(n+cnt1,st.top());
			add1(st.top(),n+cnt1);
			st.pop();
		}
		deg[cnt1]++;
		add1(n+cnt1,x);
		add1(x,n+cnt1);
		st.pop();
		deg[cnt1]++;
		add1(n+cnt1,father);
		add1(father,n+cnt1);
	}
}
void add(LL x,LL y){
	a[++cnt].to=y;a[cnt].nt=nxt[x];nxt[x]=cnt;
}
void dfs2(LL x){
	if(x<=n) flag[x]=true,flag1[x]=true,ans[++k]=x;
	for(LL i=nxt1[x];i;i=b[i].nt){
		if(b[i].to>n && deg[b[i].to-n]<=2) continue;
		else if(b[i].to<=n && flag[b[i].to]==true) continue;
		else dfs2(b[i].to); 
	}
}
int main(){
	tmp2[0]=1;
	for(i=1;i<=1e6;i++)
	  tmp2[i]=tmp2[i-1]*1000000%mod;
	scanf("%lld%lld",&n,&m);
	for(i=1;i<=n;i++)
	  scanf("%lld%lld",&num[i],&val[i]);
	for(i=1;i<=m;i++){
		scanf("%lld%lld",&x,&y);
		add(x,y);
		add(y,x);
	}
	memset(vis,false,sizeof(vis));
	for(i=1;i<=n;i++)
	  if(vis[i]==false){
	  	dfs(0,i);
	  }
	memset(flag,false,sizeof(flag));
	memset(vis,false,sizeof(vis));
	for(i=1;i<=n;i++){
		if(flag[i]==false){
			//printf("%lld ",i);
			k=0;
			dfs2(i);
			
			bool flag2=false; 
			for(j=1;j<=k;j++){
				for(LL x=nxt[ans[j]];x;x=a[x].nt)
				  if(flag1[a[x].to]==true) deg1[j]++;
				//printf("%lld %lld\n",j,deg1[j]);
				if(deg1[j]!=0 && deg1[j]!=2){
					flag2=true;break;
				}  
			}
		//	printf("%lld %d\n",k,flag2);
			if(flag2==false){
				if(k==1){
					if(num[i]!=val[i]){
						printf("impossible");
						return 0;
					}
					else{
						flag1[i]=false;continue;
					}
				}
				LL stat=0,tmp1=i;
				while(stat==0 || tmp1!=i){	
				  for(j=nxt[i];j;j=a[j].nt)
				    if(flag1[a[j].to]==true && a[j].to!=stat){
				  	  stat=tmp1,tmp1=a[j].to;
				  	  tmp[++tmp[0]]=a[j].to;
				    }
				}
				LL num1=0,val1=0;
				for(j=1;j<=k;j++){
					num1=num1*1000000ll%mod+num[tmp[j]]%mod;
					num1%=mod; 
					val1=val1*1000000ll%mod+val[tmp[j]]%mod;
					val1%=mod;
				}
				bool flag3=false;
				for(j=1;j<=k;j++){
					val1=val1-num[val[j]]*tmp2[k-1]%mod+val[tmp[j]];
					val1=(val1%mod+mod)%mod;
					if(val1==num1){
						flag3=true;break;
					}
				}
				if(flag3==false){
					printf("impossible");
					return 0;
				}
			}
			else{
				for(j=1;j<=k;j++)
				  useless[num[ans[j]]]++,useless[val[ans[j]]]--;
				for(j=1;j<=k;j++)
				  if(useless[num[ans[j]]]!=0){
				  	printf("impossible");
				  	return 0;
				  }
			}
			for(j=1;j<=k;j++)
			  flag1[ans[j]]=false;
		}
	}
	printf("possible");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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'