HDU 3062 Party(2-SAT模版题)
m: 表示有m 对矛盾关系 ( m < (n - 1) * (n -1))
在接下来的m行中,每行会有4个数字,分别是 A1,A2,C1,C2
A1,A2分别表示是夫妻的编号
C1,C2 表示是妻子还是丈夫 ,0表示妻子 ,1是丈夫
夫妻编号从 0 到 n -1
否则输出 NO
#include <cstdio>
#include <cstring>
using namespace std; const int MAXN = ;
const int MAXM = **; struct TwoSAT{
int n, ecnt;
bool mark[MAXN];
int St[MAXN], c;//手动栈
int head[MAXN];
int next[MAXM], to[MAXM]; bool dfs(int x){
if(mark[x^]) return false;
if(mark[x]) return true;
mark[x] = true;
St[c++] = x;
for(int p = head[x]; p; p = next[p])
if(!dfs(to[p])) return false;
return true;
} void init(int n){
this->n = n;
ecnt = ;
memset(head,,sizeof(head));
memset(mark,,sizeof(mark));
} void addEdge1(int x, int y){//x*y=false
to[ecnt] = y^; next[ecnt] = head[x]; head[x] = ecnt++;
to[ecnt] = x^; next[ecnt] = head[y]; head[y] = ecnt++;
} void addEdge2(int x, int y){//x+y=true
to[ecnt] = y; next[ecnt] = head[x^]; head[x^] = ecnt++;
to[ecnt] = x; next[ecnt] = head[y^]; head[y^] = ecnt++;
} bool solve(){
for(int i = ; i < n*; i += )
if(!mark[i] && !mark[i+]){
c = ;
if(!dfs(i)) {
while(c>) mark[St[--c]] = false;
if(!dfs(i^)) return false;
}
}
return true;
}
} G; int main(){
int n, m, a, b, c, d;
while(scanf("%d%d",&n,&m)!=EOF){
G.init(n);
for(int i = ; i < n; ++i) G.addEdge2(i*,(i+n)*);
while(m--){
scanf("%d%d%d%d",&a,&b,&c,&d);
G.addEdge1((a + n*c)*, (b + n*d)*);
}
if(G.solve()) printf("YES\n");
else printf("NO\n");
}
return ;
}
#include <cstdio>
#include <cstring>
using namespace std; const int MAXN = ;
const int MAXM = **; struct TwoSAT{
int n, ecnt, dfs_clock, scc_cnt;
int St[MAXN], c;//手动栈
int head[MAXN], lowlink[MAXN], pre[MAXN], sccno[MAXN];
int next[MAXM], to[MAXM]; void dfs(int u){
pre[u] = lowlink[u] = ++dfs_clock;
St[++c] = u;
for(int p = head[u]; p; p = next[p]){
int &v = to[p];
if(!pre[v]){
dfs(v);
if(lowlink[u] > lowlink[v]) lowlink[u] = lowlink[v];
}else if(!sccno[v]){
if(lowlink[u] > pre[v]) lowlink[u] = pre[v];
}
}
if(lowlink[u] == pre[u]){
scc_cnt++;
while(true){
int x = St[c--];
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} void init(int n){
this->n = n;
ecnt = ; dfs_clock = scc_cnt = ;
memset(head,,sizeof(head));
memset(sccno,,sizeof(sccno));
memset(pre,,sizeof(pre));
} void addEdge1(int x, int y){//x*y=false
to[ecnt] = y^; next[ecnt] = head[x]; head[x] = ecnt++;
to[ecnt] = x^; next[ecnt] = head[y]; head[y] = ecnt++;
} bool solve(){
for(int i = ; i < n; ++i)
if(!pre[i]) dfs(i);
for(int i = ; i < n; i += )
if(sccno[i] == sccno[i^]) return false;
return true;
}
} G; int main(){
int n, m, a, b, c, d;
while(scanf("%d%d",&n,&m)!=EOF){
G.init(*n);
while(m--){
scanf("%d%d%d%d",&a,&b,&c,&d);
G.addEdge1(a* + c, b* + d);
}
if(G.solve()) printf("YES\n");
else printf("NO\n");
}
return ;
}
tarjan
HDU 3062 Party(2-SAT模版题)的更多相关文章
- hdu 1213(并查集模版题)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1083 Courses(最大匹配模版题)
题目大意: 一共有N个学生跟P门课程,一个学生可以任意选一 门或多门课,问是否达成: 1.每个学生选的都是不同的课(即不能有两个学生选同一门课) 2.每门课都有一个代表(即P门课都被成功选过 ...
- Go Deeper HDU - 3715(2 - sat 水题 妈的 智障)
Go Deeper Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 1712 ACboy needs your help (分组背包模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...
- hdu 4825 Xor Sum(01字典树模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- hdu 1286 找新朋友 欧拉函数模版题
找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Des ...
- 【hdu 1576】A/B(数论--拓展欧几里德 求逆元 模版题)
题意:给出 A%9973 和 B,求(A/B)%9973的值. 解法:拓展欧几里德求逆元.由于同余的性质只有在 * 和 + 的情况下一直成立,我们要把 /B 转化为 *B-1,也就是求逆元. 对于 B ...
随机推荐
- 剑指Offer_编程题之替换空格
题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy.
- 写shell脚本需要注意哪些地方----零基础必看
shell脚本是完全靠自学的,每一步需要注意的问题都是我自己亲自实践出来的,对于大神可能看来是小儿科,但是对于新手,是必须注意的 一.首先执行 echo $SHELL查看本机的解释器, 二.开始写脚本 ...
- bootstrap-01-学习记录
1.bootstrap所有插件依赖JQ,必须在JQ之后引入. 2.bootstrap分预编译版(css,js,fonts)和源码版(less,js,fonts,dist->预编译版内容,docs ...
- Array方法学习小结
原生js forEach()和map()遍历 A:相同点: 1.都是循环遍历数组中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前 ...
- Python文本和字符串常用操作
## 字符串分割 line = "This is my love!" fields = line.split(' ') print(fields) # ['This', 'is', ...
- MySQL数据库 : 函数和存储过程
CONCAT 可以把多个字符串连在一起,如把 doc_id 和 title这两个字段的查询结果组合成一个字符串:SELECT CONCAT(doc_id,title) FROM simhash; CO ...
- PHP留言板的实现思路
本文实例为大家分享了php留言板的实现思路,供大家参考,具体内容如下:1.创建一个存放留言信息的文件名2.获取表单中的数据给一个变量3.判断文件的时候存在4.对文件执行写的操作,在这之前,注意打开文件 ...
- rails中发送ajax请求
最近在写一个blog系统练练手,遇到一个一个问题,用户添加评论的时候想发送ajax请求,但是rails里的ajax和Python中的不太一样,Python中的ajax是用js,jquery实现的和ra ...
- 用kubeadm构建k8s集群部署
一.环境 三台centos机器 二.软件及容器准备 1.安装docker环境 本例安装 docker-ce版本,repo源为docker-ce.repo文件,拷贝到 /etc/yum.repos.d下 ...
- C语言实验报告(四)完全数
完全数,又称完美数或者完备数.是一些特殊的自然数.它所有的真因子的和,恰好等于它本身.编程找出1000以内的所有完全数,并输出该数成为完全数的因子. (例如6=1+2+3.按照6,its factor ...