poj 1182用向量的思考模式
不会写果断看答案http://cavenkaka.iteye.com/blog/1489588
#include<stdio.h>
#include<string.h>
#define N 50010
int father[N],link[N];
int find(int x) {
if(x!=father[x]) {
int h=father[x];
father[x]=find(father[x]);
link[x]=(link[x]+link[h])%3;
}
return father[x];
}
void insert(int x,int y,int d) {
int xx=find(x);
int yy=find(y);
father[xx]=yy;
link[xx]=(link[y]-link[x]+3+d)%3;
}
int main() {
int n,k,i,m,a,b,c,f1,f2;
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
father[i]=i;
memset(link,0,sizeof(link));
m=0;
while(k--) {
scanf("%d%d%d",&a,&b,&c);
if(b>n||c>n||(a==2&&b==c)) {
m++;
continue;
}
f1=find(b);
f2=find(c);
if(f1==f2) {
if((link[b]-link[c]+3)%3!=a-1)
m++;
}
else
insert(b,c,a-1);
}
printf("%d\n",m);
return 0;
}