HDU 4115 Eliminate the Conflict(2-sat)
HDU 4115 Eliminate the Conflict
题意: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)的更多相关文章
- hdu 4115 Eliminate the Conflict ( 2-sat )
Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 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 ...
- HDU 4115 Eliminate the Conflict
2-SAT,拆成六个点. #include<cstdio> #include<cstring> #include<cmath> #include<stack& ...
- 图论--2-SAT--HDU/HDOJ 4115 Eliminate the Conflict
Problem Description Conflicts are everywhere in the world, from the young to the elderly, from famil ...
- hdu 4115 石头剪子布(2-sat问题)
/* 意甲冠军:石头剪子布,目前已知n周围bob会有什么,对alice限制.供u,v,w:设w=0说明a,b回合必须出的一样 否则,必须不一样.alice假设输一回合就输了,否则就赢了 解: 2-sa ...
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
- HDU 5980 Find Small A(寻找小A)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- Linux之基础命令——文件搜索
grep(匹配符合条件的字符串) 无参:显示匹配行 -c:显示匹配行数 -e 字符串:匹配特殊字符串,如-开头 -i:忽略大小写 -v:输出不匹配行 -w:匹配指定字符串 可以和别的命令通过" ...
- jeecms
===标签=== <!-- 显示一级栏目对应的二级栏目 --> <!-- [@cms_channel_list parentId=c.id] [#if tag_list?size&g ...
- vscode 打开新文件不替换旧文件
设置 "workbench.editor.enablePreview": false
- python基础一 day5 知识点
Unicode转化为gbk和utf-8 表现形式:str转化为bytes
- c++ vector容器遍历方式
#include <vector> #include <iostream> class Test { public: int a; int b; int c; Test() { ...
- python之字符串str操作方法
str.upper() (全部大写) str.lower() (全部小写) str.startswith() (以什么开头) str.endswith() (以什么结尾) str.count() (统 ...
- 小程序02 wxml和wxss
微信小程序的排版就跟wxml和wxss有关,它们两者相当于HTML和CSS,其中wxml指定了界面的框架结构,而wxss指定了界面的框架及元素的显示样式. 一.wxml 界面结构wxmL比较容易理解, ...
- gcc编译问题
gcc avl.o hash.o list.o rb.o example.o -o 123.exe 多个.o输出 exe -c和-o都是gcc编译器的可选参数.-c表示只编译(compile)源文件但 ...
- luogu P3353 在你窗外闪耀的星星
问题:天空可以理解为一条数轴,在这条数轴上分布着许多颗星星,对于每颗星星都有它的位置Xi和自身的亮度Bi.而窗户所能看到的范围是一个给出的参数W,我们看到的星星也包括窗户边缘的星星.现在,要你求出调整 ...
- CreateProcess相关
CreateProcess不创建窗口执行: https://blog.csdn.net/rongwenbin/article/details/24422041 CreateProcess返回值: 执行 ...