Problem Description
有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席。在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的。有没有可能会有n 个人同时列席?
 
Input
n: 表示有n对夫妻被邀请 (n<= 1000)
m: 表示有m 对矛盾关系 ( m < (n - 1) * (n -1))

在接下来的m行中,每行会有4个数字,分别是 A1,A2,C1,C2 
A1,A2分别表示是夫妻的编号 
C1,C2 表示是妻子还是丈夫 ,0表示妻子 ,1是丈夫
夫妻编号从 0 到 n -1 

 
Output
如果存在一种情况 则输出YES 
否则输出 NO
 
思路:2-SAT模版题,分别要求夫妻必上一个、仇人不能同时上
 
两个代码,第一个是普通的搜索,第二个是tarjan的解法,第一个593MS,第二个625MS(难道写挫了。。。?),(好吧用改一下第二个代码用C++交变成了312MS……)
 
 #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模版题)的更多相关文章

  1. hdu 1213(并查集模版题)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. HDU 1083 Courses(最大匹配模版题)

    题目大意: 一共有N个学生跟P门课程,一个学生可以任意选一 门或多门课,问是否达成:    1.每个学生选的都是不同的课(即不能有两个学生选同一门课)   2.每门课都有一个代表(即P门课都被成功选过 ...

  3. Go Deeper HDU - 3715(2 - sat 水题 妈的 智障)

    Go Deeper Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  4. HDU 2222 Keywords Search(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  5. HDU 1712 ACboy needs your help (分组背包模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...

  6. hdu 4825 Xor Sum(01字典树模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...

  7. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  8. hdu 1286 找新朋友 欧拉函数模版题

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

  9. 【hdu 1576】A/B(数论--拓展欧几里德 求逆元 模版题)

    题意:给出 A%9973 和 B,求(A/B)%9973的值. 解法:拓展欧几里德求逆元.由于同余的性质只有在 * 和 + 的情况下一直成立,我们要把 /B 转化为 *B-1,也就是求逆元. 对于 B ...

随机推荐

  1. Struts2+Spring+Hibernate整合开发(Maven多模块搭建)

    Struts2+Spring+Hibernate整合开发(Maven多模块搭建) 0.项目结构 Struts2:web层 Spring:对象的容器 Hibernate:数据库持久化操作 1.父模块导入 ...

  2. Python模拟校园网登录

    最近忙着实验室的项目,学习的时间相对较少.前一段时间刚开始接触python时,依葫芦画瓢照着写了一个爬虫,爬取了某个网站的图片.当看到一张张图片自动出现在电脑屏幕上时,有些小小成就感.我想大多数人开始 ...

  3. eclipse 安装 lombok

    转载自http://bbs.itmayiedu.com/article/1527769518449 由于项目中有 @Slf4j 注解等,而 eclipse 需要安装 lombok 插件才能正常编译.由 ...

  4. delphi 2010以上 安装 第三方控件

    delphi-“can't be installed because it is not a design time package. 一定要先装 dcl*.dpk ----------------- ...

  5. Spring Cloud之 Config Server 使用ip端口号配置高可用

    先看官方文档的配置 --- spring: profiles: peer1 eureka: instance: hostname: peer1 client: serviceUrl: defaultZ ...

  6. html 的radio单选框如何实现互斥------radio只是input的type属性

    先看看没有互斥的情况: <html> <body> 男性:<input type="radio" id="male" /> ...

  7. python学习笔记:第18天 面向对象04-反射

    目录 issubclass和isinstance 区分函数和方法 反射 issubclass和isinstance issubclass:可以判断一个类是否另一个类的子类. # issubclass ...

  8. php+IIS 配置环境(windows环境)

    继php7+apache2.4 配置环境(window环境)后,由于B2C项目准备上线:特此小编在阿里云上搭建PHP7环境,为此特写上搭建过程希望正处于搭建php7+IIS(windows环境)中的朋 ...

  9. Codeforces Round #482 (Div. 2) :B - Treasure Hunt

    题目链接:http://codeforces.com/contest/979/problem/B 解题心得: 这个题题意就是三个人玩游戏,每个人都有一个相同长度的字符串,一共有n轮游戏,每一轮三个人必 ...

  10. 钓鱼 洛谷p1717

    题目描述 话说发源于小朋友精心设计的游戏被电脑组的童鞋们藐杀之后非常不爽,为了表示安慰和鼓励,VIP999决定请他吃一次“年年大丰收”,为了表示诚意,他还决定亲自去钓鱼,但是,因为还要准备2013NO ...