POJ 3678 Katu Puzzle (2-sat,4级)

来源:互联网 发布:mac口红吧怎么看真假 编辑:程序博客网 时间:2024/05/24 05:58
D - Katu Puzzle
Crawling in process...Crawling failedTime Limit:1000MS    Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
SubmitStatus
Appoint description:

Description

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

 Xa op Xb = c

The calculating rules are:

AND01000101OR01001111XOR01001110

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 40 1 1 AND1 2 1 OR3 2 0 AND3 0 0 XOR

Sample Output

YES

Hint

X0 = 1, X1 = 1,X2 = 0, X3 = 1.
哎,又是一几乎模板,就是要搞清楚逻辑关系。
#include<cstdio>#include<cstring>#include<iostream>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int mm=2010;class Edge{  public:int v,next;};class TWO_SAT{public:  int dfn[mm],e_to[mm],stack[mm];  Edge e[mm*mm*2];  int edge,head[mm],top,dfs_clock,bcc;  void clear()  {    edge=0;clr(head,-1);  }  void add(int u,int v)  {    e[edge].v=v;e[edge].next=head[u];head[u]=edge++;  }  void add_my(int x,int xval,int y,int yval)  {    x=x+x+xval;y=y+y+yval;    add(x,y);  }  void add_clause(int x,int xval,int y,int yval)  {///x or y    x=x+x+xval;    y=y+y+yval;    add(x^1,y);add(y^1,x);  }  void add_con(int x,int xval)//x is xval  { x=x+x+xval;    add(x^1,x);  }  int tarjan(int u)  {    int lowu,lowv;    lowu=dfn[u]=++dfs_clock;    int v; stack[top++]=u;    for(int i=head[u];~i;i=e[i].next)    {      v=e[i].v;      if(!dfn[v])      {        lowv=tarjan(v);        lowu=min(lowv,lowu);      }      else if(e_to[v]==-1)//in stack        lowu=min(lowu,dfn[v]);    }    if(dfn[u]==lowu)    {      ++bcc;      do{        v=stack[--top];        e_to[v]=bcc;      }while(v!=u);    }    return lowu;  }  bool find_bcc(int n)  { clr(e_to,-1);    clr(dfn,0);    bcc=dfs_clock=top=0;    FOR(i,0,2*n-1)    if(!dfn[i])      tarjan(i);    for(int i=0;i<2*n;i+=2)      if(e_to[i]==e_to[i^1])return 0;    return 1;  }}two;int n,m;char s[44];int main(){ int a,b,c,d;  while(~scanf("%d%d",&n,&m))  {    two.clear();    FOR(i,1,m)    {      scanf("%d%d%d%s",&a,&b,&c,s);      if(s[0]=='A')      {        if(c)        {          two.add_clause(a,1,b,1);          two.add_clause(a,0,b,1);//imposible a 1 b 0          two.add_clause(a,1,b,0);//          two.add_my(a,0,b,1);//          two.add_my(a,0,b,0);//          two.add_my(b,0,a,1);//          two.add_my(b,0,a,0);        }        else        {          two.add_clause(a,0,b,0);        }      }      else if(s[0]=='O')      {        if(c)        {          two.add_clause(a,1,b,1);        }        else        {   two.add_clause(a,0,b,0);            two.add_clause(a,1,b,0);            two.add_clause(a,0,b,1);//          two.add_my(a,1,b,0);//          two.add_my(a,1,b,1);//          two.add_my(b,1,a,0);//          two.add_my(b,1,a,1);        }      }      else if(s[0]=='X')      {        if(c)        {          two.add_clause(a,1,b,1);          two.add_clause(a,0,b,0);        }        else        {          two.add_clause(a,1,b,0);          two.add_clause(a,0,b,1);        }      }    }    if(two.find_bcc(n*2))printf("YES\n");    else printf("NO\n");  }  return 0;}



原创粉丝点击