poj 3678 Katu Puzzle(2-sat)

Katu Puzzle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9684 Accepted: 3620

Description

Katu Puzzle is presented as a directed graph G(V, E) with each edgee(a, b) labeled by a boolean operator op (one of AND, OR, XOR) and an integerc (0 ≤ c ≤ 1). One Katu is solvable if one can find each vertexVi a value Xi (0 ≤ Xi ≤ 1) such that for each edgee(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 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

X 0 = 1, X 1 = 1, X 2 = 0, X 3 = 1.


题意:n个点,m条边,每条边有一个运算符,要求给边上两点赋值(0|1)得计算得到指定结果。问能否满足所有条件。

思路:由于点上的值只有0|1,那么就是很典型的2-sat 算法,把每个点拆成两个点,分别代表1,和0,按照限制条件连边。跑2-sat即可。

建边规则:
2sat的基础建边


AND = 1 : ~x -> x ,~y -> y   (两个数必须全为1)  

AND = 0 : y -> ~x ,x -> ~y  (两个数至少有一个为0) 
OR  = 1 : ~x -> y ,~y -> x  (两个数至少有一个为1)  
OR  = 0 : x -> ~x ,y -> ~y  (两个数全为0)                
XOR = 1 : x -> ~y ,y -> ~x ,~y -> x ,~x -> y(两个数不同)
XOR = 0 : x -> y ,~x -> ~y ,y -> x ,~y -> ~x(两个数相同)

x,y不能都选是(基础的矛盾) :  x -> ~y ,y -> ~x 
x,y不能都选否               :~x -> y ,~y -> x
不能同时x选是,y选否        :x -> y ,~y -> ~x
不能同时x选否,y选是        :~x -> ~y ,y -> x

代码:
//#include<bits/stdc++h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<math.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
const int N = 2010;
const int M = N*N*4;
struct Edge
{
    int from, to, next;
} edge[M], edge2[M];
int head[N];
int cntE, cntE2;
void addedge(int u, int v)
{
    edge[cntE].from = u;
    edge[cntE].to = v;
    edge[cntE].next = head[u];
    head[u] = cntE++;
}
void addedge2(int u, int v)
{
    edge2[cntE2].from = u;
    edge2[cntE2].to = v;
    edge2[cntE2].next = head[u];
    head[u] = cntE2++;
}

int dfn[N], low[N], idx;
int stk[N], top;
int in[N];
int kind[N], cnt;

void tarjan(int u)
{
    dfn[u] = low[u] = ++idx;
    in[u] = true;
    stk[++top] = u;
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
        else if (in[v]) low[u] = min(low[u], dfn[v]);
    }
    if (low[u] == dfn[u])
    {
        ++cnt;
        while (1)
        {
            int v = stk[top--];
            kind[v] = cnt;
            in[v] = false;
            if (v == u) break;
        }
    }
}

int opp[N], ind[N], col[N]; // 相对的点 入度 染色 col[]=1选择

bool topsort(int n) // 序号从0开始
{
    for (int i = 0; i < 2*n; i += 2)
    {
        int k1 = kind[i];
        int k2 = kind[i^1]; // 相对的两个点
        if (k1 == k2) return false;
        opp[k1] = k2;
        opp[k2] = k1;
    }
    memset(head, -1, sizeof head);
    int u, v;
    for (int i = 0; i < cntE; ++i)
    {
        u = edge[i].from, v = edge[i].to;
        if (kind[u] != kind[v])   // 反向建图
        {
            addedge2(kind[v], kind[u]);
            ind[kind[u]]++;
        }
    }
    queue<int> q;
    for (int i = 1; i <= cnt; ++i) if (!ind[i]) q.push(i);
    while (q.size())
    {
        u = q.front();
        q.pop();
        if (!col[u]) col[u] = 1, col[ opp[u] ] = -1;
        for (int i = head[u]; i != -1; i = edge2[i].next)
            if (--ind[edge2[i].to] == 0) q.push(edge2[i].to);
    }
    return true;
}

void init()
{
    cntE = cntE2 = 0;
    memset(head, -1, sizeof head);
    memset(dfn, 0, sizeof dfn);
    memset(in, false, sizeof in);
    idx = top = cnt = 0;
    memset(ind, 0, sizeof ind);
    memset(col, 0, sizeof col);
}
int main()
{
    int n;
    init();
    int m;
    scanf("%d%d",&n,&m);
    int a,b,c;
    char s[100];
    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d%s",&a,&b,&c,s);
        if(s[0]=='A')
        {
            if(c==1)
            {
                addedge(a*2+1,a*2);
                addedge(b*2+1,b*2);
            }

            else
            {
                addedge(a*2,b*2+1);
                addedge(b*2,a*2+1);
            }

        }
        if(s[0]=='O')
        {
            if(c==1)
            {
                //addedge(a*2,b*2);
                addedge(a*2+1,b*2);
                addedge(b*2+1,a*2);
                //addedge(b*2,a*2);
            }
            else
            {
                addedge(a*2,a*2+1);
                addedge(b*2,b*2+1);
            }

        }
        if(s[0]=='X')
        {
            if(c==1)
            {
                addedge(a*2,b*2+1);
                addedge(b*2+1,a*2);
                addedge(b*2,a*2+1);
                addedge(a*2+1,b*2);
            }
            else
            {
                addedge(a*2,b*2);
                addedge(b*2,a*2);
                addedge(a*2+1,b*2+1);
                addedge(b*2+1,a*2+1);
            }

        }
    }
    for(int i=0; i<2*n; i++)
        if(!dfn[i])
            tarjan(i);
    if(topsort(n))
        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、付费专栏及课程。

余额充值