POJ 3678 Katu Puzzle (2-sat基础)

题意:每个数只有0,1两种可能,给出两两之间的AND,OR,XOR的值,判断有没有解

裸题。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 2010;
struct Edge
{
    int v,next;
}es[N*N];
int head[N];
int n,m;
int tmp[N],low[N],dfn[N],sta[N],top,scc,index;
int cnt;

inline void add_edge(int u,int v)
{
    es[cnt].v=v;
    es[cnt].next=head[u];
    head[u]=cnt++;
}
void tarjan(int u)
{

    low[u]=dfn[u]=++index;
    sta[++top]=u;
    tmp[u]=1;
    for(int i=head[u];~i;i=es[i].next)
    {
        int v=es[i].v;
        if(!tmp[v]) tarjan(v);
        if(tmp[v]==1) low[u]=min(low[u],low[v]);
    }
    if(low[u]==dfn[u])
    {
        scc++;
        do
        {
            int v=sta[top];
            tmp[v]=2;
            low[v]=scc;
        }while(sta[top--]!=u);
    }
}

bool judge()
{
    for(int i=0;i<2*n;i++) if(low[i]==low[i^1]) return false;
    return true;
}

void ini()
{
    memset(head,-1,sizeof(head));
    memset(tmp,0,sizeof(tmp));
    top=scc=index=cnt=0;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        ini();
        for(int i=1;i<=m;i++)
        {
            int u,v,c;
            char op[10];
            scanf("%d%d%d%s",&u,&v,&c,op);
            u<<=1,v<<=1;
            if(op[0]=='A')
            {
                if(c==1)add_edge(u,u^1),add_edge(v,v^1);
                else add_edge(u^1,v),add_edge(v^1,u);
            }
            else if(op[0]=='O')
            {
                if(c) add_edge(u,v^1),add_edge(v,u^1);
                else add_edge(u^1,u),add_edge(v^1,v);
            }
            else
            {
                if(c) add_edge(u,v^1),add_edge(v,u^1),add_edge(u^1,v),add_edge(v^1,u);
                else add_edge(u,v),add_edge(v,u),add_edge(u^1,v^1),add_edge(v^1,u^1);
            }
        }
        for(int i=0;i<2*n;i++) if(!tmp[i]) tarjan(i);
        if(judge()) puts("YES");
        else puts("NO");
    }
    return 0;
}
时间: 2024-08-28 19:01:17

POJ 3678 Katu Puzzle (2-sat基础)的相关文章

[2-SAT] poj 3678 Katu Puzzle

题目链接: http://poj.org/problem?id=3678 Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7888   Accepted: 2888 Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean operator 

POJ 3678 Katu Puzzle(2-sat 模板题)

题目链接:http://poj.org/problem?id=3678 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

poj 3678 Katu Puzzle(2-sat)

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) s

poj 3678 Katu Puzzle 2-SAT 建图入门

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) s

POJ - 3678 - Katu Puzzle(2SAT)

链接: https://vjudge.net/problem/POJ-3678 题意: 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 ver

POJ 3678 Katu Puzzle

2-SAT简单题.位运算写的时候忘记加括号WA了一发.... #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<stack> #include<algorithm> using namespace std; const int maxn=2000+10; int N,M; char s[1

POJ 3678 Katu Puzzle (2-SAT,常规)

题意:给出n个点,每个点上有一个数字可以0或1,然后给出m条限制,要求a和b两个点上的数字满足 a op b = c,op和c都是给定.问是否能够有一组解满足所有限制?(即点上的数字是0是1由你决定) 思路:题意很清晰了,难点在建图.要考虑所有可能的冲突: 当op为and: (1)c为0时,其中1个必为0. (2)c为1时,两者必为1.要加两条边,形如 a0->a1. 当op为or: (1)c为0时,两者必为0.要加两条边,形如 a1->a0. (2)c为1时,其中1个必为1. 当op为xor

POJ 3678 Katu Puzzle(强连通 法)

题目链接 题意:给出a, b, c 和操作类型 (与或异或),问是否满足所有的式子 主要是建图: 对于 and , c == 1: 说明 a 和 b都是1,那么 0 就不能取, a' -> a , b' - > b ,因为 a 和 a'是对立事件,对于 a' - >a说明,a'如果成立,那么a也一定存在,显然这是不可能的所以a'不会 成立的. c == 0 说明 a 和 b不全为1, a' -> b , b' -> a 对于 or,  c == 1 :说明 a 和 b 不全为

【POJ】3678 Katu Puzzle

http://poj.org/problem?id=3678 题意:很幼稚的题目直接看英文题面= = #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <iostream> using namespace std; const int N=1000*2+10, M=N*N*4; struct E { int next, to; }