考えてみましょう\(2-SAT \) 。
ハン式考え\(0 \)式の上に見られる状態、\(1 \)状態、物質がそれぞれ分割される(\ 01 \)二つの状態。
\(\)する\(B \)選挙側意味の表現に接続されている\(\)選挙がなければならない後\(B \) 。
その後も各辺次のように:
\(ADD(X_ {A \ Oplus。1}、y_b)、追加(Y_ {B \ Oplus。1}、X_A)\)(\ (X_A \)と\(y_b \)要件の査定、\(X \)と\(Y \)材料を表し、\(A \)と\(B \)が示す状態)
評価者は、他の要件が満たされなければならない要件を満たすためには意味がない場合。
でも見つかった場合、エッジポイントを凝縮した後\(X_A \)及び(X_ {1 oplus \} \)\ 同じ強連結成分に、無溶液。
その他のコードを見て、いくつかの実装を処理します。
\(コード:\)
#include<bits/stdc++.h>
#define maxn 4000010
using namespace std;
template<typename T> inline void read(T &x)
{
x=0;char c=getchar();bool flag=false;
while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
if(flag)x=-x;
}
int t,n,m;
char str[5];
struct edge
{
int to,nxt;
}e[maxn];
int head[maxn],edge_cnt;
void add(int from,int to)
{
e[++edge_cnt]=(edge){to,head[from]};
head[from]=edge_cnt;
}
int dfn_cnt,co_cnt,top;
int dfn[maxn],low[maxn],co[maxn],st[maxn];
bool vis[maxn];
void tarjan(int x)
{
dfn[x]=low[x]=++dfn_cnt;
st[++top]=x;
vis[x]=true;
for(int i=head[x];i;i=e[i].nxt)
{
int y=e[i].to;
if(!dfn[y])
{
tarjan(y);;
low[x]=min(low[x],low[y]);
}
else if(vis[y])
low[x]=min(low[x],dfn[y]);
}
if(low[x]==dfn[x])
{
co_cnt++;
int now;
do
{
now=st[top--];
vis[now]=false;
co[now]=co_cnt;
}while(now!=x);
}
}
bool check()
{
for(int i=1;i<=2*n;++i)
if(!dfn[i])
tarjan(i);
for(int i=1;i<=n;++i)
if(co[i]==co[i+n])
return false;
return true;
}
void clear()
{
top=dfn_cnt=co_cnt=edge_cnt=0;
memset(co,0,sizeof(co));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(vis,0,sizeof(vis));
memset(head,0,sizeof(head));
}
int main()
{
read(t);
while(t--)
{
clear();
read(n),read(m);
while(m--)
{
int x,y,a,b,len;
scanf("%s",str);
if(str[0]=='h') a=0;
else a=1;
x=0,len=strlen(str);
for(int i=1;i<len;++i) x=x*10+str[i]-'0';
scanf("%s",str);
if(str[0]=='h') b=0;
else b=1;
y=0,len=strlen(str);
for(int i=1;i<len;++i) y=y*10+str[i]-'0';
add(x+(a^1)*n,y+b*n),add(y+(b^1)*n,x+a*n);
}
if(check()) puts("GOOD");
else puts("BAD");
}
return 0;
}