POJ 3678 Katu Puzzle 2-SAT

本文介绍了一种解决KatuPuzzle问题的算法实现,该问题被定义为在一个带有布尔运算符标记的有向图中寻找每个节点的可行值。文章提供了完整的代码示例,解释了如何通过构建图并应用Tarjan算法来确定谜题是否可解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Katu Puzzle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8330 Accepted: 3069

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



WA了几发之后我想明白了一个问题。。然后过了>.<


/** Author: ¡î¡¤aosaki(*¡¯(OO)¡¯*)  niconiconi¡ï **/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
//#include <tuple>
#define ALL(v) (v).begin(),(v).end()
#define foreach(i,v) for (__typeof((v).begin())i=(v).begin();i!=(v).end();i++)
#define SIZE(v) ((int)(v).size())
#define mem(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lp(k,a) for(int k=1;k<=a;k++)
#define lp0(k,a) for(int k=0;k<a;k++)
#define lpn(k,n,a) for(int k=n;k<=a;k++)
#define lpd(k,n,a) for(int k=n;k>=a;k--)
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d %d",&a,&b)
#define lowbit(x) (x&(-x))
#define ll long long
#define pi pair<int,int>
#define vi vector<int>
#define PI acos(-1.0)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define TT cout<<"*****"<<endl;
#define TTT cout<<"********"<<endl;
inline int gcd(int a,int b)
{
    return a==0?b:gcd(b%a,a);
}

#define INF 1e9
#define eps 1e-8
#define mod 10007
#define maxn 5010
#define maxm 4000010
using namespace std;

int v[maxn],col[maxn];
int rea[maxn],low[maxn],stack[maxn];
int n,m,tot=0,color,tm=0,top=0;
int pre[maxn];

struct Side
{
    int to,next;
}e[maxm];

void add(int u,int v)
{
    e[tot].to=v;
    e[tot].next=pre[u];
    pre[u]=tot++;
}

void tarjan(int i)
{
    v[i]=1;
    top++;
    stack[top]=i;
    ++tm;
    rea[i]=tm;
    low[i]=tm;
    for(int j=pre[i];j!=-1;j=e[j].next)
        {
            int x=e[j].to;
            if(v[x]==0) tarjan(x);
            if(v[x]<2) low[i]=min(low[i],low[x]);
        }
    if(rea[i]==low[i])
    {
        color++;
        while(stack[top+1]!=i)
        {
            col[stack[top]]=color;
            v[stack[top]]=2;
            top--;
        }

    }

}


void init()
{
    mem(col);
    mem(v);
    mem1(pre);
    tot=0;
    top=0;
    tm=0;
    color=0;
    mem(low);
    mem(rea);
    mem(stack);
}


bool ok()
{
    lp(i,n)
    {
        if((col[i*2]==col[i*2+1]))
            return 0;
    }
    return 1;
}

int main()
{
     freopen("in.txt","r",stdin);
     int a,b,c;
     char s[10];
     while(~sc2(n,m))
     {
        init();
        lp(i,m)
        {
            sc(a);sc(b);sc(c);
            scanf("%s",s);
            a++; b++;
            if(s[0]=='A')
            {
                 if(c==1)
                {
                    add(a*2,a*2+1);
                    add(b*2,b*2+1);
                }
                else
                {
                    add(a*2+1,b*2);
                    add(b*2+1,a*2);
                }
            }

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

            }

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

        }
        lp(i,n*2)
          if(!rea[i])
             tarjan(i);
        if(ok()) printf("YES\n");
          else printf("NO\n");

     }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值