#include<bits/stdc++.h>
using namespace std;
int n,k,ans = 0,d,x,y;
int father[50005],rala[50005];
int getfather(int a){
if(father[a] == a){
return a;
}else{
int dad = father[a];
rala[a] = (rala[a]+rala[dad])%3;
father[a] = getfather(dad);
return father[a];
}
}
void merge(int a,int b,int fa,int fb,int d){
father[fb] = fa;
rala[fb] = (rala[a]-rala[b]+d+3)%3;
}
int main(){
//freopen("P2024_2.in","r",stdin);
scanf("%d%d",&n,&k);
for(int i = 1; i <= n; i++){
father[i] = i;
//rala[i] = 0;
}
for(int i = 1; i <= k; i++){
scanf("%d%d%d",&d,&x,&y);
if(d == 2 && x == y){
ans++;
}else if(x > n || y > n){
ans++;
}else{
int fx,fy;
fx = getfather(x);
fy = getfather(y);
if(fx != fy){
merge(x,y,fx,fy,d-1);
}else{
if(rala[x] != rala[y] && d == 1){
ans++;
}else if((rala[y]-rala[x]+3)%3 != d-1){
ans++;
}
}
}
}
printf("%d",ans);
return 0;
}
只过了1,3,9,其他的是WA。带权并查集。求调。