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> ...
随机推荐
- Mybatis设置自增主键
useGeneratedKeys="true" keyProperty="id" 方法1: <insert id="insert" p ...
- 这两天写的mybatis配置文件,主要是有输出和输入的存储过程
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- ListView 条目加载上滑下滑首尾缩放动画实现
要实现这个效果,只需要再适配器getView之前,给每个条目的view设置相应的动画即可. 首先需要2个动画的xml文件. 在res下新建anim文件夹:(res/anim) 第一个动画xml文件: ...
- java面向对象之 类和对象
OOP:Object Oriented Programming(面向对象编程) 面向对象: 1:将复杂的事情简单化. 2:面向对象将以前的过程中的执行者,变成了指挥者. 3:面向对象这种思想是符合现在 ...
- Ubuntu Server修改IP、DNS、hosts
本文记录下Ubuntu Server 16.04修改IP.DNS.hosts的方法 -------- 1. Ubuntu Server 修改IP sudo vi /etc/network/interf ...
- BOOL、sizeof
BOOL使用前需要声明 #include <stdbool.h>(这个头文件定义了bool,true,false等宏) int a[5]; sizeof(a[5]),sizeof是关键字, ...
- Is it possible to implement a Firebug-like “inspect element” DOM element highlighter with client-side JavaScript?
Is it possible to implement a Firebug-like "inspect element" DOM element highlighter with ...
- 160G 视频教程(Java+Android+项目视频)免费下载
我不喜欢多说没用,直接给下载链接,进去直接下载,下载不动的联系网站客服解决!我只和我的好朋友们分享好的视频教程 http://edu.csdn.net/main/video.shtml 视频教程目录过 ...
- CF#52 C Circular RMQ (线段树区间更新)
Description You are given circular array a0, a1, ..., an - 1. There are two types of operations with ...
- IE7下浮动元素的内容自动换行的BUG解决方法
有时候我们想写个浮动得到这样的效果: 代码: <!doctype html> <html> <head> <meta charset="utf-8& ...