标签:des style class blog c code
题目链接:
http://poj.org/problem?id=1182
参考大神岐哥链接:
http://www.cnblogs.com/wuyiqi/archive/2011/08/24/come__in.html
题目为:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 42412 | Accepted: 12366 |
Description
Input
Output
Sample Input
100 7 1 101 1 2 1 2 2 2 3 2 3 3 1 1 3 2 3 1 1 5 5
Sample Output
3
Source
#include<cstdio> const int maxn=50000+10; int root[maxn],rank[maxn]; int n,k; void FBI() { for(int i=1;i<=n;i++) { root[i]=i; rank[i]=0; } } int findroot(int x) { if(root[x]!=x) { int t=root[x]; root[x]=findroot(root[x]); rank[x]=(rank[x]+rank[t])%3; } return root[x]; } int merge(int op,int x,int y) { if(x>n||y>n) return 0; if(op==1&&x==y) return 0; int fx=findroot(x); int fy=findroot(y); if(fx!=fy)//说明这句话一定是对的 { root[fy]=fx; rank[fy]=(rank[x]+op-rank[y]+3)%3; return 1; } else { if((rank[y]-rank[x]+3)%3!=op) return 0; else return 1; } } int main() { int i,pos; int op,x,y; scanf("%d%d",&n,&k); FBI(); pos=0; for(i=1;i<=k;i++) { scanf("%d%d%d",&op,&x,&y); int flag=merge(op-1,x,y); if(!flag) pos++; } printf("%d\n",pos); return 0; }
标签:des style class blog c code
原文地址:http://blog.csdn.net/u014303647/article/details/26625097