作者:
fangy
,
2023-03-15 21:36:23
,
所有人可见
,
阅读 3
#include <iostream>
using namespace std;
const int N = 100010;
int n, m;
int p[N], d[N];
int find(int x)
{
if (p[x] != x)
{
int t = find(p[x]);
d[x] += d[p[x]];
p[x] = t;
}
return p[x];
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; ++i) p[i] = i;
int res = 0;
while (m--)
{
int op, x, y;
scanf("%d%d%d", &op, &x, &y);
if (x > n || y > n) res++;
else
{
int px = find(x), py = find(y);
if (op == 1)
{
if (px == py && (d[x] - d[y]) % 3) res++;
else if (px != py) p[py] = px, d[py] = (d[x] - d[y] + 3) % 3;
}
else
{
if (px == py && (d[y] - d[x] - 1) % 3) res++;
else if (px != py) p[py] = px, d[py] = (d[x] - d[y] + 1) % 3;
}
}
}
cout << res << endl;
return 0;
}