poj 3678 Katu Puzzle(2-sat应用)

来源:互联网 发布:制作生日快乐贺卡软件 编辑:程序博客网 时间:2024/06/08 06:43
Katu Puzzle
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5727 Accepted: 2067

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

题目:http://poj.org/problem?id=3678

题意:给你n个布尔型变量,给你一个关系表,标志n个变量之间的关系,问这样的关系是否合法。。。。

分析:好吧,由于是在2-sat分类里面看到的题,所有直接想到2-sat,只要分清3个关系式,就可以建图了,比如and并且为1,则两个变量同时为1,and为0的话,只要保证一个是0就行。。。

代码:

#include<cstdio>#include<iostream>using namespace std;const int mm=4444444;const int mn=2222;int ver[mm],next[mm];int head[mn],dfn[mn],low[mn],q[mn],id[mn];int i,j,k,n,m,idx,top,cnt,edge;void add(int u,int v){    ver[edge]=v,next[edge]=head[u],head[u]=edge++;}void dfs(int u){    dfn[u]=low[u]=++idx;    q[top++]=u;    for(int i=head[u],v;i>=0;i=next[i])        if(!dfn[v=ver[i]])            dfs(v),low[u]=min(low[u],low[v]);        else if(!id[v])low[u]=min(low[u],dfn[v]);    if(dfn[u]==low[u])    {        id[u]=++cnt;        while(q[--top]!=u)id[q[top]]=cnt;    }}void Tarjan(){    for(idx=top=cnt=i=0;i<n+n;++i)dfn[i]=id[i]=0;    for(i=0;i<n+n;++i)        if(!dfn[i])dfs(i);}bool ok(){    for(i=0;i<n+n;i+=2)        if(id[i]==id[i^1])return 0;    return 1;}char op[22];int main(){    while(~scanf("%d%d",&n,&m))    {        for(edge=i=0;i<n+n;++i)head[i]=-1;        while(m--)        {            scanf("%d%d%d%s",&i,&j,&k,op);            if(op[0]=='A')            {                if(k)add(i<<1,i<<1|1),add(j<<1,j<<1|1);                else add(i<<1|1,j<<1),add(j<<1|1,i<<1);            }            if(op[0]=='O')            {                if(k)add(i<<1,j<<1|1),add(j<<1,i<<1|1);                else add(i<<1|1,i<<1),add(j<<1|1,j<<1);            }            if(op[0]=='X')            {                if(k)add(i<<1|1,j<<1),add(j<<1|1,i<<1),add(i<<1,j<<1|1),add(j<<1,i<<1|1);                else add(i<<1,j<<1),add(j<<1,i<<1),add(i<<1|1,j<<1|1),add(j<<1|1,i<<1|1);            }        }        Tarjan();        puts(ok()?"YES":"NO");    }    return 0;}


原创粉丝点击