HDU 4115 Eliminate the Conflict

pid=4115">题目链接

题意:Alice和Bob这对狗男女在玩剪刀石头布。已知Bob每轮要出什么,然后Bob给Alice一些限制,1表示i轮和j轮Alice必须出不一样的,0表示必须出一样的。假设Alice有一局输了就算输了,否则就是赢,问Alice能否赢

思路:2-sat问题,已经Bob出什么,Alice要么就出赢的要么就出平的,然后加上m个约束就是2-sat问题了

代码:

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std; const int N = 10005; int t, n, m, x[N], sn, S[N * 2];
vector<int> g[N * 2];
bool mark[N * 2]; void init() {
for (int i = 0; i < 2 * n; i++) g[i].clear();
memset(mark, false, sizeof(mark));
} void add_edge(int u, int x, int v, int y) {
u = u * 2 + x;
v = v * 2 + y;
g[u^1].push_back(v);
g[v^1].push_back(u);
} bool dfs(int u) {
if (mark[u^1]) return false;
if (mark[u]) return true;
mark[u] = true;
S[sn++] = u;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (!dfs(v)) return false;
}
return true;
} bool solve() {
for (int i = 0; i < 2 * n; i += 2) {
if (!mark[i] && !mark[i + 1]) {
sn = 0;
if (!dfs(i)) {
for (int j = 0; j < sn; j++) mark[S[j]] = false;
sn = 0;
if (!dfs(i + 1)) return false;
}
}
}
return true;
} int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
init();
scanf("%d%d", &n, &m);
int tmp;
for (int i = 0; i < n; i++)
scanf("%d", &x[i]);
int u, v, w;
while (m--) {
scanf("%d%d%d", &u, &v, &w);
u--; v--;
int tmp = 6 - x[u] - x[v];
int a = 6 - x[u] - tmp;
int b = 6 - x[v] - tmp;
int u1, u2, v1, v2;
u2 = a > tmp; u1 = !u2;
v2 = b > tmp; v1 = !v2;
if (w == 1) {
if (x[u] == x[v]) {
add_edge(u, 1, v, 1);
add_edge(u, 0, v, 0);
} else add_edge(u, !u1, v, !v1);
} else {
if (x[u] == x[v]) {
add_edge(u, 0, v, 1);
add_edge(u, 1, u, 0);
} else {
add_edge(u, u1, u, u1);
add_edge(v, v1, v, v1);
}
}
}
printf("Case #%d: %s\n", ++cas, solve() ? "yes" : "no");
}
return 0;
}

HDU 4115 Eliminate the Conflict(2-sat)的更多相关文章

  1. hdu 4115 Eliminate the Conflict ( 2-sat )

    Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  2. HDU 4115 Eliminate the Conflict(2-SAT)(2011 Asia ChengDu Regional Contest)

    Problem Description Conflicts are everywhere in the world, from the young to the elderly, from famil ...

  3. HDU 4115 Eliminate the Conflict

    2-SAT,拆成六个点. #include<cstdio> #include<cstring> #include<cmath> #include<stack& ...

  4. 图论--2-SAT--HDU/HDOJ 4115 Eliminate the Conflict

    Problem Description Conflicts are everywhere in the world, from the young to the elderly, from famil ...

  5. hdu 4115 石头剪子布(2-sat问题)

    /* 意甲冠军:石头剪子布,目前已知n周围bob会有什么,对alice限制.供u,v,w:设w=0说明a,b回合必须出的一样 否则,必须不一样.alice假设输一回合就输了,否则就赢了 解: 2-sa ...

  6. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  7. HDU 5980 Find Small A(寻找小A)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  8. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. vscode setting.json

    setting.json { "sync.gist": "#github的码##", "sync.lastUpload": "20 ...

  2. python基础一 day4 字典

    增加:   结果 删:  结果: 结果:   返回值是一个元组形式的键值   改: 结果: 结果:                                      代码: info=inpu ...

  3. vue 组件相互传值

    Part.1  传值几种方式 在写项目时,遇到组件传值问题,特此记录一波~~~ (1)  父传子 (2)  子传父 (2)  兄弟组件传值 Part.2  父传子 顾名思义,就是父组件传值给子组件 子 ...

  4. 这就是那个feature map256 256向量

    http://blog.csdn.net/XZZPPP/article/details/51582810 在这个特征图上使用3*3的卷积核(滑动窗口)与特征图进行卷积,那么这个3*3的区域卷积后可以获 ...

  5. JavaScript/JQuery radioButton(单选按钮)练习20190409

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. windows环境下Robot Framework的安装步骤

    Robot Framework是由python编写的开源的用来做功能性测试的自动化测试框架.本文介绍Robot Framework在windows环境下的安装步骤. 安装python从python官网 ...

  7. django实现github第三方本地登录

    1.安装 pip install social-auth-app-django 2.生成Client ID和Client Secret 3.修改setting.py INSTALLED_APPS = ...

  8. python基础教程之pymongo库

    1. 引入 在这里我们来看一下Python3下MongoDB的存储操作,在本节开始之前请确保你已经安装好了MongoDB并启动了其服务,另外安装好了Python的PyMongo库. 1.  安装 pi ...

  9. Django框架基础知识13-auth系统

    我们昨天登录admin时创建的用户信息是存放在哪里了呢? auth系统的数据表: 从表的名称我们就能看出, auth_user,auth_group,auth_permission分别存放了用户,用户 ...

  10. 2019年,Python工程师必考的6个面试题,Python面试题No5

    第1题:Python里面如何实现tuple和list的转换? 函数tuple(seq)可以把所有可迭代的(iterable)序列转换成一个tuple, 元素不变,排序也不变 list转为tuple: ...