POJ 3678 Katu Puzzle 2-SAT

来源:互联网 发布:vb调用matlab工具箱 编辑:程序博客网 时间:2024/05/17 08:29

Katu Puzzle
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8330 Accepted: 3069

Description

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 ≤ X≤ 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:

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 (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.

Source

POJ Founder Monthly Contest – 2008.07.27, Dagger


WA了几发之后我想明白了一个问题。。然后过了>.<


/** Author: ¡î¡¤aosaki(*¡¯(OO)¡¯*)  niconiconi¡ï **///#pragma comment(linker, "/STACK:1024000000,1024000000")//#include<bits/stdc++.h>#include <iostream>#include <sstream>#include <cstdio>#include <cstring>#include <algorithm>#include <functional>#include <cmath>#include <vector>#include <queue>#include <map>//#include <tuple>#define ALL(v) (v).begin(),(v).end()#define foreach(i,v) for (__typeof((v).begin())i=(v).begin();i!=(v).end();i++)#define SIZE(v) ((int)(v).size())#define mem(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define lp(k,a) for(int k=1;k<=a;k++)#define lp0(k,a) for(int k=0;k<a;k++)#define lpn(k,n,a) for(int k=n;k<=a;k++)#define lpd(k,n,a) for(int k=n;k>=a;k--)#define sc(a) scanf("%d",&a)#define sc2(a,b) scanf("%d %d",&a,&b)#define lowbit(x) (x&(-x))#define ll long long#define pi pair<int,int>#define vi vector<int>#define PI acos(-1.0)#define pb(a) push_back(a)#define mp(a,b) make_pair(a,b)#define TT cout<<"*****"<<endl;#define TTT cout<<"********"<<endl;inline int gcd(int a,int b){    return a==0?b:gcd(b%a,a);}#define INF 1e9#define eps 1e-8#define mod 10007#define maxn 5010#define maxm 4000010using namespace std;int v[maxn],col[maxn];int rea[maxn],low[maxn],stack[maxn];int n,m,tot=0,color,tm=0,top=0;int pre[maxn];struct Side{    int to,next;}e[maxm];void add(int u,int v){    e[tot].to=v;    e[tot].next=pre[u];    pre[u]=tot++;}void tarjan(int i){    v[i]=1;    top++;    stack[top]=i;    ++tm;    rea[i]=tm;    low[i]=tm;    for(int j=pre[i];j!=-1;j=e[j].next)        {            int x=e[j].to;            if(v[x]==0) tarjan(x);            if(v[x]<2) low[i]=min(low[i],low[x]);        }    if(rea[i]==low[i])    {        color++;        while(stack[top+1]!=i)        {            col[stack[top]]=color;            v[stack[top]]=2;            top--;        }    }}void init(){    mem(col);    mem(v);    mem1(pre);    tot=0;    top=0;    tm=0;    color=0;    mem(low);    mem(rea);    mem(stack);}bool ok(){    lp(i,n)    {        if((col[i*2]==col[i*2+1]))            return 0;    }    return 1;}int main(){     freopen("in.txt","r",stdin);     int a,b,c;     char s[10];     while(~sc2(n,m))     {        init();        lp(i,m)        {            sc(a);sc(b);sc(c);            scanf("%s",s);            a++; b++;            if(s[0]=='A')            {                 if(c==1)                {                    add(a*2,a*2+1);                    add(b*2,b*2+1);                }                else                {                    add(a*2+1,b*2);                    add(b*2+1,a*2);                }            }            if(s[0]=='O')            {                if(c==1)                {                    add(a*2,b*2+1);                    add(b*2,a*2+1);                }                else                {                    add(a*2+1,a*2);                    add(b*2+1,b*2);                                  }            }            if(s[0]=='X')            {                if(c==1)                {                    add(a*2+1,b*2);                    add(b*2+1,a*2);                    add(a*2,b*2+1);                    add(b*2,a*2+1);                }                else                {                    add(a*2+1,b*2+1);                    add(b*2+1,a*2+1);                    add(b*2,a*2);                    add(a*2,b*2);                }            }        }        lp(i,n*2)          if(!rea[i])             tarjan(i);        if(ok()) printf("YES\n");          else printf("NO\n");     }    return 0;}



0 0
原创粉丝点击