开三倍的数组,分别是:同类域 捕食域 天敌域
#include <iostream>
//同类 捕食 天敌
using namespace std;
const int N = 50010;
int f[3 * N];
int res;
int find(int x)
{
return f[x] = (f[x] == x ? x : find(f[x]));
}
void merge(int a , int b)
{
a = find(a) , b = find(b);
f[a] = b;
}
int main()
{
int n , k;
cin >> n >> k;
for(int i = 1 ; i <= 3 * n ; i++) f[i] = i;
while(k--)
{
int d , a , b;
cin >> d >> a >> b;
if(a > n || b > n) res ++;
else if(d == 1)
{
if(find(a) == find(b + n) || find(a) == find(b + n + n) ) res++;
else merge(a , b) , merge(a + n , b + n) , merge(a + n + n , b + n + n);
}
else if(d == 2)
{
if(a == b || find(a) == find(b) || find(a) == find(b + n)) res++;
else merge(a , b + n + n) , merge(a + n , b) , merge(a + n + n , b + n);
}
}
cout << res << endl;
return 0;
}