题目链接: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)的更多相关文章

  1. [2-sat]HDOJ1824 Let's go home

    中问题 题意略 和HDOJ 3062一样 这里 每个队员都有 选 和 不选 两种, 即 上篇所说的$x$和$x’$ 建图:队长(a)留下或者其余两名队员(b.c)同时留下 那么就是$a' \Right ...

  2. [2-sat]HDOJ3062 Party

    中文题 题意略 学2-sat啦啦啦 2-sat就是    矛盾的 ($x.x’$不能同时取) m对人 相互也有限制条件 取出其中n个人 也有可能是把一件东西分成 取/不取 相矛盾的两种情况 (那就要拆 ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  5. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. 多边形碰撞 -- SAT方法

    检测凸多边形碰撞的一种简单的方法是SAT(Separating Axis Theorem),即分离轴定理. 原理:将多边形投影到一条向量上,看这两个多边形的投影是否重叠.如果不重叠,则认为这两个多边形 ...

  7. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  8. hdoj 1385Minimum Transport Cost

    卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...

  9. HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT

    一条边<u,v>表示u选那么v一定被选. #include <iostream> #include <cstring> #include <cstdio> ...

随机推荐

  1. Scala io操作

    1. 读文件 scala特有的是scala.io.Source,例如: import scala.io._ Source.fromFile(“cn.scala”,”utf8”).mkString 逐行 ...

  2. html回车事件

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. Mysql笔记之 -- 开启Mysql慢查询

    Mysql慢查询日志_1--如何开启慢查询日志 Windows下开启MySQL慢查询 MySQL在Windows系统中的配置文件一般是是my.ini找到[mysqld]下面加上 log-slow-qu ...

  4. [LeetCode]题解(python):137-Single Number II

    题目来源: https://leetcode.com/problems/single-number-ii/ 题意分析: 给定一个数组,数组里面每一个数字都出现了3次除了一个,找出那个数.要求时间复杂度 ...

  5. MySQL float 与decimal 各中的区别。

    想一个问题: 1/3+1/3+1/3=1.0 0.3+0.3+0.3 =0.9 想一想在小数的世界里要什么表示1/3呢!它的办法就是取一个与1/3十分接近的小数来代替:如上面例子中的0.3来代替1/3 ...

  6. CFBundleName系列参数的含义

    顺带讲一下其他这些选项表示什么意思: CFBundleName: CFBundleName指定了该束的简称.简称应该小于16个字符并且适合在菜单和“关于”中显示.通过把它加入到适当的.lproj子文件 ...

  7. Delphi2010的RTTI增强

    Delphi编译的文件体积增大了很多.很大一部分原因是因为Delphi2010默认提供了全信息的RTTI. 每一个数据类型都有全部运行时信息.例如可以在运行时获得结构体的成员以及成员类型等. 这个功能 ...

  8. Android程序报错 Connection refused 处理

    在用Android测试JSON数据解析的时候,报了这样一个异常: java.net.ConnectException: localhost/ - Connection refused 原来模拟器默认把 ...

  9. ios post空文件流导致400错误

  10. C++使用OLE高速读写EXCEL的源码

    我的代码参考的地方是这儿,再次感谢原作者 http://blog.csdn.net/gyssoft/archive/2007/04/29/1592104.aspx 我根据自己的需要做了整理,干净了一点 ...