POJ 3678 2-SAT

                                                                                               Katu Puzzle











Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 6870Accepted: 2524

Description




Katu Puzzle is presented as a directed graph G(V, E)
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:





















AND01
000
101














OR01
001
111














XOR01
001
110

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 a (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
题意:有n个顶点,m条边,每条边上有一个运算符,以及运算结果,判断是否存在一组解,给每个点上赋值0或1,使得每条边上的值等于这条边两个顶点的运算值。
解题思路:简单2-SAT,建边比较多,写错两个字母,白白的wa三次,坑。
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
using namespace std;
struct node
{
        int to,next;
}edge[9000000];
int head[3000],tol,low[3000],dfn[3000],indexx,Stack[300000],instack[3000],belong[3000],scc,top;
void add(int u,int v)
{
        edge[tol].to=v;
        edge[tol].next=head[u];
        head[u]=tol++;
}
void tarjin(int u)
{
        low[u]=dfn[u]=++indexx;
        Stack[top++]=u;instack[u]=1;
        int i,v;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
                v=edge[i].to;
                if(!dfn[v])
                {
                        tarjin(v);
                        if(low[u]>low[v])low[u]=low[v];
                }
                else if(instack[v]&&low[u]>dfn[v])low[u]=dfn[v];
        }
        if(low[u]==dfn[u])
        {
                scc++;
                do
                {
                        v=Stack[--top];
                        instack[v]=0;
                        belong[v]=scc;
                }while(u!=v);
        }
}
int solve(int n)
{
        memset(dfn,0,sizeof(dfn));
        memset(instack,0,sizeof(instack));
        memset(belong,0,sizeof(belong));
        indexx=top=scc=0;
        for(int i=0;i<2*n;i++)if(!dfn[i])tarjin(i);
        //cout<<tol<<" "<<scc<<endl;
        for(int i=0;i<n;i++)if(belong[2*i]==belong[2*i+1])return 0;
        return 1;
}
int main()
{
        int i,j,k,m,n;
        while(~scanf("%d%d",&n,&m))
        {
                memset(head,-1,sizeof(head));tol=0;
                char str[400];
                while(m--)
                {
                      scanf("%d%d%d",&i,&j,&k);
                      scanf("%s",str);
                      if(strcmp(str,"AND")==0)
                      {
                            if(k)
                            {
                                    add(2*i+1,2*i);
                                    add(2*j+1,2*j);
                                    add(2*i,2*j);
                                    add(2*j,2*i);
                            }
                            else
                            {
                                    add(2*i,2*j+1);
                                    add(2*j,2*i+1);
                            }
                      }
                      if(strcmp(str,"OR")==0)
                      {
                              if(k)
                              {
                                      add(2*i+1,2*j);
                                      add(2*j+1,2*i);
                              }
                              else
                              {
                                      add(2*i,2*i+1);
                                      add(2*j,2*j+1);
                                      add(2*i+1,2*j+1);
                                      add(2*j+1,2*i+1);
                              }
                      }
                      if(strcmp(str,"XOR")==0)
                      {
                              if(k==0)
                              {
                                      add(2*i,2*j);
                                      add(2*j,2*i);
                                      add(2*i+1,2*j+1);
                                      add(2*j+1,2*i+1);
                              }
                              else
                              {
                                      add(2*i,2*j+1);
                                      add(2*i+1,2*j);
                                      add(2*j,2*i+1);
                                      add(2*j+1,2*i);
                              }
                      }
                }
                if(solve(n))puts("YES");
                else puts("NO");
        }
        return 0;
}
posted @ 2013-09-04 10:25  线性无关  阅读(104)  评论(0)    收藏  举报