POJ 3678 Katu Puzzle (2-SAT)

题目地址:POJ 3678

算是2-SAT裸题了。。分类讨论就行了。。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
#define LL __int64
const int INF=0x3f3f3f3f;
int head[2100], cnt, index, top, ans;
int dfn[2100], low[2100], belong[2100], instack[2100], stak[2100];
struct node
{
    int u, v, next;
}edge[10000000];
void add(int u, int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void tarjan(int u)
{
    dfn[u]=low[u]=++index;
    instack[u]=1;
    stak[++top]=u;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u])
    {
        ans++;
        while(1)
        {
            int v=stak[top--];
            belong[v]=ans;
            instack[v]=0;
            if(u==v) break;
        }
    }
}
void init()
{
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(instack,0,sizeof(instack));
    cnt=ans=top=index=0;
}
int main()
{
    int n, m, i, j, a, b, c;
    char s[10];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        while(m--)
        {
            scanf("%d%d%d%s",&a,&b,&c,s);
            if(!strcmp(s,"AND"))
            {
                if(c==0)
                {
                    add(a<<1,b<<1|1);
                    add(b<<1,a<<1|1);
                }
                else
                {
                    add(a<<1,b<<1);
                    add(b<<1,a<<1);
                    add(a<<1|1,a<<1);
                    add(b<<1|1,b<<1);
                }
            }
            else if(!strcmp(s,"OR"))
            {
                if(c==0)
                {
                    add(a<<1|1,b<<1|1);
                    add(b<<1|1,a<<1|1);
                    add(a<<1,a<<1|1);
                    add(b<<1,b<<1|1);
                }
                else
                {
                    add(a<<1|1,b<<1);
                    add(b<<1|1,a<<1);
                }
            }
            else
            {
                if(c==0)
                {
                    add(a<<1|1,b<<1|1);
                    add(a<<1,b<<1);
                    add(b<<1|1,a<<1|1);
                    add(b<<1,a<<1);
                }
                else
                {
                    add(a<<1|1,b<<1);
                    add(a<<1,b<<1|1);
                    add(b<<1|1,a<<1);
                    add(b<<1,a<<1|1);
                }
            }
        }
        for(i=0;i<n<<1;i++)
        {
            if(!dfn[i])
                tarjan(i);
        }
        int flag=0;
        for(i=0;i<n;i++)
        {
            if(belong[i<<1]==belong[i<<1|1])
            {
                flag=1;
                break;
            }
        }
        if(flag) puts("NO");
        else puts("YES");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值