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 ...
随机推荐
- 交换机安全学习笔记 第二章 MAC地址泛洪攻击
本文为书中相关知识的摘要,由于书中以思科设备为配置依据,所以笔记中补充了华为.H3C设备的相关配置.华为设备配置参考华为S2352EI 产品版本:V100R005C01文档版本:02. H3C配置参 ...
- CGAL 属性配置
libgmp-10.lib libmpfr-4.lib boost_system-vc120-mt-gd-1_63.lib D:\dev\CGAL-4.9\include D:\dev\CGAL-4. ...
- springboot处理单个文件上传
1. 引入pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</gro ...
- E - 卿学姐与城堡的墙(树状数组求逆序数)
卿学姐与城堡的墙 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- Java Web开发技术教程入门-静态网页技术
昨天了解了构建动态网站的几种技术:Servlet技术.JSP技术,ASP技术和ASP.NET技术以及PHP技术.昨天的精髓在于JSP技术的运行原理:通过用户请求JSP文件,首先检查JSP文件的 ...
- [.net core]5.outProcess
与inProcess比较 OutProcess性能更差,因为此时它使用了两个web服务器 ,内部是kestrel 外部可能是iis apache nginx 等. 使用visual studio ...
- VSCode使用Remote-SSH远程服务器
VSCode的Remote-SSH 之前一直使用的xshell5,现在在window上必须要升级方可使用,在mac上没法安装学习版.于是就想着vscode能不能实现这一需求. 微软开发了一个VSCod ...
- Asp.net Core中文转换成拼音
一.概述 之前使用.net framework,可以使用Microsoft Visual Studio International Feature Pack 1.0 进行转换,现在使用asp.net ...
- 【转载】jquery版的网页倒计时效果
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 在docker下运行mysql
docker pull mysql 从镜像仓库中拉取mysql镜像. 运行镜像 到此mysql在docker容器下运行成功. 使用Navicat连接工具连接到mysql 经过以上步骤就完成了在dock ...