You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.3 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 50005;
int p[N];
int d[N]; // 0 同类 1吃 2被吃
// 分析https://blog.csdn.net/yjr3426619/article/details/82315133
// A吃B B吃C ==> A被C吃 Relation(a,c) = Relation(a,b)+Relation(b,c)
// A被B吃 B被C吃 ==> A吃C Relation(a,c) = (Relation(a,b)+Relation(b,c))%3
int find(int x) {
if (x != p[x]) {
int t = p[x];
p[x] = find(p[x]);
d[x] = (d[x] + d[t]) % 3;
}
return p[x];
}
int main() {
//输入+输出重定向
freopen("../AcWing/N5/poj-1182.txt", "r", stdin);
int n, k;
cin >> n >> k;
int ans = 0;
for (int i = 1; i <= n; i++) {
p[i] = i;
}
while (k--) {
int relation, x, y;
scanf("%d%d%d", &relation, &x, &y);
if (x > n || y > n || (relation == 2 && x == y)) {
ans++;
} else {
int fx = find(x);
int fy = find(y);
if (fx == fy) {
if ((relation - 1) != ((d[x] - d[y] + 3) % 3))
ans++;
} else {
p[fx] = fy;
d[fx] = (-d[x] + relation - 1 + d[y]) % 3;
}
}
}
cout << ans;
//关闭文件
fclose(stdin);
return 0;
}