POJ 3678 Katu Puzzle

2-SAT简单题。位运算写的时候忘记加括号WA了一发....

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;

const int maxn=2000+10;
int N,M;
char s[1000];
int a,b,c;
int a1=0,a2=1,b1=0,b2=1;

stack<int>S;
vector<int>G[maxn];
vector<int>FG[maxn];
int Belong[maxn];
int flag[maxn];
int Block;

void init()
{
    for(int i=0; i<maxn; i++) G[i].clear();
    for(int i=0; i<maxn; i++) FG[i].clear();
    memset(Belong,0,sizeof Belong);
    memset(flag,0,sizeof flag);
    while(!S.empty()) S.pop();
    Block=0;
}

void addEgde(int x,int y)
{
    G[x].push_back(y);
    FG[y].push_back(x);
}

void dfs1(int now)
{
    flag[now]=1;
    for(int i=0; i<G[now].size(); i++)
        if(!flag[G[now][i]])
            dfs1(G[now][i]);
    S.push(now);
}

void dfs2(int now)
{
    Belong[now]=Block;
    for(int i=0; i<FG[now].size(); i++)
        if(!Belong[FG[now][i]])
            dfs2(FG[now][i]);
}

bool judge()
{
    for(int i=0; i<2*N; i++) if(!flag[i]) dfs1(i);
    while(!S.empty())
    {
        int Top=S.top();
        S.pop();
        if(!Belong[Top])
        {
            Block++;
            dfs2(Top);
        }
    }
    for(int i=0; i<N; i++)
        if(Belong[2*i]==Belong[2*i+1])
            return 0;
    return 1;
}

int main()
{
    while(~scanf("%d%d",&N,&M))
    {
        init();
        for(int i=0; i<M; i++)
        {
            scanf("%d%d%d%s",&a,&b,&c,s);
            if(strcmp("AND",s)==0)
            {
                if((a1&b1)!=c)
                {
                    addEgde(2*a,2*b+1);
                    addEgde(2*b,2*a+1);
                }

                if((a1&b2)!=c)
                {
                    addEgde(2*a,2*b);
                    addEgde(2*b+1,2*a+1);
                }

                if((a2&b1)!=c)
                {
                    addEgde(2*a+1,2*b+1);
                    addEgde(2*b,2*a);
                }

                if((a2&b2)!=c)
                {
                    addEgde(2*a+1,2*b);
                    addEgde(2*b+1,2*a);
                }

            }
            else if(strcmp("OR",s)==0)
            {
                if((a1|b1)!=c)
                {
                    addEgde(2*a,2*b+1);
                    addEgde(2*b,2*a+1);
                }

                if((a1|b2)!=c)
                {
                    addEgde(2*a,2*b);
                    addEgde(2*b+1,2*a+1);
                }

                if((a2|b1)!=c)
                {
                    addEgde(2*a+1,2*b+1);
                    addEgde(2*b,2*a);
                }

                if((a2|b2)!=c)
                {
                    addEgde(2*a+1,2*b);
                    addEgde(2*b+1,2*a);
                }
            }
            else if(strcmp("XOR",s)==0)
            {
                if((a1^b1)!=c)
                {
                    addEgde(2*a,2*b+1);
                    addEgde(2*b,2*a+1);
                }

                if((a1^b2)!=c)
                {
                    addEgde(2*a,2*b);
                    addEgde(2*b+1,2*a+1);
                }

                if((a2^b1)!=c)
                {
                    addEgde(2*a+1,2*b+1);
                    addEgde(2*b,2*a);
                }

                if((a2^b2)!=c)
                {
                    addEgde(2*a+1,2*b);
                    addEgde(2*b+1,2*a);
                }
            }
        }
        if(judge()) printf("YES
");
        else printf("NO
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/4915590.html