[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(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:

AND 0 1
0 0 0
1 0 1
OR 0 1
0 0 1
1 1 1
XOR 0 1
0 0 1
1 1 0

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

X0 = 1, X1 = 1, X2 = 0, X3 = 1.

Source

POJ Founder Monthly Contest – 2008.07.27, Dagger

[Submit]   [Go Back]   [Status]  
[Discuss]

题目意思:

给一幅有向图,每条边代表连接的两个节点的一种运算,并且告诉运算后的值,问能否有一种节点取值,使得各边都满足边的运算。

解题思路:

2-SAT

i代表1 ~i带表0

当i&j==1时,建边  ~i->i     ~j->j

当i&j==0时,建边   i->~j    j->~i  ~i->~j   ~j->~i

当i|j==1时,建边    ~i->j     ~j->i

当i|j==0时,建边   i->~i    j->~j

当i^j==1时,建边  i->~j  ~i->j   j->~i  ~j->i

当i^j==0时,建边  i->j  ~i->~j   j->i  ~j->~i

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 2200

int n,m;
vector<vector<int> >myv;
int low[Maxn],dfn[Maxn],sc,bc,dep;
int in[Maxn],sta[Maxn];
bool iss[Maxn];

void tarjan(int cur)
{
    int ne;
    low[cur]=dfn[cur]=++dep;
    sta[++sc]=cur;
    iss[cur]=true;

    for(int i=0;i<myv[cur].size();i++)
    {
        ne=myv[cur][i];
        if(!dfn[ne])
        {
            tarjan(ne);
            low[cur]=min(low[cur],low[ne]);
        }
        else if(iss[ne]&&dfn[ne]<low[cur])
            low[cur]=dfn[ne];
    }
    if(low[cur]==dfn[cur])
    {
        ++bc;
        do
        {
            ne=sta[sc--];
            in[ne]=bc;
            iss[ne]=false;
        }while(ne!=cur);
    }
}
void solve()
{
    sc=bc=dep=0;

    memset(dfn,0,sizeof(dfn));
    memset(iss,false,sizeof(iss));
    for(int i=0;i<2*n;i++)
        if(!dfn[i])
            tarjan(i);
}

int main()
{
    //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   while(~scanf("%d%d",&n,&m))
   {
       myv.clear();
       myv.resize(2*n+10);

       for(int i=1;i<=m;i++)
       {
           int a,b,c;
           char ss[10];
           scanf("%d%d%d%s",&a,&b,&c,ss);
           if(*ss=='A')
           {
               if(c==1)
               {
                   myv[2*a].push_back(2*a+1);
                   myv[2*a+1].push_back(2*b+1);
                   myv[2*b].push_back(2*b+1);
                   myv[2*b+1].push_back(2*a+1);
               }
               else
               {
                   myv[2*a+1].push_back(2*b);
                   myv[2*b+1].push_back(2*a);
               }
           }
           else if(*ss=='O')
           {
               if(c==1)
               {
                   myv[2*a].push_back(2*b+1);
                   myv[2*b].push_back(2*a+1);
               }
               else
               {
                   myv[2*a+1].push_back(2*a);
                   myv[2*a].push_back(2*b);
                   myv[2*b].push_back(2*a);
                   myv[2*b+1].push_back(2*b);
               }
           }
           else
           {
               if(c==1)
               {
                   myv[2*a].push_back(2*b+1);
                   myv[2*a+1].push_back(2*b);
                   myv[2*b].push_back(2*a+1);
                   myv[2*b+1].push_back(2*a);
               }
               else
               {
                   myv[2*a].push_back(2*b);
                   myv[2*a+1].push_back(2*b+1);
                   myv[2*b].push_back(2*a);
                   myv[2*b+1].push_back(2*a+1);
               }
           }
       }
       solve();
       bool ans=true;

       for(int i=0;i<n;i++)
       {
           if(in[2*i]==in[2*i+1])
           {
               ans=false;
               break;
           }
       }
       if(ans)
            printf("YES\n");
       else
            printf("NO\n");
   }
    return 0;
}
时间: 2024-12-21 17:46:35

[2-SAT] poj 3678 Katu Puzzle的相关文章

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 (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],

【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; }