poj1182食物链

来源:互联网 发布:软件开源有什么用 编辑:程序博客网 时间:2024/05/17 01:05
#include<stdio.h>#include<string.h>#include<stdlib.h>int p[100005],r[100005],w[1000005],d,t;void init(int n){for(int i=0;i<=n;i++)p[i]=i,w[i]=0,r[i]=0;}int find(int x){if(x==p[x]) return x;int tmp=p[x];p[x]=find(p[x]);w[x]=(w[x]+w[tmp])%3;return p[x];}void combine(int a,int b){int t1=find(a);int t2=find(b);if(d==1){if(t1!=t2){if(r[t1]>r[t2]){p[t2]=t1;w[t2]=(w[a]-w[b]+3)%3;}else if(r[t1]==r[t2]){r[t1]++;p[t2]=t1;w[t2]=(w[a]-w[b]+3)%3;}else{p[t1]=t2;w[t1]=(3-(w[a]-w[b])%3)%3;}}else{if(w[a]!=w[b])t++;}}else{if(t1!=t2){if(r[t1]>r[t2]){p[t2]=p[t1];w[t2]=(w[a]-w[b]-1+3)%3;}else if(r[t1]==r[t2]){r[t1]++;p[t2]=t1;w[t2]=(w[a]-w[b]-1+3)%3;}else{p[t1]=t2;w[t1]=(3-(w[a]-w[b]-1)%3)%3;}}else{if((w[a]-w[b]+3)%3!=1)t++;}}}int main(){int n,m,i,j,k,a,b,c;t=0;scanf("%d%d",&n,&m);init(n);while(m--){scanf("%d%d%d",&d,&b,&c);if(b>n||c>n) {t++;continue;}if(d==2&&b==c) {t++;continue;}else(combine(b,c));}printf("%d\n",t);return 0;}

0 0
原创粉丝点击