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
OR
XOR

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.

有一个有向图G(V,E),每条边e(a,b)上有一个位运算符op(AND, OR或XOR)和一个值c(0或1)。

问能不能在这个图上的每个点分配一个值X(0或1),使得每一条边e(a,b)满足  Xa op Xb =  c

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=10+1000; int n,m,pre[maxn],lowlink[maxn],dfs_clock,sccno[maxn],scc_cnt;
vector<int> G[2*maxn];
stack<int> S; void read()
{
int a,b,c;char s[10];
scanf("%d %d %d %s",&a,&b,&c,s);
if(s[0]=='A')
{
if(c)
{
G[a].push_back(a+n);
G[b].push_back(b+n);
}
else
{
G[a+n].push_back(b);
G[b+n].push_back(a);
}
}
else if(s[0]=='O')
{
if(!c)
{
G[a+n].push_back(a);
G[b+n].push_back(b);
}
else
{
G[a].push_back(b+n);
G[b].push_back(a+n);
}
}
else if(s[0]=='X')
{
if(c)
{
G[a].push_back(b+n);
G[a+n].push_back(b);
G[b].push_back(a+n);
G[b+n].push_back(a);
}
else
{
G[a].push_back(b);
G[a+n].push_back(b+n);
G[b].push_back(a);
G[b+n].push_back(a+n);
}
}
} void tarjan(int u)
{
pre[u]=lowlink[u]=++dfs_clock;
S.push(u);
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(!pre[v])
{
tarjan(v);
lowlink[u]=min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
lowlink[u]=min(lowlink[u],pre[v]);
} if(lowlink[u]==pre[u])
{
scc_cnt++;
while(1)
{
int x=S.top();S.pop();
sccno[x]=scc_cnt;
if(x==u) break;//找到了当前强连通的起始节点就退出,<br>//不然会破坏其他强连通分量
}
}
} void find_scc()
{
MM(pre,0);
MM(sccno,0);
scc_cnt=dfs_clock=0;
for(int i=0;i<n;i++)
if(!pre[i])
tarjan(i);
} int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=0;i<2*n;i++) G[i].clear();
for(int i=1;i<=m;i++)
read(); find_scc(); int flag=1;
for(int i=0;i<n;i++)
if(sccno[i]==sccno[i+n])
{
flag=0;
break;
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}

  2-sat的建图参考;

http://blog.csdn.net/u011466175/article/details/23048459?utm_source=tuicool&utm_medium=referral

http://blog.csdn.net/leolin_/article/details/7215871

TTTTTTTTTTTTTT POJ 3678 与或异或 2-SAT+强连通 模板题的更多相关文章

  1. POJ 2387 Til the Cows Come Home --最短路模板题

    Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...

  2. POJ 2409 Let it Bead【Polya定理】(模板题)

    <题目链接> 题目大意:用k种颜色对n个珠子构成的环上色,旋转.翻转后相同的只算一种,求不等价的着色方案数. 解题分析: 对于这种等价计数问题,可以用polay定理来解决,本题是一道pol ...

  3. poj 1679 The Unique MST (次小生成树模板题)

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  4. POJ - 2774 Long Long Message (后缀数组/后缀自动机模板题)

    后缀数组: #include<cstdio> #include<algorithm> #include<cstring> #include<vector> ...

  5. POJ 2387 Til the Cows Come Home (dijkstra模板题)

    Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...

  6. poj 2007 凸包构造和极角排序输出(模板题)

    Scrambled Polygon Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10841   Accepted: 508 ...

  7. HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT

    一条边<u,v>表示u选那么v一定被选. #include <iostream> #include <cstring> #include <cstdio> ...

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

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

  9. poj 2236:Wireless Network(并查集,提高题)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 677 ...

随机推荐

  1. 阿里云安装filezilla

    1.连接服务器 ssh 或者 远程连接 到服务器: 2.安装相应软件 安装EPEL,EPEL是yum的一个软件源,里面包含了许多基本源里没有的软件: yum -y install epel-relea ...

  2. gdb笔记 ---《Linux.C编程一站式学习》

    gdb笔记 ---<Linux.C编程一站式学习> 单步执行和跟踪函数调用 函数调试实例 #include <stdio.h> int add_range(int low, i ...

  3. C语言--浮点数

    在程序中使用浮点数 -- 浮点数的精确性有限 -- 在从c语言中float类型的精确度只到小数点的7位 -- 浮点数只能在一定范围内去相信它 -- 在有精确度高的要求下不要使用浮点数(在算钱的时候,误 ...

  4. 新版 Scrapy 中 sys.conf.settings 的替代方法

    新版 Scrapy 中 sys.conf.settings 的替代方法 在 scrapy 项目目录下,有个 settings.py 文件,此文件是用来存放爬虫项目的各种配置,比如说 MongoDB 的 ...

  5. python接口、抽象类与抽象方法

    接口: -url -数据类型,python不存在 class 类名 1.类中的方法可以写任意多个 2.如果想要对类中的方法做约束,就需要写接口 接口中定义一个方法f1,可以约束继承他的子类 class ...

  6. 使用T4模板为EF框架添加数据库实体注释(转)

    1. 下载文件GetSummery.ttinclude2. 把我们下载下来的文件解压,将解压出来的文件放入解决方案中3. 修改下app.config,添加一个连接字符串: <add name=& ...

  7. Flask开发系列之快速入门

    Flask开发系列之快速入门 文档 一个最小的应用 调试模式 路由 变量规则 构造 URL HTTP 方法 静态文件 模板渲染 访问请求数据 环境局部变量 请求对象 文件上传 Cookies 重定向和 ...

  8. Delphi 语句

  9. Oracle 之 触发器

    触发器是特定的事件出现的时候,自动隐式执行代码块,这个过程用户无法控制,用户只能控制触发的事件,触发后的操作,触发过程是自动执行的. 定义触发器: 1.名称 2.触发时间:是在执行事件之前(befor ...

  10. docker命令(随时补充)

    导入导出容器:https://blog.csdn.net/LEoe_/article/details/78685156 拷贝文件到容器内:docker cp 本地路径 容器长ID:容器路径