POJ 3678 Katu Puzzle 【经典2-SAT】


传送门:POJ 3678


Katu Puzzle
Time Limit: 1000MS Memory Limit: 65536K

Problem Description
Katu Puzzle is presented as a directed graph G(V, E) 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:
在这里插入图片描述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 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.



题意:
有n个Xi…告诉你m个位运算( AND OR XOR )结果…问是否有存在可行解
使得每个Xa op Xb = c都成立。


题解:
对于X里的每个数,我们都只可以取0和1两个数,m次的位运算就是限制条件,典型的2-SAT题。
记对于Xi的两个取值 a a a代表1, a ′ a&#x27; a代表0
要注意的几个限制条件是:

  • A AND B=1:A,B必须都是1,所以有 a ′ − &gt; a , b ′ − &gt; b a&#x27;-&gt;a,b&#x27;-&gt;b a>ab>b
  • A OR B=0:A,B必须都是0,所以有 a − &gt; a ′ , b − &gt; b ′ a-&gt;a&#x27;, b-&gt;b&#x27; a>a,b>b

其他的按照运算规则建边即可。



AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#define ll long long
using namespace std;
const int N=1100;
int n,m,top;
bool mark[2*N];
int stack[2*N];
vector<int> g[2*N];
int a,b,c;
char op[5];
void init()
{
  memset(mark,false,sizeof(mark));
  for(int i=0;i<2*N;i++)
  {
    g[i].clear();
  }
}
void addedge(int x,int idx,int y,int idy)
{
  x=2*x+idx;
  y=2*y+idy;
  g[x].push_back(y);
}
bool dfs(int x)
{
  if(mark[x^1]) return false;
  if(mark[x]) return true;
  mark[x]=true;
  stack[++top]=x;
  int len=g[x].size();
  for(int i=0;i<len;i++)
  {
    if(!dfs(g[x][i])) return false;
  }
  return true;
}
bool twosat()
{
  for(int i=0;i<2*n;i+=2)
  {
    if(!mark[i]&&!mark[i+1])
    {
      top=0;
      if(!dfs(i))
      {
        while(top>0)
          mark[stack[top--]]=false;
        if(!dfs(i+1)) return false;
      }
    }
  }
  return true;
}
int main()
{
  while(scanf("%d%d",&n,&m)!=EOF)
  {
    init();
    for(int i=0;i<m;i++)
    {
      scanf("%d%d%d%s",&a,&b,&c,op);
      if(op[0]=='A')
      {
        if(c==1)
        {
          addedge(a,0,a,1);
          addedge(b,0,b,1);
        }
        else
        {
          addedge(a,1,b,0);
          addedge(b,1,a,0);
        }
      }
      if(op[0]=='O')
      {
        if(c==0)
        {
          addedge(a,1,a,0);
          addedge(b,1,b,0);
        }
        else
        {
          addedge(a,0,b,1);
          addedge(b,0,a,1);
        }
      }
      if(op[0]=='X')
      {
        if(c==1)
        {
          addedge(a,0,b,1);
          addedge(a,1,b,0);
          addedge(b,0,a,1);
          addedge(b,1,a,0);
        }
        else
        {
          addedge(a,0,b,0);
          addedge(a,1,b,1);
          addedge(b,0,a,0);
          addedge(b,1,a,1);
        }
      }
    }
    if(twosat()) printf("YES\n");
    else printf("NO\n");
  }
  return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值