TTTTTTTTTTTTTT POJ 3678 与或异或 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
有一个有向图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
TTTTTTTTTTTTTT POJ 3678 与或异或 2-SAT+强连通 模板题的更多相关文章
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- POJ 2409 Let it Bead【Polya定理】(模板题)
<题目链接> 题目大意:用k种颜色对n个珠子构成的环上色,旋转.翻转后相同的只算一种,求不等价的着色方案数. 解题分析: 对于这种等价计数问题,可以用polay定理来解决,本题是一道pol ...
- poj 1679 The Unique MST (次小生成树模板题)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- POJ - 2774 Long Long Message (后缀数组/后缀自动机模板题)
后缀数组: #include<cstdio> #include<algorithm> #include<cstring> #include<vector> ...
- 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 ...
- poj 2007 凸包构造和极角排序输出(模板题)
Scrambled Polygon Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10841 Accepted: 508 ...
- HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT
一条边<u,v>表示u选那么v一定被选. #include <iostream> #include <cstring> #include <cstdio> ...
- POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9987 Accepted: 3741 Descr ...
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
随机推荐
- 20191209 Linux就该这么学(1-3)
1. 部署虚拟环境安装 Linux 系统 RPM 是为了简化安装的复杂度,而 Yum软件仓库是为了解决软件包之间的依赖关系. 2. 新手必须掌握的Linux命令 通常来讲,计算机硬件是由运算器.控制器 ...
- Spring MVC 中使用AOP 进行事务管理--注解实现
注解实现实现事务管理很简单,和配置式差不多,配置文件的头文件也要加相应的支持.配置数据源,并开启事务管理支持即可. <bean id="transactionManager" ...
- vue-loader介绍和单页组件介绍
$ \es6\sing-file> npm install vue-loader@14.1.1 -D vue-template-compiler@2.5.17 -D npm install v ...
- POJ - 2421 Constructing Roads(最小生成树&并查集
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- hdu3829 二分匹配 最大独立集
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Problem ...
- php 图像处理 抠图,生成背景透明png 图片
*自定义一个图片等比缩放函数 *@param string $picname 被缩放图片名 *@param string $path 被缩放图片路径 *@param int $maxWidth 图片被 ...
- ExpressionToSQL
ExpressionToSql using System; using System.Collections.Generic; using System.Collections.ObjectModel ...
- LeetCode题目(python)
1.给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样 ...
- 在ubuntu上使用Openresty+lua实现WAF----折腾笔记
1.1 参考loveshell的waf实现思路,再此感谢下面其中一部分是转载 1.2 WAF的功能 支持IP白名单和黑名单功能,直接将黑名单的IP访问拒绝.支持URL白名单,将不需要过滤的URL进行定 ...
- 在window环境下安装symfony2框架注意事项
首先先放上放上安装的教程:http://www.symfonychina.com/doc/current/setup.html 然后说一下笔者遇到的坑 在运行这条命令的时候,虽然成功下载读取下来了,但 ...