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.

65 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<bits/stdc++.h>
using namespace std;
const int N = 150010;
int fa[N];
int ans;
//带扩展域的并查集
/**
* 功能:寻找祖先
* @param x
* @return
*/
int find(int x) {
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
//加入家族集合中
void join(int x, int y) {
int f1 = find(x), f2 = find(y);
if (f1 != f2)fa[f1] = f2;
}
int main() {
int n, m;
cin >> n >> m;
//开三个并查集,合用一个数组表示
//对于每种生物设x为本身x+n为猎物x+2*n为天敌
for (int i = 1; i <= 3 * n; i++) fa[i] = i; //初始化
while (m--) {
int x, y, t;
cin >> t >> x >> y;
// 太大了,越界了,肯定是假话
if (x > n || y > n) {
ans++;
continue;
}
//xy同类
if (t == 1) {
//如果y是x的天敌或猎物为谎言
if (find(x + n) == find(y) || find(x + 2 * n) == find(y)) {
ans++;
continue;
}
//x的同类和y的同类x的猎物是y的猎物x的天敌是y的天敌
join(x, y);
join(x + n, y + n);
join(x + 2 * n, y + 2 * n);
}
//y是x的猎物
if (t == 2) {
//如果x是y的同类或猎物为谎言
if (find(x) == find(y) || find(x + 2 * n) == find(y)) {
ans++;
continue;
}
join(x, y + 2 * n);//x的同类是y的天敌
join(x + n, y);//x的猎物是y的同类
join(x + 2 * n, y + n); //x的天敌是y的猎物
}
}
printf("%d\n", ans);
return 0;
}