BZOJ1823 [JSOI2010]满汉全席

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

题目链接:BZOJ1823

正解:$2-SAT$

解题报告:

  $2-SAT$裸题。

  连边可以理解成实际意义上的"推导出",或者一个条件满足之后带来的连锁反应。

  所以只要同一个点的$0$、$1$不属于同一个强连通分量就好了。

//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long LL;
const int MAXN = 450;
const int MAXM = 5012;
int n,m,ecnt,first[MAXN],to[MAXM],next[MAXM],dfn[MAXN],stack[MAXN],low[MAXN],top,bel[MAXN],scnt;
bool pd[MAXN];
inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; }
inline int getint(){
    int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
    if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}

inline void tarjan(int x){
	dfn[x]=low[x]=++ecnt; stack[++top]=x; pd[x]=1;
	for(int i=first[x];i;i=next[i]) {
		int v=to[i];
		if(!dfn[v]) {
			tarjan(v);
			low[x]=min(low[x],low[v]);
		}
		else if(pd[v]) low[x]=min(low[x],dfn[v]);
	}
	if(dfn[x]==low[x]) {
		scnt++;
		while(stack[top]!=x) {
			pd[stack[top]]=0;
			bel[stack[top]]=scnt;
			top--;
		}
		pd[x]=0; bel[x]=scnt; top--;
	}
}

inline void work(){
	int T=getint(); int x,y; char c;
	while(T--) {
		ecnt=0;	memset(first,0,sizeof(first)); memset(dfn,0,sizeof(dfn));
		memset(pd,0,sizeof(pd)); memset(bel,0,sizeof(bel));
		top=0; memset(low,0,sizeof(low));
		n=getint(); m=getint();
		for(int i=1;i<=m;i++) {
			c=getchar(); while(c!='h' && c!='m') c=getchar();
			x=getint(); x<<=1; if(c=='h') x|=1;

			c=getchar(); while(c!='h' && c!='m') c=getchar();
			y=getint(); y<<=1; if(c=='h') y|=1;

			link(x^1,y);
			link(y^1,x);
		}
		ecnt=0; scnt=0; for(int i=2;i<=(n<<1)+1;i++) if(!dfn[i]) tarjan(i);
		bool ok=true;
		for(int i=1;i<=n;i++) if(bel[i<<1]==bel[i<<1|1]) { ok=false; break; }
		if(ok) puts("GOOD");
		else puts("BAD");
	}
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("1823.in","r",stdin);
	freopen("1823.out","w",stdout);
#endif
    work();
    return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。

  

原文地址:https://www.cnblogs.com/ljh2000-jump/p/6676804.html