poj3687(2-sat)

Katu Puzzle is presented as a directed graph G(VE) with each edge e(a, b) labeled by a boolean operator op (one of AND, OR, XOR) and an integer c (0 ≤ c ≤ 1). One Katu is solvable if one can find each vertex Vi a value Xi (0 ≤ Xi ≤ 1) such that for each edge e(a, b) labeled by op and c, the following formula holds:

 Xa op Xb = c

The calculating rules are:

AND 0 1
0 0 0
1 0 1
OR 0 1
0 0 1
1 1 1
XOR 0 1
0 0 1
1 1 0

Given a Katu Puzzle, your task is to determine whether it is solvable.

Input

The first line contains two integers N (1 ≤ N ≤ 1000) and M,(0 ≤ M ≤ 1,000,000) indicating the number of vertices and edges.
The following M lines contain three integers (0 ≤ a < N), b(0 ≤ b < N), c and an operator op each, describing the edges.

Output

Output a line containing "YES" or "NO".

Sample Input

4 4
0 1 1 AND
1 2 1 OR
3 2 0 AND
3 0 0 XOR

Sample Output

YES

Hint

X 0 = 1, X 1 = 1, X 2 = 0, X 3 = 1.

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1000+10;
struct TwoSAT
{
    int n;
    vector<int> G[maxn*2];
    int S[maxn*2],c;
    bool mark[maxn*2];

    bool dfs(int x)
    {
        if(mark[x^1]) return false;
        if(mark[x]) return true;
        mark[x]=true;
        S[c++]=x;
        for(int i=0;i<G[x].size();i++)
            if(!dfs(G[x][i])) return false;
        return true;
    }

    void init(int n)
    {
        this->n=n;
        for(int i=0;i<n*2;i++) G[i].clear();
        memset(mark,0,sizeof(mark));
    }

    void add_clause(int x,int xval,int y,int yval)//这里的函数做了修改,只加单向边
    {
        x=x*2+xval;
        y=y*2+yval;
        G[x].push_back(y);
    }

    bool solve()
    {
        for(int i=0;i<2*n;i+=2)
        if(!mark[i] && !mark[i+1])
        {
            c=0;
            if(!dfs(i))
            {
                while(c>0) mark[S[--c]]=false;
                if(!dfs(i+1)) return false;
            }
        }
        return true;
    }
}TT;


int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
        TT.init(n);
        char ss[10];
        int a,b,c;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d %s",&a,&b,&c,ss);
            if(ss[0]=='A')
            {    if(c==1)
                 {
                     TT.add_clause(a,0,a,1);
                     TT.add_clause(b,0,b,1);
                 }
                else  if(c==0)
                 {
                     TT.add_clause(a,1,b,0);
                     TT.add_clause(b,1,a,0);
                 }
            }
            else if(ss[0]=='O')
            {
                if(c==1)
                {
                    TT.add_clause(a,0,b,1);
                    TT.add_clause(b,0,a,1);
                }
                else if(c==0)
                {
                    TT.add_clause(a,1,a,0);
                    TT.add_clause(b,1,b,0);
                }
            }
           else if(ss[0]=='X')
            {
                if(c==0)
                {
                    TT.add_clause(a,0,b,0);
                TT.add_clause(a,1,b,1);
                TT.add_clause(b,1,a,1);
                TT.add_clause(b,0,a,0);
                }

                else if(c==1)
                {
               TT.add_clause(a,1,b,0);
                TT.add_clause(a,0,b,1);
                TT.add_clause(b,1,a,0);
                TT.add_clause(b,0,a,1);
                }
            }
        }
        if(!TT.solve())
        {
            printf("NO\n");
        }
        else
        {
            printf("YES\n");
        }
}

猜你喜欢

转载自blog.csdn.net/qq_40859951/article/details/88085645