HDU 4115 Eliminate the Conflict (2-SAT)
来源:互联网 发布:淘宝美工是干嘛的 编辑:程序博客网 时间:2024/05/12 04:46
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4115
题意:Alice和Bob在玩剪刀石头布,Alice知道Bob每轮出什么。现在给Alice若干组限制(A B K),若K为1,则代表Alice第A轮和第B轮不能出的一样,若A为0,则代表Alice第A轮和第B轮出的必须一样。问在这些限制下,Alice是否能赢。(赢的定义是在任意一轮都不能输(即任意一轮要么平要么赢))。
思路:对于每一轮,Alice只有两种出法,最终要在多组限制下找一个满足的方案,所以这是一个很明显的2-SAT问题。在某一轮中Alice要想不输,则有两种手法(a1, a2)可以使用;当 k = 1时 ,在 a1 == b1的情况下会发生矛盾,所以连边 a1 -> b2,b1 -> a2;当 k = 0,那么则在a1 != b1 的情况下会发生矛盾,所以连边 a1 -> b2,b1 -> a2。讨论一下即可。
#include<cstdio>#include<cstring>#include<string>#include<cctype>#include<iostream>#include<set>#include<map>#include<cmath>#include<sstream>#include<vector>#include<stack>#include<queue>#include<algorithm>#define fin(a) freopen("a.txt","r",stdin)#define fout(a) freopen("a.txt","w",stdout)typedef long long LL;using namespace std;typedef pair<int, int> P;const int INF = 1e8 + 10;const int maxn = 10000 + 10;const int rock = 1;const int paper = 2;const int scissor = 3;int from[maxn], to[maxn], dif[maxn];int bob[maxn];struct Node { int l, r;}X[maxn];struct TwoSAT { int n; vector<int> G[maxn*2]; bool mark[maxn*2]; int S[maxn*2], c; bool dfs(int x) { if(mark[x^1]) return false; if(mark[x]) return true; mark[x] = true; S[c++] = x; for(int i = 0; i < G[x].size(); i++) { if(!dfs(G[x][i])) return false; } return true; } void init(int n) { this->n = n; for(int i = 0; i < n*2; i++) G[i].clear(); memset(mark, 0, sizeof mark); } void add_clause(int x, int xval, int y, int yval) { x = x * 2 + xval; y = y * 2 + yval; G[x^1].push_back(y); G[y^1].push_back(x); } bool solve() { for(int i = 0; i < n*2; i += 2) if(!mark[i] && !mark[i+1]) { c = 0; if(!dfs(i)) { while(c > 0) mark[S[--c]] = false; if(!dfs(i+1)) return false; } } return true; }}two;void AddEdge(int n, int m) { for(int i = 0; i < m; i++) { int u = from[i], v = to[i]; if(dif[i] == 1) { if(X[u].l == X[v].l) two.add_clause(u, 1, v, 1); if(X[u].l == X[v].r) two.add_clause(u, 1, v, 0); if(X[u].r == X[v].l) two.add_clause(u, 0, v, 1); if(X[u].r == X[v].r) two.add_clause(u, 0, v, 0); } else { if(X[u].l != X[v].l) two.add_clause(u, 1, v, 1); if(X[u].l != X[v].r) two.add_clause(u, 1, v, 0); if(X[u].r != X[v].l) two.add_clause(u, 0, v, 1); if(X[u].r != X[v].r) two.add_clause(u, 0, v, 0); } }}int main() { int T, kase = 0; scanf("%d", &T); while(T--) { int n, m; scanf("%d%d", &n, &m); two.init(n); for(int i = 0; i < n; i++) { scanf("%d", &bob[i]); if(bob[i] == rock) { X[i].l = rock; X[i].r = paper; } else if(bob[i] == paper) { X[i].l = paper; X[i].r = scissor; } else { X[i].l = scissor; X[i].r = rock; } } for(int i = 0; i < m; i++) { scanf("%d%d%d", &from[i], &to[i], &dif[i]); --from[i]; --to[i]; } AddEdge(n, m); printf("Case #%d: %s\n", ++kase, two.solve() ? "yes" : "no"); } return 0;}
阅读全文
0 0
- HDU 4115 Eliminate the Conflict 2-sat
- HDU 4115 Eliminate the Conflict 2-sat
- HDU 4115 Eliminate the Conflict (2-SAT)
- HDU 4115 Eliminate the Conflict (2-SAT)
- 【HDU】4115 Eliminate the Conflict 2-sat
- HDU 4115 Eliminate the Conflict(2-sat)
- HDU 4115 Eliminate the Conflict【2-sat】
- HDU - 4115 Eliminate the Conflict(2-SAT)
- HDU 4115 Eliminate the Conflict 2-SAT
- HDU 4115Eliminate the Conflict 2-sat
- hdu 4115 Eliminate the Conflict (2-sat)
- hdu 4115 Eliminate the Conflict 2-sat
- HDU 4115 Eliminate the Conflict (2-SAT)
- HDU 4115 Eliminate the Conflict (2-SAT判断)
- HDU 4115 Eliminate the Conflict (2-sat,4级)
- hdu 4115 Eliminate the Conflict ( 2-sat )
- Hdu 4115 Eliminate the Conflict (2-sat 可行性判定)
- HDU 4115 Eliminate the Conflict(2-SAT)
- Python爬虫框架Scrapy学习二记——Scrapy开发环境配置
- jsonp跨域请求API接口[自己写接口]
- Linux 网卡驱动学习(四)(缓存描述符 Buffer Description)
- 二.Groovy的介绍
- maven配置
- HDU 4115 Eliminate the Conflict (2-SAT)
- IDEA远程调试mapreduce程序
- 198. House Robber
- 错误积累
- ARouter源码解析06-openDebug
- 数据库安装之各种问题二(创建ODBC源,VB丢失Microsoft Data Environment Instance6.0解决办法)
- 【MVC】过滤器应用-代码
- Android 通过 localstorage 向 HTML5 传参
- 词汇暴增计划