#include <iostream>
#include <stdio.h>
using namespace std;
int par[50000],kin[50000];
void init(int n){
int i;
for(i=0;i<n;i++){
par[i] = i;
kin[i] = 0;
}
}
void uniun(int xroot,int yroot,int x,int y,int o){
par[yroot] = xroot;
kin[yroot] = (kin[x]-kin[y]-o+3)%3;
}
int find(int x){
if(par[x] == x)return x;
int xroot;
xroot = find(par[x]);
kin[x] = (kin[par[x]]+kin[x]+3)%3;
par[x] = xroot;
return xroot;
}
int main(){
int n,k;
int x,y,o;
int count = 0;
scanf("%d%d",&n,&k);
init(n);
while(k--){
scanf("%d%d%d",&o,&x,&y);
x--;
y--;
if(x>=n||y>=n){
count++;
continue;
}
int xroot = find(x);
int yroot = find(y);
if(xroot == yroot){
int u = (kin[x] - kin[y]+3)%3;
if(u==0 && o == 1){
continue;
}
if(u == 1 && o == 2){
continue;
}
count++;
}else{
uniun(xroot,yroot,x,y,o-1);
}
}
printf("%d\n",count);
return 0;
}
一般父类便为更节点更新节点时 kin = 0