P2024 [NOI2001]食物链

传送门(洛谷)

此题和      \;\; P1525 关押罪犯      \;\; 差不多,都是维护一个自己的反面。我们开一个三倍的并查集
x x x表示它自己
x + n x+n x+n表示它的天敌
x + 2 ∗ n x+2*n x+2n表示它的食物
如果 x , x + 2 ∗ n , x + n x,x+2*n,x+n x,x+2n,x+n任意两个在一个集合中都是不合法情况

#include<bits/stdc++.h>
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
using namespace std;
const int maxn=2e6+10;
const int maxm=2500+10;
int n,m,ans,cnt;
int f[maxn],head[maxn];

template <class t> inline void read(t &x) {
	int f=1;x=0;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-') ch=getchar();ch=getchar();}
	while(isdigit(ch)){x=10*x+ch-'0';ch=getchar();}
	x*=f;
}

inline int find(int x) {
	if(f[x]==x) return x;
	else return f[x]=find(f[x]);
}

inline void meger(int x,int y) {
	int fx=find(x),fy=find(y);
	f[fx]=fy;
}

void readdata() {
	read(n);read(m);
	rep(i,1,3*n) f[i]=i;
}

void work() {
	rep(i,1,m) {
		int flag,x,y;
		read(flag),read(x),read(y);
		if(x>n || y>n) {ans++;continue;}
		if(flag==1) {
			if(find(x+n)==find(y) || find(x+2*n)==find(y)) {ans++;continue;}
			meger(x,y);meger(x+n,y+n);meger(x+2*n,y+2*n);
		}
		if(flag==2) {
			if(x==y) {ans++;continue;}
			if(find(x)==find(y) || find(x+n)==find(y)) {ans++;continue;}
			meger(x+2*n,y);meger(x,y+n);meger(x+n,y+2*n);
		}
	}
	printf("%d\n",ans);
}

int main() {
	//freopen("input.txt","r",stdin);
	freopen("test.in","r",stdin);
	readdata();
	work();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值