Bzoj1823:[JSOI2010]满汉全席:2-sat

1 篇文章 0 订阅

题目连接:1823:[JSOI2010]满汉全席

2-sat首题!

2-sat的图中每一条边<i,j>表示选了i必须选j

拆点,一个表示选一个表示不选,跑tarjan,如果有一个点拆成的两个点在一个联通分量里就是BAD

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=2010;
int n,m,h[maxn],tot=0,sta[maxn],top=0;
struct edge{int to,next;}G[maxn*10];
int cnt=0,scc[maxn],dfn[maxn],low[maxn],ind=0;
char s1,s2;
bool insta[maxn];

void tarjan(int x){
	dfn[x]=low[x]=++ind; 
	insta[x]=1; sta[++top]=x;
	for (int i=h[x];i;i=G[i].next){
		int v=G[i].to;
		if (!dfn[v]){
			tarjan(v);
			low[x]=min(low[x],low[v]);
		}else if (insta[v])
			low[x]=min(low[x],dfn[v]);
	}
	if (low[x]==dfn[x]){
		int k=0; ++cnt;
		while (k!=x){
			k=sta[top--]; insta[k]=0;
			scc[k]=cnt;
		}
	}
}

void add(int x,int y){
	G[++tot].to=y;G[tot].next=h[x];h[x]=tot;
}

int main(){
	int T; scanf("%d",&T);
	while (T--){
		scanf("%d%d",&n,&m);
		int N=2*n; ind=0; cnt=0; top=0;
		for (int i=1;i<=N;++i) h[i]=0; tot=0;
		for (int i=1;i<=m;++i){
			int t1,t2,x,y;
			cin>>s1; scanf("%d",&t1);
			cin>>s2; scanf("%d",&t2);
			if (s1=='h') t1=t1*2-1; else t1=t1<<1;
			if (s2=='h') t2=t2*2-1; else t2=t2<<1; 
			if (t1%2==0) x=t1-1; else x=t1+1;
			if (t2%2==0) y=t2-1; else y=t2+1;
			add(x,t2); add(y,t1);
		}
		for (int i=1;i<=N;++i) dfn[i]=low[i]=0;
		for (int i=1;i<=N;++i) if (!dfn[i]) tarjan(i);
		bool flag=1;
		for (int i=1;i<=n;++i)
			if (scc[(i<<1)-1]==scc[i<<1]){
				printf("BAD\n"); 
				flag=0; break;	
			}
		if (flag) printf("GOOD\n");
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值