题目来源:http://poj.org/problem?id=1182
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 45355 | Accepted: 13229 |
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 Max=50001;
int n,k,D,x,y,father[Max],r[Max],ans=0;
int find(int t){
if(t==father[t]) return t;
int temp=find(father[t]);
r[t]=(r[t]+r[father[t]])%3;
father[t]=temp;
return temp;
}
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
father[i]=i; r[i]=0;
}
while(k--){
scanf("%d%d%d",&D,&x,&y);
if(x>n||y>n||D==2&&x==y) {
ans++; continue; //条件2 3
}
int tempx=find(x),tempy=find(y);
if(tempx==tempy){
if((r[x]-r[y]+3)%3!=D-1) ans++;
}
else {
father[tempx]=tempy; r[tempx]=(D+2-r[x]+r[y])%3;
}
}
printf("%d\n",ans);
return 0;
} 原文:http://blog.csdn.net/mummyding/article/details/39078043