题目

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.

输入格式

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 a line containing "YES" or "NO".

输入样例

4 4

0 1 1 AND

1 2 1 OR

3 2 0 AND

3 0 0 XOR

输出样例

YES

提示

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

题解

跪了。。。就因为n << 1写成了1 << n QAQ

这题加深了我对2-sat建图的理解,建边就表示选择了起点就必须选择终点

对于每个限制条件,我们分别考虑选择x的不同值

AND

为1,则x0->x1,y0->y1,让x0,y0自相矛盾,无法选择

为0,则x0->y1,y0->x1

OR

为1,则x0->y1,y0->x1

为0,则x1->x0,y1->y0

XOR

为1,则x0->y1,x1->y0,y1->x0,y0->x1

为0,则x0->y0,x1->y1,y0->x0,y1->x1

tarjan判断一下x0和x1是否在同一个强联通分量即可

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(x) memset(x,0,sizeof(x))
using namespace std;
const int maxn = 4005,maxm = 4000005,INF = 1000000000;
int n,m,h[maxn],ne;
struct EDGE{int to,nxt;}ed[maxm];
void build(int u,int v){ed[ne] = (EDGE){v,h[u]}; h[u] = ne++;}
int dfn[maxn],low[maxn],Scc[maxn],st[maxn],scci,top,cnt;
void dfs(int u){
dfn[u] = low[u] = ++cnt;
st[++top] = u;
Redge(u){
if (!dfn[to = ed[k].to]){
dfs(to);
low[u] = min(low[u],low[to]);
}else if (!Scc[to]) low[u] = min(low[u],dfn[to]);
}
if (dfn[u] == low[u]){
scci++;
do{Scc[st[top]] = scci;}while (st[top--] != u);
}
}
char opt[10];
int main(){
while (~scanf("%d%d",&n,&m)){
int a,b,v,x0,x1,y0,y1; cnt = scci = top = 0; ne = 1;
cls(dfn); cls(h); cls(Scc); cls(low);
while (m--){
scanf("%d%d%d%s",&a,&b,&v,opt);
x0 = a << 1; x1 = x0 | 1; y0 = b << 1; y1 = y0 | 1;
if (opt[0] == 'A'){
if (v) build(x0,x1),build(y0,y1);
else build(x1,y0),build(y1,x0);
}
else if (opt[0] == 'O'){
if (v) build(x0,y1),build(y0,x1);
else build(x1,x0),build(y1,y0);
}
else if (opt[0] == 'X'){
if (v) build(x0,y1),build(y0,x1),build(x1,y0),build(y1,x0);
else build(x1,y1),build(y0,x0),build(x0,y0),build(y1,x1);
}
}
for (int i = 0; i < 2 * n; i++) if (!dfn[i]) dfs(i);
bool flag = true;
for (int i = 0; i < n; i++)
if (Scc[i << 1] == Scc[i << 1 | 1]){
flag = false; break;
}
if (flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}

POJ3678 Katu Puzzle 【2-sat】的更多相关文章

  1. POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang

    Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...

  2. poj3678 Katu Puzzle 2-SAT

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6714   Accepted: 2472 Descr ...

  3. POJ-3678 Katu Puzzle 2sat

    题目链接:http://poj.org/problem?id=3678 分别对and,or,xor推出相对应的逻辑关系: 逻辑关系 1 0  A and B     A'->A,B'->B ...

  4. POJ3678 Katu Puzzle

    原题链接 \(2-SAT\)模板题. 将\(AND,OR,XOR\)转换成\(2-SAT\)的命题形式连边,用\(tarjan\)求强连通分量并检验即可. #include<cstdio> ...

  5. POJ1651 Multiplication Puzzle【区间DP】

    LINK 每次删除一个数,代价是左右两边相邻的数的当前数的积 第一个和最后一个数不能删除 问最后只剩下第一个数的最后一个数的最小代价 思路 很简单的DP 正着考虑没有办法确定两边的数 那么就把每个区间 ...

  6. poj 1651 Multiplication Puzzle【区间DP】

    题目链接:http://poj.org/problem? id=1651 题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n) 同一时候ans+=a ...

  7. USACO4.4 Shuttle Puzzle【bfs+优化】

    直接上$bfs$,每一个状态记录下当前字符串的样子,空格的位置,和走到这个状态的答案. 用空格的位置转移,只有$50pts$ 考虑到题目一个性质:$W$只往右走,$B$只往左走,就可以过了. #inc ...

  8. poj2893 M*N puzzle 【n*m数码问题小结】By cellur925

    题目传送门 这个问题是来源于lydrainbowcat老师书上讲排序的一个扩展.当时讲的是奇数码问题,其实这种问题有两种问法:一种局面能否到另一种局面.到达目标局面的最小步数. 本文部分内容引用于ly ...

  9. 【codeforces 761E】Dasha and Puzzle

    [题目链接]:http://codeforces.com/contest/761/problem/E [题意] 给你一棵树,让你在平面上选定n个坐标; 使得这棵树的连接关系以二维坐标的形式展现出来; ...

随机推荐

  1. js清空表单数据的方式(遍历+reset)

    方法1:遍历页面元素 /* 清空FORM表单内容 id:表单ID*/ function ClearForm(id) { var objId = document.getElementById(id); ...

  2. 踩坑日志!viser-ng的使用

    在ng-alian项目中使用viser图表库,在app.module中引用了viser-ng,然而,在具体的html项目中使用<v-chart>会报错,提示v-chart不是一个angul ...

  3. 阿里云服务器下安装LAMP环境(CentOS Linux 6.3)(1)

    阿里的云服务器准备好以后,我们首先要做的就是把自己购买的磁盘空间挂载到系统里面,我们为服务器选择的是 Linux 系统,确切说的是 CentOS 系统. 默认阿里云服务器带了一个 20G 的空间,一般 ...

  4. 使用 Repeater方式和完全静态页面使用AJAX读取和提交数据

    1.使用Repeater方式: Comments.aspx <html xmlns="http://www.w3.org/1999/xhtml"> <head r ...

  5. java基础面试题:Math.round(11.5)等於多少? Math.round(-11.5)等於多少?

    package com.swift; public class Math_Round { public static void main(String[] args) { /* * Math roun ...

  6. DOS当中的基本操作命令,如何切换磁盘,如何查看文件和文件夹,如何清屏,进入文件夹的命令,javac是什么意思,作用是什么?DOS如何建文件夹?退出文件夹?

    如何切换磁盘:使用盘符+:举例 d: 如何查看文件和文件夹 dir/w 如何清屏: cls (clear screen) 进入文件夹的命令cd ,举例cd JDK javac是什么意思,c 是comp ...

  7. JSPatch - iOS 动态补丁

    JSPatch库,支持在线更新iOS应用,目前BDN项目中有用到,主要用来修复线上Crash和Bug 相关博文推荐: JSPatch – 动态更新iOS APP(这是JSPatch作者的博文) JSP ...

  8. 短短几行css代码实现滚动条效果

    如何实现使用css实现滚动条效果 实现效果,运用线性渐变来实现功能 假设我们的页面被包裹在 <body> 中,可以滚动的是整个 body,给它添加这样一个从左下到到右上角的线性渐变: bo ...

  9. 1045: [HAOI2008] 糖果传递

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4897  Solved: 2457[Submit][Status][Discuss] Descript ...

  10. SAP后台JOB的Submit & EVENT JOB

    SM35执行一个后台作业后,想及时停止, 运行SM37后,点击ctr + F1停止活动的作业,系统根本就没反应. 解决方法: 第一步:SM50, 找到,Ty.列为BGD的(Background),然后 ...