2-sat——poj3678经典建图
比较经典的建图,详见进阶指南
2-sat一般要用到tarjan来求强连通分量
/*2-sat要加的是具有强制关系的边*/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 2005
#define M 2000005
struct Edge{int to,nxt;}e[M<<];
int n,m,head[N],tot;
void init(){
memset(head,-,sizeof head);
}
void add(int u,int v){
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
} int low[N],dfn[N],cnt,id[N],ind,stk[N],top,ins[N];
void tarjan(int x){
dfn[x]=low[x]=++ind;
stk[++top]=x;ins[x]=;
for(int i=head[x];i!=-;i=e[i].nxt){
int y=e[i].to;
if(!dfn[y]){
tarjan(y);
low[x]=min(low[x],low[y]);
}
else if(ins[y])
low[x]=min(low[x],low[y]);
}
if(dfn[x]==low[x]){
cnt++;int y;
do{
y=stk[top--];
ins[y]=;
id[y]=cnt;
}while(x!=y);
}
} int main(){
init();
int a,b,c;
char op[];
cin>>n>>m;
for(int i=;i<=m;i++){
scanf("%d%d%d %s",&a,&b,&c,op);
a++,b++;
if(op[]=='A'){
if(c==)add(a+n,b),add(b+n,a);
if(c==)add(a,a+n),add(b,b+n);
}
if(op[]=='O'){
if(c==)add(a+n,a),add(b+n,b);
if(c==)add(a,b+n),add(b,a+n);
}
if(op[]=='X'){
if(c==)add(a,b),add(b,a),add(a+n,b+n),add(b+n,a+n);
if(c==)add(a,b+n),add(a+n,b),add(b,a+n),add(b+n,a);
}
}
for(int i=;i<=*n;i++)
if(!dfn[i])tarjan(i);
for(int i=;i<=n;i++)
if(id[i]==id[i+n]){
puts("NO");
return ;
}
puts("YES");
}
2-sat——poj3678经典建图的更多相关文章
- hdoj--5093--Battle ships(二分图经典建图)
Battle ships Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- poj--1149--PIGS(最大流经典建图)
PIGS Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Submit Status D ...
- hdu 4185 Oil Skimming(二分图匹配 经典建图+匈牙利模板)
Problem Description Thanks to a certain "green" resources company, there is a new profitab ...
- POJ1149 最大流经典建图PIG
题意: 有一个人,他有m个猪圈,每个猪圈里都有一定数量的猪,但是他没有钥匙,然后依次来了n个顾客,每个顾客都有一些钥匙,还有他要卖猪的数量,每个顾客来的时候主人用顾客的钥匙打开相应的门,可 ...
- POJ 2226 最小点覆盖(经典建图)
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8881 Accepted: 3300 Desc ...
- BZOJ-1822 Frozen Nova 冷冻波 计(jie)算(xi)几何+二分+最大流判定+经典建图
这道逼题!感受到了数学对我的深深恶意(#‵′).... 1822: [JSOI2010]Frozen Nova 冷冻波 Time Limit: 10 Sec Memory Limit: 64 MB S ...
- zoj 3460 Missile【经典建图&&二分】
Missile Time Limit: 2 Seconds Memory Limit: 65536 KB You control N missile launching towers. Ev ...
- poj 1149 PIGS【最大流经典建图】
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18727 Accepted: 8508 Description ...
- Leapin' Lizards(经典建图,最大流)
Leapin' Lizards http://acm.hdu.edu.cn/showproblem.php?pid=2732 Time Limit: 2000/1000 MS (Java/Others ...
随机推荐
- Unity 如何修改 particle system 的 start color 属性
代码如下: ParticleSystem ps = GetComponent<ParticleSystem>(); var main = ps.main; var color = Colo ...
- function attributes, MDK
The keyword format is either of the following: __attribute__((attribute1, attribute2, ...)) __attrib ...
- 剑指offer——02二维数组中的查找
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...
- python学习2—python3特性与各种运算符
python学习2—python3特性与各种运算符 python3与python2相比具有的新特性 在python2中可以使用__future__模块调用python3的特性 print()函数必须带 ...
- 深夜Python - 第1夜 - for 迷 in 迷思
深夜Python - 第1夜 - for 迷 in 迷思 在一个月黑风高的夜晚,我悄悄打开编辑器,进入程序的世界.刚刚学会Python的我,由于一段时间的过度装B,被委托优化一段程序,我信心十足地接下 ...
- Linux网络配置 RPM命令 samba服务 Linux目录结构
第一种方法: (1)用root身份登录,运行setup命令进入到 text mode setup utiliy对网络进行配置,这里可以进行ip,子网掩码,默认网关,dns的设置.(2)这时网卡的配置没 ...
- linux 服务器安装mysql5.6
1.移除CentOS默认的mysql-libs: whereis mysql 2.为了避免冲突,先移除CenttOS上默认的mysql-libs: yum remove mysql-libs 3.然后 ...
- 数据库的元数据抽取SQL
一.数据库驱动类.端口.默认用户名密码 数据库 驱动 端口 用户名 密码 MySQL com.mysql.jdbc.Driver 3306 root root DB2 com.ibm.db2.jcc. ...
- 在vue项目引入阿里巴巴矢量图标
1.在阿里矢量图标库将想要的图标加入购物车,然后在购物车中将图标添加到项目: 2.到我的项目中,将图标下载到本地 3.在vue项目的assets文件夹下新建一个iconfont文件夹(名字自定义),将 ...
- redis笔记_源码_跳表skiplist
参照:https://juejin.im/post/57fa935b0e3dd90057c50fbc#comment http://redisbook.com/preview/skiplist/dat ...