Poj 3678 (two-sat)

Katu Puzzle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6893 Accepted: 2531

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

一道帮助你理解2sat的入门题。条件有3种,&&,||,^,按照相对应的逻辑连边即可。发现以前加边的模板太有局限性了,现在把加边的函数改后用起来更灵活。
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;

const int maxn = 1000 + 5;

struct TwoSAT {
  int n;
  vector<int> G[maxn*2];
  bool mark[maxn*2];
  int S[maxn*2], c;

  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;
  }

  void init(int n) {
    this->n = n;
    for (int i = 0; i < n*2; i++) G[i].clear();
    memset(mark, 0, sizeof(mark));
  }
    //kind=0 -> &&;kind=1 -> ||;kind=2 -> ^;
  void add_edge(int a,int b,int c,int kind){
    if(kind == 0){
        if(c == 0){
            G[2*a].push_back(2*b+1);
            G[2*b].push_back(2*a+1);
        }
        else{
            G[2*a+1].push_back(2*a);
            G[2*b+1].push_back(2*b);
        }
    }
    else if(kind == 1){
        if(c == 0){
            G[2*a].push_back(2*a+1);
            G[2*b].push_back(2*b+1);
        }
        else{
            G[2*a+1].push_back(2*b);
            G[2*b+1].push_back(2*a);
        }
    }
    else{
        if(c == 0){
            G[2*a].push_back(2*b);
            G[2*a+1].push_back(2*b+1);
            G[2*b].push_back(2*a);
            G[2*b+1].push_back(2*a+1);
        }
        else{
            G[2*a].push_back(2*b+1);
            G[2*a+1].push_back(2*b);
            G[2*b].push_back(2*a+1);
            G[2*b+1].push_back(2*a);
        }
    }
  }

  bool solve() {
    for(int i = 0; i < n*2; 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;
  }
};

TwoSAT solver;

int main(){
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF){
        solver.init(n);
        for(int i = 0;i < m;i++){
            int x,y,val;
            char s[5];
            scanf("%d%d%d%s",&x,&y,&val,s);
            if(s[0] == 'A') solver.add_edge(x,y,val,0);
            else if(s[0] == 'O') solver.add_edge(x,y,val,1);
            else solver.add_edge(x,y,val,2);
        }
        if(solver.solve()) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
模板解释,每个点拆成2*i(真)和2*i+1(假),下标从0开始。加边函数参数,kind代表条件的类型,c是a op b 的值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值