题解:
tips:用cout,cin会TLE
没用带权的并查集
#include<iostream>
#include<cstdio>
using namespace std;
int fa[150005];
int find(int x)
{
if(x == fa[x]) return x;
return fa[x] = find(fa[x]);
}
int main (){
int N,K;
scanf("%d %d",&N,&K);
for(int i=1;i<=3*N;i++)
fa[i] = i;
int cnt = 0;
int a ,b,c;
for(int i = 1;i<=K;i++)
{
scanf("%d %d %d",&a,&b,&c);
if(b>N||c>N)
{
cnt++;
continue;
}
int x1 = find(b),x2 = find(b+N),x3 = find (b+2*N);
int y1 = find(c),y2 = find(c+N),y3 = find (c+2*N);
if(a == 1)
{
if((x1 == y2)||(y1== x2))
cnt++;
else
{
fa[x1] = y1;
fa[x2] = y2;
fa[x3] = y3;
}
}
if(a == 2){
if((x1 == y1)||(x1 == y2))
cnt++;
else
{
fa[y1] = x2;
fa[y3] = x1;
fa[x3] = y2;
}
}
}
printf("%d\n",cnt);
return 0 ;
}
//cincout会TLE