65 lines
1.7 KiB
C++
65 lines
1.7 KiB
C++
#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;
|
||
} |