题目

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. 2018.2.7 css 的一些方法盒子模型

    css 的一些方法 1.盒模型代码简写 盒模型的外边距(margin).内边距(padding)和边框(border)设置上下左右四个方向的边距是按照顺时针方向设置的:上右下左.具体应用在margin ...

  2. 如何更改VirtualBox虚拟电脑内存大小

  3. vbs自由选择启动bat文件

    1.首先创建一个文件夹用来放bat文件和配置文件. 2.然后在bat文件中写入启动程序.中间红色框是启动程序,外面程序是用来隐藏命令提示符的. 3.txt配置文件配置vbs启动项,vbs只能找到此文件 ...

  4. Oracle 事务 锁

    一. 事务 是一系列的数据库操作,是数据库应用的基本逻辑单位以及并发控制的基本单位.所谓的事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位. 要将有组语句作为事务考 ...

  5. MongoDB+nodejs查询并返回数据

    const express = require('express');const router = express.Router(); const Monk = require('monk');con ...

  6. 1911: [Apio2010]特别行动队

    Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 5706  Solved: 2876[Submit][Status][Discuss] Descriptio ...

  7. Zabbix监控告警Lack of free swap space on Zabbix server解决办法

    报错详情如下: 是因为Zabbix监控没有考虑虚拟主机的交换空间情况 解决办法修改配置 修改表达式内容:{Template OS Linux:system.swap.size[,pfree].last ...

  8. c++运算符重载和虚函数

    运算符重载与虚函数 单目运算符 接下来都以AClass作为一个类例子介绍 AClass{ int var } 区分后置++与前置++ AClass operator ++ () ++前置 一般设计为返 ...

  9. 5.Cisco Packet Tracer里关于交换机或路由器配置文件和系统映像备份与恢复

    我们会将交换机或路由器的配置文件和系统镜像直接备份到tftp服务器上,所以我们需要准备一台tftp的服务器 1我们需要给服务器配一个ip地址,给路由器的f0/1端口配置一个ip地址,路由器与服务器能相 ...

  10. Net core 轮子

    .net core 使用的人渐渐多了起来,轮子也渐渐多了起来,为了避免重复造轮子,以下列举了一些造好的轮子 1. IP 请求频率限制 git: https://github.com/stefanpro ...