题目链接: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. SQL语言整理归纳

  2. 交换机access和trunk的一些小结(转)

     以太网端口有 3种链路类型:access.trunk.hybird Access类型端口只能属于1个VLAN 般用于连接计算机 端口: Trunk类型端口可以允许多个VLAN通过,可以接收和发送多个 ...

  3. C++_基础_继承、多态

    内容: (1)子类中的拷贝构造和拷贝赋值 (2)多继承和虚继承 (3)多态的初识 (4)虚析构的特性和使用 (5)多态的底层实现 (6)纯虚函数.抽象类的概念 1.子类中的拷贝构造和拷贝赋值 子类中的 ...

  4. 开源libusb驱动的libwdi驱动安装API库和zadig.exe安装UI应用程序的编译和调试

    一.目的 二.编译环境 系统:Win7 ~ Win10 编译工具:Visual Studio 2008 或 Visual Studio 2010 或Visual Studio 2015 libwdi编 ...

  5. ASP中 Request.Form中文乱码的解决方法

    分享下解决方法直接用request.Form()获取的是所有数据所以会有乱码(具体原因不祥) 用 VBScript code Foreach obj in Request.Form Response. ...

  6. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  7. 为什么要for循环以及for循环的流程

    /* Name:为什么需要循环以及for循环流程 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月10日 03:16:55 Description:求1 ...

  8. querySelectorAll 和 jQuery选择器

    参考 http://xahlee.info/js/jquery_diff_querySelectorAll.html http://stackoverflow.com/questions/115035 ...

  9. 浅谈 android-query

    介绍:android-query他是在GitHub上的一个开源轻量级的封装库,它集成了网络 .图片加载等模块,可以应用在android中的一些异步应用以及UI的操纵上,通过使用这个框架,使androi ...

  10. HDU 5919 Sequence II(可持久化线段树)

    [题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5919 [题目大意] 给出一个数列,每次查询数列中,区间非重元素的下标的中位数.查询操作强制在线. [ ...