POJ3678 Katu Puzzle(2-sat tanjar判矛盾)

Katu Puzzle

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10546 Accepted: 3913

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:

AND01OR01XOR01
000001001
101111110

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.
Source

POJ Founder Monthly Contest – 2008.07.27, Dagger


题目的意思是给出n个数你可以自由选择是0还是1,然后给出m组逻辑关系,问是否存在一种取值是的逻辑关系全成立

典型的2-sat判断,直接根据逻辑关系建图然后tanjar缩点判断矛盾

#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <cmath>  
#include <map>  
#include <set>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <bitset>  

using namespace std;

#define LL long long  
const int INF = 0x3f3f3f3f;
#define MAXN 100100  
#define MAXM 1000100  

struct node
{
    int u, v, next;
} edge[MAXM];


int dfn[MAXN], low[MAXN], s[MAXN], Stack[MAXN], in[MAXN], block[MAXN];
int cnt, tot, index, bnum,n;

void init()
{
    memset(s, -1, sizeof s);
    memset(dfn, 0, sizeof dfn);
    memset(low, 0, sizeof low);
    memset(Stack, 0, sizeof Stack);
    memset(in, 0, sizeof in);
    memset(block, 0, sizeof block);
    cnt = tot = index = bnum = 0;
}

void add(int u, int v)
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].next = s[u];
    s[u] = cnt++;
}

void tarjan(int x)
{
    dfn[x] = low[x] = ++tot;
    Stack[++index] = x;
    in[x] = 1;
    for (int i = s[x]; ~i; i = edge[i].next)
    {
        int v = edge[i].v;
        if (!dfn[v])
        {
            tarjan(v);
            low[x] = min(low[x], low[v]);
        }
        else if (in[v])
        {
            low[x] = min(low[x], low[v]);
        }
    }
    if (dfn[x] == low[x])
    {
        bnum++;
        do
        {
            //printf("%d ",Stack[index]);  
            block[Stack[index]] = bnum;
            in[Stack[index--]] = 0;
        } while (Stack[index + 1] != x);
        // printf("\n");  
    }
}

bool solve()
{
    for (int i = 1; i <= n; i++)
        if (!dfn[i])
            tarjan(i);

    for (int i = 0; i<2*n; i+=2)
    {
        if (block[i] == block[i ^ 1])
            return 0;
    }
    return 1;
}

int main()
{
    int m,a,b,c;
    char s[100];
    while (~scanf("%d%d", &n, &m))
    {
        init();
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d%d%s", &a, &b, &c, s);
            if (!strcmp(s, "AND"))
            {
                if (c == 1)
                {
                    add(2 * a+1, 2 * a);
                    add(2 * b + 1, 2 * b);
                }
                else
                {
                    add(2 * a, 2 * b + 1);
                    add(2 * b, 2 * a + 1);
                }
            }
            else if (!strcmp(s, "OR"))
            {
                if (c == 0)
                {
                    add(2 * a , 2 * a + 1);
                    add(2 * b , 2 * b + 1);
                }
                else
                {
                    add(2 * a+1, 2 * b);
                    add(2 * b+1, 2 * a);
                }
            }
            else
            {
                if (c == 0)
                {
                    add(2 * a, 2 * b);
                    add(2 * b, 2 * a);
                    add(2 * a+1, 2 * b + 1);
                    add(2 * b+1, 2 * a + 1);
                }
                else
                {
                    add(2 * a, 2 * b+1);
                    add(2 * b, 2 * a+1);
                    add(2 * a + 1, 2 * b);
                    add(2 * b + 1, 2 * a);

                }
            }

        }
        if (solve()) printf("YES\n");
            else printf("NO\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值