poj3678 Katu Puzzle(2-SAT+经典建图)

Katu Puzzle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6669 Accepted: 2444

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中的经典建图,图样图森破啊

这题虽然很裸,但是很容易错。其他的还好,关键在AND和OR2个操作,容易漏条件。

当op=AND,c=1的时候,显然有a=b=1;很容易建边a->b,b->a。但其实这里还隐藏了一组条件:a'->a,b'->b,为什么呢,因为当a AND b = 1时,很明显a=b=1,那么a和b任意一个为0时肯定是不合法的。那么加上这组条件后,就把a=0和b=0的情况排除了。如果a和b有一个取0,肯定就能推出矛盾。

当op=OR,c = 0的时候同理。

详情请见代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 2005;
const int M = 1000001;
struct edge
{
    int to,next,c;
}g[M];
int head[N];
int scc[N];
int vis[N];
int stack1[N];
int stack2[N];
int n,m,num;
bool flag;

void init()
{
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    memset(scc,0,sizeof(scc));
    stack1[0] = stack2[0] = num = 0;
    flag = true;
}

void build(int s,int e)
{
    g[num].to = e;
    g[num].next = head[s];
    head[s] = num ++;
}

void dfs(int cur,int &sig,int &cnt)
{
    if(!flag)
        return;
    vis[cur] = ++sig;
    stack1[++stack1[0]] = cur;
    stack2[++stack2[0]] = cur;
    for(int i = head[cur];~i;i = g[i].next)
    {
        if(!vis[g[i].to])
            dfs(g[i].to,sig,cnt);
        else
        {
            if(!scc[g[i].to])
            {
                while(vis[stack2[stack2[0]]] > vis[g[i].to])
                    stack2[0] --;
            }
        }
    }
    if(stack2[stack2[0]] == cur)
    {
        stack2[0] --;
        ++cnt;
        do
        {
            scc[stack1[stack1[0]]] = cnt;
            int tmp = stack1[stack1[0]];
            if((tmp >= n && scc[tmp - n] == cnt) || (tmp < n && scc[tmp + n] == cnt))
            {
                flag = false;
                return;
            }
        }while(stack1[stack1[0] --] != cur);
    }
}

void Gabow()
{
    int i,sig,cnt;
    sig = cnt = 0;
    for(i = 0;i < n + n && flag;i ++)
        if(!vis[i])
            dfs(i,sig,cnt);
}

int main()
{
    int i,j;
    int a,b,c;
    char op[8];
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(i = 1;i <= m;i ++)
        {
            scanf("%d%d%d%s",&a,&b,&c,op);
            switch(op[0])
            {
            case 'A':if(c)
                    {
                        build(a + n,b + n);//0~n-1表示0,n~2n-1表示1
                        build(b + n,a + n);
                        build(a,a + n);//a!=0
                        build(b,b + n);//b!=0
                    }
                    else
                    {
                        build(a + n,b);
                        build(b + n,a);
                    }
                    break;
            case 'O':if(c)
                    {
                        build(a,b + n);
                        build(b,a + n);
                    }
                    else
                    {
                        build(a,b);
                        build(b,a);
                        build(a + n,a);//a!=1
                        build(b + n,b);//b!=1
                    }
                    break;
            case 'X':if(c)
                    {
                        build(a,b + n);
                        build(a + n,b);
                        build(b,a + n);
                        build(b + n,a);
                    }
                    else
                    {
                        build(a,b);
                        build(b,a);
                        build(a + n,b + n);
                        build(b + n,a + n);
                    }
            }
        }
        Gabow();
        if(flag)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}
//1080K	47MS




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值