poj 3678 Katu Puzzle(2-sat应用)

Katu Puzzle
Time Limit: 1000MS Memory Limit: 65536K
Total 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:

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

Source

题目: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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值