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) 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:
|
|
|
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack> using namespace std;
const int maxn = 1e5 + ;
const int mod = 1e9 + ;
const int INF = 0x7ffffff;
struct node {
int v, next;
} edge[maxn];
int head[maxn], dfn[maxn], low[maxn];
int s[maxn], belong[maxn], instack[maxn];
int tot, cnt, top, flag, n, m;
void init() {
tot = cnt = top = flag = ;
memset(s, , sizeof(s));
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn));
memset(instack, , sizeof(instack));
}
void add(int u, int v ) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void tarjan(int v) {
dfn[v] = low[v] = ++flag;
instack[v] = ;
s[top++] = v;
for (int i = head[v] ; ~i ; i = edge[i].next ) {
int j = edge[i].v;
if (!dfn[j]) {
tarjan(j);
low[v] = min(low[v], low[j]);
} else if (instack[j]) low[v] = min(low[v], dfn[j]);
}
if (dfn[v] == low[v]) {
cnt++;
int t;
do {
t = s[--top];
instack[t] = ;
belong[t] = cnt;
} while(t != v) ;
}
}
int check() {
for (int i = ; i < n ; i++)
if (belong[ * i] == belong[ * i + ]) return ;
return ;
}
int main() {
while(scanf("%d%d", &n, &m) != EOF) {
if (n == && m == ) break;
init();
char op[];
int x, y, c;
for (int i = ; i < m ; i++) {
scanf("%d%d%d%s", &x, &y, &c, op);
if (op[] == 'A') {
if (c) {
add( * x + , * x);
add( * y + , * y);
} else {
add( * x, * y + );
add( * y, * x + );
}
}
if (op[] == 'O') {
if (c) {
add( * x + , * y);
add( * y + , * x);
} else {
add( * x, * x + );
add( * y, * y + );
}
}
if (op[] == 'X') {
if (c) {
add( * x, * y + );
add( * x + , * y);
add( * y, * x + );
add( * y + , * x);
} else {
add( * x + , * y + );
add( * x, * y);
add( * y + , * x + );
add( * y, * x);
}
} }
for (int i = ; i < * n ; i++)
if (!dfn[i]) tarjan(i);
if (check()) printf("YES\n");
else printf("NO\n");
}
return ;
}
poj 3678 Katu Puzzle 2-SAT 建图入门的更多相关文章
- poj 3678 Katu Puzzle(Two Sat)
题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...
- [poj 3678]Katu Pazzle[2-SAT常用建图法]
题意: 不说了..典型的2-SAT 常用模型: 重点: 突出"绑定性". 连线表示限制而非可行. 因为最后要求对立点不在同一强连通分量是说同一强连通中的点必须同时选. 坑: 首先是 ...
- 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 ...
- poj 3678 Katu Puzzle(2-sat)
Description Katu Puzzle ≤ c ≤ ). One Katu ≤ Xi ≤ ) such that for each edge e(a, b) labeled by op and ...
- POJ 3678 Katu Puzzle (2-SAT)
Katu Puzzle Time Limit: 1000MS ...
- POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9987 Accepted: 3741 Descr ...
- POJ 3678 Katu Puzzle (经典2-Sat)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6553 Accepted: 2401 Descr ...
- POJ 3678 Katu Puzzle(强连通 法)
题目链接 题意:给出a, b, c 和操作类型 (与或异或),问是否满足所有的式子 主要是建图: 对于 and , c == 1: 说明 a 和 b都是1,那么 0 就不能取, a' -> a ...
- POJ 3678 Katu Puzzle (2-SAT,常规)
题意:给出n个点,每个点上有一个数字可以0或1,然后给出m条限制,要求a和b两个点上的数字满足 a op b = c,op和c都是给定.问是否能够有一组解满足所有限制?(即点上的数字是0是1由你决定) ...
随机推荐
- node服务端渲染(完整demo)
简介 nodejs搭建多页面服务端渲染 技术点 koa 搭建服务 koa-router 创建页面路由 nunjucks 模板引擎组合html webpack打包多页面 node端异步请求 服务端日志打 ...
- scrapy--json(360美图)
之前开始学习scrapy,接触了AJax异步加载.一直没放到自己博客,趁现在不忙,也准备为下一个爬虫做知识储存,就分享给大家. 还是从爬取图片开始,先上图给大家看看成果,QAQ. 一.图片加载的方法 ...
- 使用命令行设置MySql编码格式
使用命令行设置MySql编码格式 1.登录mysql 2.输入 SHOW VARIABLES LIKE 'character_set_%'; 3.查看 value值是否为utf8,如果不是,则使用SE ...
- anaconda+jupyter notebook 安装配置
安装Anaconda 从清华大学开源软件镜像站选择合适自己的版本 wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda ...
- hive 学习系列一(数据类型的定义)
数字类型(Numeric Types) 整型 TINYINT(取值范围:-128 -- 127) SMALLINT(取值范围:-32,768 to 32,767) INT/INTEGER(取值范围: ...
- js石头剪刀布小游戏
代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...
- Too many parameters: expected 1, was given 2 Query: SELECT count(id) FROM `user` WHERE username = ?; Parameters: [org.apache.commons.dbutils.handlers.ScalarHandler@453da22c, [李明]]
public Object getValue(String sql,Object... args) { Connection conn = null; Object obj= null; try { ...
- echart搭配时间轴进行展示 (本例展示的是多时间 多地区 多指标条件 )
1:照常先来几张图 看效果 2:首先 看官方文档 我把echart官方的例子给扒下来并整理了得出如下效果 上 案例图和代码 效果图 : 代码: <style type="text/c ...
- Mybatis常用xml
工作中mybatis常用的xml代码 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ma ...
- curl 编译
curl是利用URL语法在命令行方式下工作的开源文件传输工具.它被广泛应用在Unix.多种Linux发行版中,并且有DOS和Win32.Win64下的移植版本.支持很多协议:FTP, FTPS, HT ...