poj3678Katu Puzzle

点为0或1,看满足m个条件时,是否有解

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN = 1010;
struct twosat
{
    int n,c;
    vector<int> g[MAXN<<1];
    bool mark[MAXN<<1];
    int s[MAXN<<1];    

    bool dfs(int x)
    {
        int i;
        if (mark[x^1])
            return 0;
        if (mark[x])
            return 1;
        mark[x] = 1;
        s[c++] = x;
        for (i = 0; i < g[x].size(); i++)
            if (!dfs(g[x][i]))
                return 0;
        return 1;
    }    

    void init(int n)
    {
        int i,t=n<<1;
        this->n = n;
        for (i = 0; i < t; i++)
            g[i].clear();
        memset(mark, 0, sizeof(mark));
    }    

    void add_clause(int x,int xval,int y,int yval)
    {
    	x=x*2+xval;
    	y=y*2+yval;
        g[x].push_back(y^1);
        g[y].push_back(x^1);
    }    

    bool solve()
    {
        int i,t=n<<1;
        for (i = 0; i < t; i += 2)
            if (!mark[i] && !mark[i + 1])
            {
                c = 0;
                if (!dfs(i))
                {
                    while (c > 0)
                        mark[s[--c]] =0;
                    if (!dfs(i + 1))
                        return 0;
                }
            }
        return 1;
    }
}ender;
int work(int a,int b,int k)
{
	return k==0?a&b:k==1?a|b:a^b;
}
int main()
{
	char s[100];
	int a,b,c,n,m,x,y,k;
	while(cin>>n>>m)
	{
		ender.init(n);
		while(m--)
		{
			scanf("%d%d%d%s",&a,&b,&c,s);
			k=strcmp(s,"AND")==0?0:strcmp(s,"OR")==0?1:2;
			for(x=0;x<2;x++)
				for(y=0;y<2;y++)
					if(work(x,y,k)!=c)
						ender.add_clause(a,x,b,y);
		}
		cout<<(ender.solve()?"YES":"NO")<<endl;

	}
	return 0;
}

Katu Puzzle

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8007   Accepted: 2943

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) 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 a (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

时间: 2024-10-09 22:31:43

poj3678Katu Puzzle的相关文章

UVa - 227 - Puzzle

给空格子上下左右的互换操作,问最后是怎样的 注意一行的最后一个若是空格,需要自己加注意读取时 操作可能分好多行,一定要读取到 0 为止 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 char map[50][50],op[1000],c,tmp; 5 int k,t,x,y,cnt; 6 bool flag; 7 void fuc() 8 { 9 flag=1; 10 for(int i=0;

HDU 5465 Clarke and puzzle Nim游戏+二维树状数组

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5465 Clarke and puzzle Accepts: 42 Submissions: 269 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 克拉克是一名人格分裂患者.某一天,有两个克拉克(aa和bb)在玩一个方格游戏. 这个方格是一个n*mn∗m的矩阵,每个格子里有一

HDU 1098 Ignatius&#39;s puzzle 费马小定理+扩展欧几里德算法

题目大意: 给定k,找到一个满足的a使任意的x都满足 f(x)=5*x^13+13*x^5+k*a*x 被65整除 推证: f(x) = (5*x^12 + 13 * x^4 + ak) * x 因为x可以任意取 那么不能总是满足 65|x 那么必须是 65 | (5*x^12 + 13 * x^4 + ak) 那么就是说 x^12 / 13 + x^4 / 5 + ak / 65 正好是一个整数 假设能找到满足的a , 那么将 ak / 65 分进x^12 / 13 + x^4 / 5中得到

HDU 4708 Rotation Lock Puzzle (贪心+模拟)

Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1668    Accepted Submission(s): 530 Problem Description Alice was felling into a cave. She found a strange door with a number

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Alice was felling into a cave. She found a strange door with a number square m

Aizu 0121 Seven Puzzle (康托展开+bfs)

Seven Puzzle Time Limit : 1 sec, Memory Limit : 65536 KB 7パズルは8つの正方形のカードとこれらのカードがぴたりと収まる枠を使って行います.それぞれのカードは互いに区別できるように.0,1,2....7と番号がつけられています.枠には.縦に2個.横に4個のカードを並べることができます. 7パズルを始めるときには.まず枠にすべてのカードを入れます.枠のなかで0のカードだけは.上下左右に隣接するカードと位置を交換することができます.たとえば.枠

杭电1097-A hard puzzle

Problem Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.this puzzle describes that: gave a and b,how to know

uva live 12846 A Daisy Puzzle Game

如果下一个状态有必败,那么此时状态一定是必胜,否则此时状态一定是必败 状压DP #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include&