Katu Puzzle
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11429   Accepted: 4233

Description

Katu Puzzle is presented as a directed graph G(VE) 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 ≤ X≤ 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 (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.
 
建图有问题,其他的很简单,容我仔细思考一下。
 
#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)的更多相关文章

  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. POJ 3678 Katu Puzzle (经典2-Sat)

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6553   Accepted: 2401 Descr ...

  3. POJ 3678 Katu Puzzle(强连通 法)

    题目链接 题意:给出a, b, c 和操作类型 (与或异或),问是否满足所有的式子 主要是建图: 对于 and , c == 1: 说明 a 和 b都是1,那么 0 就不能取, a' -> a ...

  4. POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9987   Accepted: 3741 Descr ...

  5. POJ 3678 Katu Puzzle (2-SAT,常规)

    题意:给出n个点,每个点上有一个数字可以0或1,然后给出m条限制,要求a和b两个点上的数字满足 a op b = c,op和c都是给定.问是否能够有一组解满足所有限制?(即点上的数字是0是1由你决定) ...

  6. 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 ...

  7. 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 ...

  8. poj 3678 Katu Puzzle(Two Sat)

    题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...

  9. POJ 3678 Katu Puzzle 2-SAT 强连通分量 tarjan

    http://poj.org/problem?id=3678 给m条连接两个点的边,每条边有一个权值0或1,有一个运算方式and.or或xor,要求和这条边相连的两个点经过边上的运算后的结果是边的权值 ...

随机推荐

  1. Python图形用户界面

    1.使用Tkinter创建图形用户界面的步骤 (1)导入Tkinter模块,使用import Tkinter或from Tkinter import * (2)创建顶层窗口对象容器,使用top = T ...

  2. Java面向对象之多态的静态和动态实现

    简单而言: 静态多态:即为重载,方法的重载 动态多态:即为重写/覆盖,方法的重写

  3. Bootstrap之信息记录

    Bootstrap中文网: http://www.bootcss.com/ 上面有一些资料和范例 实例精选: https://v3.bootcss.com/getting-started/#examp ...

  4. 转 My97日历控件常用功能记录

    My97相信大家都不陌生,应该是我所见过的最强大的一个日历控件了,最近的项目中也比较多地用到了此控件,而且项目中经常会有不同时间范围的需求,在此列出一些比较常用的日期范围格式的设置,尽管在My97的官 ...

  5. php重定向http请求

    302  临时重定向 301  永久重定向     (  302 和 301  的区别主要在于搜索引擎,搜索引擎一般不会抓取临时重定向的页面  ) 301 和302 适用于 普通的GET 请求: 如果 ...

  6. onbeforeunload事件两种写法及效果

    在符合W3C标准的浏览器里,可以使用addEventListener方法来添加事件. 当不需要为一个事件添加多个处理函数的时候,可以简单的使用onXXX=function(){}的方式来添加事件处理函 ...

  7. 学习 Spring (十四) Introduction

    Spring入门篇 学习笔记 Introduction 允许一个切面声明一个实现指定接口的通知对象,并且提供了一个接口实现类来代表这些对象 由 中的 元素声明该元素用于声明所匹配的类型拥有一个新的 p ...

  8. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  9. poj-1236(强连通分量)

    题意:给你n个点,每个点可能有指向其他点的单向边,代表这个点可以把软件传给他指向的点,然后解决两个问题, 1.问你最少需要给几个点,才能使所有点都能拿到软件: 2.问你还需要增加几条单向边,才能使任意 ...

  10. Python getattr() 函数

    Python getattr() 函数  Python 内置函数 描述 getattr() 函数用于返回一个对象属性值. 语法 getattr 语法: getattr(object, name[, d ...