我的代码:
#include<bits/stdc++.h>
#define I using
#define AK namespace
#define IOI std
#define i_ak return
#define ioi 0
#define int long long
I AK IOI;
const int N=5e4+5;
int n,m,ans,fa[3*N];
int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
void Union(int x,int y){
x=find(x);
y=find(y);
if(x!=y)fa[y]=x;
}
bool same(int x,int y){
return find(x)==find(y);
}
signed main(){
cin>>m>>n;
for(int i=1;i<=3*m;i++)fa[i]=i;
for(int i=1,a,x,y;i<=n;i++){
cin>>a>>x>>y;
if(x>m||y>m){
ans++;
continue;
}
if(a==1){
if(same(x+n,y)||same(x,y+n))ans++;
else{
Union(x,y);
Union(x+m,y+m);
Union(x+2*m,y+2*m);
}
}
else{
if(same(x,y)||same(x,y+n))ans++;
else{
Union(x,y+m);
Union(x+m,y+2*m);
Union(x+2*m,y);
}
}
}
cout<<ans;
i_ak ioi;
}