感觉和题解一样啊,为啥错了啊qwq。
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5e4 + 10;
int p[N], d[N];
int find(int x)
{
if (p[x] != x)
{
int u = find(p[x]);
d[x] += d[p[x]];
p[x] = u;
}
return p[x];
}
int main()
{
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) p[i] = i;
int ans = 0;
while (k--)
{
int t, x, y;
scanf("%d%d%d", &t, &x, &y);
if (x > n || y > n) ans++;
else
{
int px = find(x), py = find(y);
if (t == 1)
{
if (px == py && d[x] != d[y]) ans++;
else if (px != py)
{
// 保证d[x]为非负整数
p[px] = py;
d[px] = (d[y] - d[x] + 3) % 3;
}
}
else
{
// x吃y
// 0类吃1类,1类吃2类,2类吃0类
if (px == py && (d[x] == d[y] || d[x] == (d[y] + 1) % 3)) ans++;
else
{
p[px] = py;
d[px] = (d[y] - d[x] - 1 + 3) % 3;
}
}
}
}
printf("%d\n", ans);
return 0;
}