POJ 3678 Katu Puzzle(2-SAT)

本文介绍了一种解决KatuPuzzle的方法,这是一种基于有向图的谜题,通过将问题转化为2-SAT问题并使用特定算法求解。文章详细解释了如何根据布尔运算规则建立图的边,并提供了一个具体的C++实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Katu Puzzle

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12336 Accepted: 4545

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

X0 = 1, X1 = 1, X2 = 0, X3 = 1.

Source

POJ Founder Monthly Contest – 2008.07.27, Dagger

 

题意:

题意来自:https://blog.csdn.net/u013480600/article/details/34134687

  一个N个顶点和M条边的有向图,每个顶点能取0或1两个值.现在每条边被一个操作符(or,and,xor)以及一个值(0或1)标记了,表示a与b按操作符运算的结果是值(0或1).问你该有向图是否有可行解?

分析:

2-sat入门模板题

2-sat解析:https://blog.csdn.net/u013480600/article/details/44858849

运算关系的转化:

根据什么能推出什么建边

and:

a and b = 0   : a=0 或 b=0 建边:2*a+1->2*b  2*b+1->2*a

a and b = 1   : a=1 且 b=1 建边: 2*a->2*a+1  2*b->2*b+1(只要a为0或b为0必然错误,所以我们只要添加上只要a=0则a就为1的矛盾命题即可)

or:

a or b = 0 :    a=0且b=0 建边: 2*a+1->2*a  2*b+1->2*b(只要a为1或b为1必然错误)

a or b = 1 :     a=1 或b=1   建边:  2*a->2*b+1   2*b->2*a+1

xor:

a xor b=0 : a=1且b=1 或 a=0且b=0  建边 :2*a->2*b    2*b->2*a    2*a+1->2*b+1        2*b+1->2*a+1.

a xor b=1 :  a=1且b=0 或a=0且b=1  建边:  2*a+1->2*b      2*b->2*a+1      2*a->2*b+1     2*b+1->2*a
 

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=10000+10;
struct TwoSAT
{
    int n;//原始图的节点数(未翻倍)
    vector<int> G[maxn*2];//G[i]==j表示如果mark[i]=true,那么mark[j]也要=true
    bool mark[maxn*2];//标记
    int S[maxn*2],c;//S和c用来记录一次dfs遍历的所有节点编号
 
    void init(int n)
    {
        this->n=n;
        for(int i=0;i<2*n;i++) G[i].clear();
        memset(mark,0,sizeof(mark));
    }
 
    //加入(x,xval)或(y,yval)条件
    //xval=0表示假,yval=1表示真
    void add_clause(int x,int xval,int y,int yval)
    {
        x=x*2+xval;
        y=y*2+yval;
        G[x].push_back(y);
       // G[y^1].push_back(x);
    }
 
    //从x执行dfs遍历,途径的所有点都标记
    //如果不能标记,那么返回false
    bool dfs(int x)
    {
        if(mark[x^1]) return false;//这两句的位置不能调换
        if(mark[x]) return true;
        mark[x]=true;
        S[c++]=x;
        for(int i=0;i<G[x].size();i++)
            if(!dfs(G[x][i])) return false;
        return true;
    }
 
    //判断当前2-SAT问题是否有解
    bool solve()
    {
        for(int i=0;i<2*n;i+=2)
        if(!mark[i] && !mark[i+1])
        {
            c=0;
            if(!dfs(i))
            {
                while(c>0) mark[S[--c]]=false;
                if(!dfs(i+1)) return false;
            }
        }
        return true;
    }
}TS;
int main()
{
	 int n,m;
	 int a,b,c;
	 char op[10];
	 while(scanf("%d%d",&n,&m)!=-1)
	{
		TS.init(n);
		for(int i=1;i<=m;i++){
		scanf("%d %d %d %s",&a,&b,&c,&op);
		if(op[0]=='A')
        {
            if(c==0)
            {
                TS.add_clause(a,1,b,0);
                TS.add_clause(b,1,a,0);
            }
            else if(c==1)
            {
                TS.add_clause(a,0,a,1);
                TS.add_clause(b,0,b,1);
            }
        }
        else if(op[0]=='O')
        {
            if(c==0)
            {
                TS.add_clause(a,1,a,0);
                TS.add_clause(b,1,b,0);
            }
            else if(c==1)
            {
                TS.add_clause(a,0,b,1);
                TS.add_clause(b,0,a,1);
            }
        }
        else if(op[0]=='X')
        {
            if(c==0)
            {
                TS.add_clause(a,0,b,0);
                TS.add_clause(a,1,b,1);
                TS.add_clause(b,0,a,0);
                TS.add_clause(b,1,a,1);
            }
            else if(c==1)
            {
                TS.add_clause(a,0,b,1);
                TS.add_clause(a,1,b,0);
                TS.add_clause(b,0,a,1);
                TS.add_clause(b,1,a,0);
            }
        }
    }
    if(TS.solve()) printf("YES\n");
    else printf("NO\n");
	 }
	 return 0;
	 
	  
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值