题目链接: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. Java中单例七种写法(懒汉、恶汉、静态内部类、双重检验锁、枚举)

    /*** * 懒汉模式 1 * 可以延迟加载,但线程不安全. * @author admin * */ public class TestSinleton1 { private static Test ...

  2. C++小知识之Map用法

    Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作! 1. map最基本的构造函数:    map<st ...

  3. 我的Fedora22美化日记

    首先我说一下,我命令是乱打的[不要打我],用之前先google 配置RPMFusion仓库 $ sudo dnf install --nogpgcheck http://download1.rpmfu ...

  4. ASPから広がり

    ASP是动态服务器页面(Active Server Page)外语缩写.[1]是微软公司开发的代替CGI脚本程序的一种应用,它可以与数据库和其它程序进行交互,是一种简单.方便的编程工具.ASP的网页文 ...

  5. javascript块级作用域

    在c/java中,拥有块级作用域的概念,大括号内就是一个块级作用域,在块级作用域内声明的变量,块以外不可见. C语音的块级作用域示例如下: ,two = ; if(one < two){ ; t ...

  6. CoreText 简单 使用

    - (void)drawRect:(CGRect)rect { NSString *longText = @"CoreText"; /* ... */ NSRange rang = ...

  7. MockObject

    http://www.mockobjects.com/ http://jmock.org/download.html https://jakarta.apache.org/cactus/mock_vs ...

  8. Module中引用Module中的Activity时报错了,错误是找不到R文件中的id引用

    1.好像库modul和主modul不能有相同名字和layout文件 2.资源文件名冲突导致的

  9. 使用tcpdump抓Android网络包

    1 抓包原理 tcpdump(需Root用户运行)拦截和显示发送或收到过网络连接到该机器的TCP/IP和其他数据包.简单说就监控手机进出网络数据. 2 方法优劣 2.1优点 1.手机数据包无遗漏 2. ...

  10. BZOJ 4503 两个串(FFT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4503 [题目大意] 给出S串和T串,计算T在S中出现次数,T中有通配符'?'. [题解 ...