AcWing 240. 食物链
原题链接
中等
作者:
wjie
,
2020-08-07 15:05:41
,
所有人可见
,
阅读 469
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 15e4 + 5;
int fa[N];
int findSet(int x)
{
if (x == fa[x]) return x;
return fa[x] = findSet(fa[x]);
}
void unionSet(int x, int y)
{
x = findSet(x), y = findSet(y);
if (x == y) return;
fa[x] = y;
}
int main()
{
int n, k, res = 0;
scanf("%d %d", &n, &k);
for (int i = 1; i <= 3*n; ++i) fa[i] = i;
for (int i = 0; i < k; ++i)
{
int d, x, y;
scanf("%d %d %d", &d, &x, &y);
if (x > n || y > n) res++;
else if (d == 1)
{
if (findSet(x) == findSet(y+n) || findSet(x) == findSet(y+2*n)) res++;
else
{
unionSet(x, y);
unionSet(x+n, y+n);
unionSet(x+2*n, y+2*n);
}
}
else if (d == 2)
{
if (findSet(x) == findSet(y) || findSet(x) == findSet(y+n)) res++;
else
{
unionSet(x, y+2*n);
unionSet(x+n, y);
unionSet(x+2*n, y+n);
}
}
// cout << i << " " << res << endl;
}
printf("%d", res);
return 0;
}