POJ3678 Katu Puzzle【2-SAT】

题意:给每个点赋0或1,问是否有方案,满足所有的条件

思路:对于每个条件,找出它矛盾的情况,(A、B矛盾,建边A->B' B->A')

比如 a & b = 1, (0,0) (1,0) (0,1)矛盾,建边,跑Tarjan,对应点的在同一分量即无解

如果一定要选A的话,也可以连一条A'->A的边


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<list>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;
typedef long long ll;
const int maxn = 2005;
const int maxm = 4e6+5;
struct edge
{
	int to,next;
	bool cut;
}ed[maxm];
int head[maxn], tot;  
int Low[maxn], DFN[maxn], Stack[maxn];
int Index, top;
bool Instack[maxn]; 
int block,belong[maxn];  
int bridge;

void init()
{
	memset(head, -1, sizeof(head));  
    memset(DFN, 0, sizeof(DFN));   
    memset(Instack, false, sizeof(Instack));   
    block = tot = 0;
    Index = top = 0;
    bridge = 0; 
}

void Tarjan(int u,int pre)
{
    int v;
    Low[u] = DFN[u] = ++Index;
    Stack[top++] = u;
    Instack[u] = true;
    for(int i = head[u];i != -1;i = ed[i].next)
    {
        v = ed[i].to;
        //if(v == pre)continue; //无向无重边 用,只要不往回走 
        //if(ed[i].id == pre) continue; //无向图重边判定,不走同一条边 
        if( !DFN[v] )
        {
            Tarjan(v,u);
            //Tarjan(v,ed[i].id); //无向图重边判定
            if( Low[u] > Low[v] )Low[u] = Low[v];
            if(Low[v] > DFN[u])
            {
                bridge++;
                ed[i].cut = true;
                ed[i^1].cut = true;
            }
        }
        else if( Instack[v] && Low[u] > DFN[v] )
            Low[u] = DFN[v];
    }
    if(Low[u] == DFN[u])
    {
        block++;
        do
        {
            v = Stack[--top];
            Instack[v] = false;
            belong[v] = block;
        }
        while( v!=u );
    }
}//使无向图双连通,(叶子+1) / 2

void add(int x,int y)
{
	ed[tot].to = y;
	ed[tot].next = head[x];
	ed[tot].cut = 0;
	//ed[tot].id = num; //无向图重边判定 
	head[x] = tot++;
}

void build(int a,int b,int c,string ch,int n)
{
	if(ch == "AND")
	{
		if(c == 1)//0 0, 0 1, 1 0
		{
			add(a+n,a);
			add(b+n,b);
			add(a+n,b+n);
			add(b,a);
			add(a,b);
			add(b+n,a+n);
		}
		else // 1 1
		{
			add(a,b+n);
			add(b,a+n);
		}
	}
	else if(ch == "OR")
	{
		if(c == 1) // 0 0
		{
			add(a+n,b);
			add(b+n,a);
		}
		else // 1 1 , 0 1 , 1 0 
		{
			add(a,b+n);
			add(b,a+n);
			add(a+n,b+n);
			add(b,a);
			add(a,b);
			add(b+n,a+n);
		}
	}
	else
	{
		if(c == 1) //1 1, 0 0 
		{
			add(a+n,b);
			add(b+n,a);
			add(a,b+n);
			add(b,a+n);			
		}
		else //1 0, 0 1
		{
			add(a+n,b+n);
			add(b,a);
			add(a,b);
			add(b+n,a+n);
		}
	}
}

void SCC(int n)  
{  
    for(int i = 0; i < n; i++)  
        if(!DFN[i])  
            Tarjan(i,-1);  
}


int main(void)
{
	int n,m,a,b,c;
	string ch;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		init();
		for(int i = 1; i <= m; i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			cin>>ch;
			build(a,b,c,ch,n);
		}
		SCC(2*n);
		int flag = 1;
		for(int i = 0; i < n; i++)
		{
			if(belong[i] == belong[i+n])
			{
				flag = 0;
				break;
			}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
		/*for(int i = 0; i < n; i++)
		{
			printf("%d%c",belong[i]<belong[i+n]?1:0,i==n-1?'\n':' ');
		}*/
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值