POJ 3678 Katu Puzzle (2-SAT)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 11429 | Accepted: 4233 |
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<vector>
#include<stack>
#include<cstdio>
using namespace std;
int n,m;
vector<int>u[200024];
stack<int>st;
int dfn[200024],sig,low[200024],color[20024],index;
bool book[200024];
void init()
{
scanf("%d%d",&n,&m);
char s[10];
int x,y,c;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&c);
scanf("%s",s);
if(s[0]=='A'){
if(c==1){
u[y].push_back(y+n);//不理解
u[x].push_back(x+n);//不理解
}
if(c==0){
u[x+n].push_back(y);
u[y+n].push_back(x);
}
}
else if(s[0]=='X'){
if(c==1){
u[x+n].push_back(y);
u[x].push_back(y+n);
u[y+n].push_back(x);
u[y].push_back(x+n);
}
if(c==0){
u[x+n].push_back(y+n);
u[x].push_back(y);
u[y+n].push_back(x+n);
u[y].push_back(x);
}
}
else if(s[0]=='O'){
if(c==1){
u[x].push_back(y+n);
u[y].push_back(x+n);
}
if(c==0){
u[y+n].push_back(y);//不理解
u[x+n].push_back(x);//不理解
}
} }
} void tarjan(int t)
{
dfn[t]=low[t]=++index;
book[t]=true;
st.push(t);
int siz=u[t].size();
for(int i=0;i<siz;i++){
if(!dfn[u[t][i]]){
tarjan(u[t][i]);
low[t]=min(low[t],low[u[t][i]]);
}
if(book[u[t][i]]){
low[t]=min(low[t],low[u[t][i]]);
}
}
int miui;
if(dfn[t]==low[t]){
sig++;
while(true){
if(st.empty()){break;}
miui=st.top();
st.pop();
book[miui]=false;
color[miui]=sig;
if(miui==t){break;}
}
}
} bool solve()
{
for(int i=0;i<n;i++){
if(!dfn[i]){
tarjan(i);
}
} for(int i=0;i<n;i++){
if(color[i]==color[i+n]&&color[i]!=0){
return false;
}
}
return true;
} int main()
{
init();
if(solve()){
printf("YES\n");
}
else printf("NO\n");
}
POJ 3678 Katu Puzzle (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)
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,合取范式大集合)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9987 Accepted: 3741 Descr ...
- POJ 3678 Katu Puzzle (2-SAT,常规)
题意:给出n个点,每个点上有一个数字可以0或1,然后给出m条限制,要求a和b两个点上的数字满足 a op b = c,op和c都是给定.问是否能够有一组解满足所有限制?(即点上的数字是0是1由你决定) ...
- 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 建图入门
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(Two Sat)
题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...
- POJ 3678 Katu Puzzle 2-SAT 强连通分量 tarjan
http://poj.org/problem?id=3678 给m条连接两个点的边,每条边有一个权值0或1,有一个运算方式and.or或xor,要求和这条边相连的两个点经过边上的运算后的结果是边的权值 ...
随机推荐
- Operation not allowed on a unidirectional dataset错误?
关于网友提出的“ Operation not allowed on a unidirectional dataset错误?”问题疑问,本网通过在网上对“ Operation not allowed o ...
- 关于事务回滚,rollback tran到底要不要写?
关于事务回滚,有些不明白,不知道rollback tran在什么时候用. begin tran update 表1 update 表2 commit tran 这种写法,在更新表1或表2时出错,事务会 ...
- indexOf刚开始写成IndexOf出错
{{# if(d.fronturlmin ==null||d.fronturlmin ==""){ }} <img src="@System.Configurati ...
- 01.javascript之数据类型
1.数据类型 JavaScript一共有六种数据类型.(ES6新增了第七种Symbol类型的值) 数值(Number) 字符串(String) 布尔值(boolean) undefined null ...
- cuda编程-并行规约
利用shared memory计算,并避免bank conflict:通过每个block内部规约,然后再把所有block的计算结果在CPU端累加 代码: #include <cuda_runti ...
- 做接口自动化时候,一些登录头信息可以通过aop的方式进行增强
做接口自动化时候,一些登录头信息可以通过aop的方式进行增强
- iPhone
电报交流***vpnpay app不能用了,只要能下载就可以用,谨慎卸载 升级.网络链接不成功的,多试几次,毕竟程序不会运行你的错误操作 ,重新添加链接服务器,继续搞!耐心的进行下面操作即可链接,如有 ...
- hdu-4300(kmp或者拓展kmp)
题意:乱七八糟说了一大堆,就是先给你一个长度26的字符串,对应了abcd....xyz,这是一个密码表.然后给你一个字符串,这个字符串是不完整的(完整的应该是前半部分是加密的,后半部分是解密了的),然 ...
- hdu 5652(并查集)
题意:很久之前,在中国和印度之间有通路,通路可以简化为一个n*m的字符串,0表示能通过,1表示障碍,每过一年就有一个坐标变成1,问你什么时候,通路彻底无法通过: 解题思路:无向图的连通性,一般直接搜索 ...
- 洛谷 P1102 A−B数对
题目描述 出题是一件痛苦的事情! 题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的 A+BA+BA+B ProblemProblemProblem ,改用 A−BA-BA−B 了哈哈! 好吧,题目是这 ...