浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html
题目传送门:https://www.luogu.org/problemnew/show/P2024
对于每个生物,我们都可以将其拆成三个点,一个是自己(self),一个是食物(eat),一个是天敌(enemy),然后对于每一句话我们分别判断:
如果(x)与(y)是同类:如果(x_{eat}=y_{self})或(y_{eat}=x_{self})则是假话,否则分别对应合并三个集合。
如果(x)吃(y):如果(x_{self}=y_{self})或(y_{eat}=x_{self})则是假话,否则就合并(x_{eat})与(y_{self}),(x_{enemy})与(y_{eat}),(x_{self})与(y_{enemy})
时间复杂度:(O(alpha{n}))
空间复杂度:(O(n))
代码如下:
#include <cstdio>
using namespace std;
const int maxn=1.5e5+5;
int n,m,ans;
int fa[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
int find(int x) {
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
int main() {
n=read(),m=read();
for(int i=1;i<=n*3;i++)fa[i]=i;
for(int i=1;i<=m;i++) {
int opt=read(),a=read(),b=read();
if(a>n||b>n) {ans++;continue;}
if(opt==2&&a==b) {ans++;continue;}
int a_self=find(a),a_eat=find(a+n),a_enemy=find(a+(n<<1));
int b_self=find(b),b_eat=find(b+n),b_enemy=find(b+(n<<1));
if(opt==1) {
if(a_eat==b_self||b_eat==a_self) {ans++;continue;}
fa[a_self]=b_self,fa[a_eat]=b_eat,fa[a_enemy]=b_enemy;
}
else {
if(a_self==b_self||b_eat==a_self) {ans++;continue;}
fa[a_eat]=b_self,fa[a_self]=b_enemy,fa[a_enemy]=b_eat;
}
}
printf("%d
",ans);
return 0;
}