hdoj 3062 Party(2-SAT)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3062
思路分析:将问题转换为2-SAT问题,需要注意的是将命题转换为有向图的方法;命题中A1, A2, C1, C2表示C1与C2不能同时出现,所以A1中C1出现等价于A2中C2 ^ 1出现,同理A2中C2出现等价于A1中C1^1出现,则形成了两条有向边;
代码如下:
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std; const int MAX_N = + ;
struct TwoSAT {
int n;
vector<int> G[ * MAX_N];
bool mark[ * MAX_N];
int S[ * MAX_N], c; void Init(int n)
{
this->n = n;
for (int i = ; i <= * n; ++i)
G[i].clear();
memset(mark, , sizeof(mark));
} bool Dfs(int x)
{
if (mark[x ^ ]) return false;
if (mark[x]) return true;
mark[x] = true;
S[c++] = x; for (int i = ; i < G[x].size(); ++i)
if (!Dfs(G[x][i]))
return false;
return true;
} void AddClause(int x, int x_val, int y, int y_val)
{
x = * x + x_val;
y = * y + y_val;
G[x].push_back(y ^ );
G[y].push_back(x ^ );
} bool Solve()
{
for (int i = ; i < * n; i += )
{
if (!mark[i] && !mark[i + ])
{
c = ;
if (!Dfs(i))
{
while (c > ) mark[S[--c]] = false;
if (!Dfs(i + )) return false;
}
}
}
return true;
}
}; TwoSAT sat; int main()
{
int n, m; while (scanf("%d", &n) != EOF)
{
scanf("%d", &m);
sat.Init(n);
for (int i = ; i < m; ++i)
{
int x, x_val, y, y_val;
scanf("%d %d %d %d", &x, &y, &x_val, &y_val);
sat.AddClause(x, x_val, y, y_val);
}
bool ans = sat.Solve();
if (ans)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
hdoj 3062 Party(2-SAT)的更多相关文章
- [2-sat]HDOJ1824 Let's go home
中问题 题意略 和HDOJ 3062一样 这里 每个队员都有 选 和 不选 两种, 即 上篇所说的$x$和$x’$ 建图:队长(a)留下或者其余两名队员(b.c)同时留下 那么就是$a' \Right ...
- [2-sat]HDOJ3062 Party
中文题 题意略 学2-sat啦啦啦 2-sat就是 矛盾的 ($x.x’$不能同时取) m对人 相互也有限制条件 取出其中n个人 也有可能是把一件东西分成 取/不取 相矛盾的两种情况 (那就要拆 ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 多边形碰撞 -- SAT方法
检测凸多边形碰撞的一种简单的方法是SAT(Separating Axis Theorem),即分离轴定理. 原理:将多边形投影到一条向量上,看这两个多边形的投影是否重叠.如果不重叠,则认为这两个多边形 ...
- HDOJ 1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- hdoj 1385Minimum Transport Cost
卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...
- HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT
一条边<u,v>表示u选那么v一定被选. #include <iostream> #include <cstring> #include <cstdio> ...
随机推荐
- 消息处理之performSelector
performSelector和直接调用方法的区别 performSelector: withObject:是在iOS中的一种方法调用方式.他可以向一个对象传递任何消息,而不需要在编译的时候声明这些方 ...
- Javascript获取浏览器版本
前两天有朋友请教我这个问题,说现在网上的资料太过于混乱不完整,我这里先简单整理一部分. 0 (function(){ 1 2 var ver = window.navigator.userAgen ...
- Scala io操作
1. 读文件 scala特有的是scala.io.Source,例如: import scala.io._ Source.fromFile(“cn.scala”,”utf8”).mkString 逐行 ...
- 让IE6也能智能控制图片最大宽、高度
当一个图片的宽度或高度超出了容器时,我们一般会用max-width或max-height来设置其最大宽.高度,让图片不会超出容器,但是如果同时设置了最大高度和最大宽度时,有可能会造成图片最终显示会有些 ...
- java中文件保存、打开文件对话框
package com.soft.test; //AWT: FileDialog类 + FilenameFilter类 可以实现本功能 //Swing: JFileChooser类 + FileFil ...
- brief introduction JAVA new I/O (NIO)
Reference document: Getting started with new I/O (NIO) Preface: NIO was introduced with JDK1.4 for h ...
- 转: javascript模块加载框架seajs详解
javascript模块加载框架seajs详解 SeaJS是一个遵循commonJS规范的javascript模块加载框架,可以实现javascript的模块化开发和模块化加载(模块可按需加载或全部加 ...
- HDU 5782 Cycle(KMP+Hash)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5782 [题目大意] 给出两个字符串,判断他们每一个前缀是否循环同构,循环同构的意思就是,字符串首位 ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Linux系统编程——进程调度浅析
概述 操作系统要实现多进程.进程调度不可缺少. 有人说,进程调度是操作系统中最为重要的一个部分.我认为这样的说法说得太绝对了一点,就像非常多人动辄就说"某某函数比某某函数效率高XX倍&quo ...