Los solution to a problem Valley P4171 [[JSOI2010] Feast]

Consider \ (2-SAT \) .

Han considered the formula \ (0 \) state, seen over the formula \ (1 \) state, the material is split into each \ (01 \) two states.

From \ (a \) to \ (b \) is connected to the side meaningful representation for the election \ (a \) after the election must be \ (b \) .

Even then each side as follows:

\ (the Add (X_ {A \ Oplus. 1}, y_b), the Add (Y_ {B \ Oplus. 1}, X_a) \) ( \ (X_a \) and \ (y_b \) requirements assessors, \ (X \) and \ (Y \) represents the material, \ (A \) and \ (B \) shows a state)

If there is no meaning to meet a requirement which the assessors, the other requirements must be met.

Even after condensing the edge point, if found \ (X_a \) and (x_ {a \ oplus 1} \) \ in the same strongly connected component, no solution.

Other processing some implementations, to see the code.

\(code:\)

#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;
}

Guess you like

Origin www.cnblogs.com/lhm-/p/12229832.html