HDU 4115 Eliminate the Conflict(2-SAT)(2011 Asia ChengDu Regional Contest)
Edward contributes his lifetime to invent a 'Conflict Resolution Terminal' and he has finally succeeded. This magic item has the ability to eliminate all the conflicts. It works like this:
If any two people have conflict, they should simply put their hands into the 'Conflict Resolution Terminal' (which is simply a plastic tube). Then they play 'Rock, Paper and Scissors' in it. After they have decided what they will play, the tube should be opened and no one will have the chance to change. Finally, the winner have the right to rule and the loser should obey it. Conflict Eliminated!
But the game is not that fair, because people may be following some patterns when they play, and if the pattern is founded by others, the others will win definitely.
Alice and Bob always have conflicts with each other so they use the 'Conflict Resolution Terminal' a lot. Sadly for Bob, Alice found his pattern and can predict how Bob plays precisely. She is very kind that doesn't want to take advantage of that. So she tells Bob about it and they come up with a new way of eliminate the conflict:
They will play the 'Rock, Paper and Scissors' for N round. Bob will set up some restricts on Alice.
But the restrict can only be in the form of "you must play the same (or different) on the ith and jth rounds". If Alice loses in any round or break any of the rules she loses, otherwise she wins.
Will Alice have a chance to win?
Each test case contains several lines.
The first line contains two integers N,M(1 <= N <= 10000, 1 <= M <= 10000), representing how many round they will play and how many restricts are there for Alice.
The next line contains N integers B1,B2, ...,BN, where Bi represents what item Bob will play in the ith round. 1 represents Rock, 2 represents Paper, 3 represents Scissors.
The following M lines each contains three integers A,B,K(1 <= A,B <= N,K = 0 or 1) represent a restrict for Alice. If K equals 0, Alice must play the same on Ath and Bthround. If K equals 1, she must play different items on Ath and Bthround.
#include <cstdio>
#include <cstring> const int MAXN = *;
const int MAXM = *;
const int WE = ; struct TwoSAT{
int n, ecnt, dfs_clock, scc_cnt;
int St[MAXN], c;
int head[MAXN], sccno[MAXN], pre[MAXN], lowlink[MAXN];
int next[MAXM], to[MAXM]; void init(int nn){
n = nn;
ecnt = ; dfs_clock = scc_cnt = ;
memset(head,,sizeof(head));
memset(sccno,,sizeof(sccno));
memset(pre,,sizeof(pre));
} void addEdge(int x, int y){
to[ecnt] = y; next[ecnt] = head[x]; head[x] = ecnt++;
//printf("%d->%d\n",x,y);
} void addEdge2(int x, int y){
addEdge(x,y); addEdge(y,x);
} void dfs(int u){
lowlink[u] = pre[u] = ++dfs_clock;
St[++c] = u;
for(int p = head[u]; p; p = next[p]){
int &v = to[p];
if(!pre[v]){
dfs(v);
if(lowlink[u] > lowlink[v]) lowlink[u] = lowlink[v];
}else if(!sccno[v]){
if(lowlink[u] > pre[v]) lowlink[u] = pre[v];
}
}
if(lowlink[u] == pre[u]){
++scc_cnt;
while(true){
int x = St[c--];
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} bool solve(){
for(int i = ; i < n; ++i)
if(!pre[i]) dfs(i);
for(int i = ; i < n; i += )
if(sccno[i] == sccno[i^]) return false;
return true;
} void test(){
for(int i = ; i < n; ++i){
printf("%d\n",i+);
for(int p = head[i]; p; p = next[p]) printf("%d ", to[p]+);
}
} } G; int B[MAXN]; int main(){
int T, n, m, a, b, k;
scanf("%d", &T);
for(int t = ; t <= T; ++t){
scanf("%d%d",&n,&m);
G.init(n*WE);
for(int i = ; i < n; ++i){
scanf("%d", &b);
if(b == ) {
//G.addEdge(i*WE+2*2, i*WE+2*2+1);
G.addEdge(i*WE+*+, i*WE+*);
G.addEdge(i*WE+*+, i*WE+*);
}
if(b == ) {
//G.addEdge(i*WE+0*2, i*WE+0*2+1);
G.addEdge(i*WE+*+, i*WE+*);
G.addEdge(i*WE+*+, i*WE+*);
}
if(b == ) {
//G.addEdge(i*WE+1*2, i*WE+1*2+1);
G.addEdge(i*WE+*+, i*WE+*);
G.addEdge(i*WE+*+, i*WE+*);
}
}
while(m--){
scanf("%d%d%d", &a, &b, &k);
--a, --b;
if(k == ){
for(int i = ; i < ; ++i) {
G.addEdge2(a*WE+i*, b*WE+i*);
G.addEdge2(a*WE+i*+, b*WE+i*+);
}
}else{
for(int i = ; i < ; ++i){
G.addEdge(a*WE+i*, b*WE+i*+);
G.addEdge(b*WE+i*, a*WE+i*+);
}
}
}
for(int i = ; i < n; ++i)
for(int j = ; j < ; ++j)
for(int k = ; k < ; ++k) if(j != k){
G.addEdge(i*WE+j*, i*WE+k*+);
}
if(G.solve()) printf("Case #%d: yes\n", t);
else printf("Case #%d: no\n", t);
}
return ;
}
93MS
思路2:2-SAT问题,首先别人出布,你肯定不能出石头,那么你只能在剩下的两个之中选一个,那么 剪刀 xor 布 = true且石头=false;若A、B不能出一样的,比如那么对于剪刀来讲,A出了剪刀,B就不能出剪刀,B出了剪刀,A就不能出剪刀;A、B要一样也类似。
#include <cstdio>
#include <cstring> const int MAXN = *;
const int MAXM = *;
const int WE = ; struct TwoSAT{
int n, ecnt, dfs_clock, scc_cnt;
int St[MAXN], c;
int head[MAXN], sccno[MAXN], pre[MAXN], lowlink[MAXN];
int next[MAXM], to[MAXM]; void init(int nn){
n = nn;
ecnt = ; dfs_clock = scc_cnt = ;
memset(head,,sizeof(head));
memset(sccno,,sizeof(sccno));
memset(pre,,sizeof(pre));
} void addEdge(int x, int y){
to[ecnt] = y; next[ecnt] = head[x]; head[x] = ecnt++;
//printf("%d->%d\n",x,y);
} void addEdge2(int x, int y){
addEdge(x,y); addEdge(y,x);
} void dfs(int u){
lowlink[u] = pre[u] = ++dfs_clock;
St[++c] = u;
for(int p = head[u]; p; p = next[p]){
int &v = to[p];
if(!pre[v]){
dfs(v);
if(lowlink[u] > lowlink[v]) lowlink[u] = lowlink[v];
}else if(!sccno[v]){
if(lowlink[u] > pre[v]) lowlink[u] = pre[v];
}
}
if(lowlink[u] == pre[u]){
++scc_cnt;
while(true){
int x = St[c--];
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} bool solve(){
int i;
for(i = ; i < n; ++i)
if(!pre[i]) dfs(i);
for(i = ; i < n; i += )
if(sccno[i] == sccno[i^]) return false;
return true;
} void test(){
for(int i = ; i < n; ++i){
printf("%d\n",i+);
for(int p = head[i]; p; p = next[p]) printf("%d ", to[p]+);
}
} } G; int B[MAXN]; int main(){
int T, n, m, a, b, k;
scanf("%d", &T);
for(int t = ; t <= T; ++t){
scanf("%d%d",&n,&m);
G.init(n*WE);
for(int i = ; i < n; ++i){
scanf("%d", &b);
if(b == ) {
G.addEdge(i*WE+*, i*WE+*+);
G.addEdge2(i*WE+*+, i*WE+*);
G.addEdge2(i*WE+*, i*WE+*+);
}
if(b == ) {
G.addEdge(i*WE+*, i*WE+*+);
G.addEdge2(i*WE+*+, i*WE+*);
G.addEdge2(i*WE+*, i*WE+*+);
}
if(b == ) {
G.addEdge(i*WE+*, i*WE+*+);
G.addEdge2(i*WE+*+, i*WE+*);
G.addEdge2(i*WE+*, i*WE+*+);
}
}
while(m--){
scanf("%d%d%d", &a, &b, &k);
--a, --b;
if(k == ){
for(int i = ; i < ; ++i) {
G.addEdge2(a*WE+i*, b*WE+i*);
G.addEdge2(a*WE+i*+, b*WE+i*+);
}
}else{
for(int i = ; i < ; ++i){
G.addEdge(a*WE+i*, b*WE+i*+);
G.addEdge(b*WE+i*, a*WE+i*+);
}
}
}
if(G.solve()) printf("Case #%d: yes\n", t);
else printf("Case #%d: no\n", t);
}
return ;
}
78MS
思路3:实际上还有时空复杂度都比较好的方法,就是每个ROUND都不要必败的点,网上有很多都是这个思路的,不过写起来比较麻烦我就不写啦~~
HDU 4115 Eliminate the Conflict(2-SAT)(2011 Asia ChengDu Regional Contest)的更多相关文章
- HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)
Description P. T. Tigris is a student currently studying graph theory. One day, when he was studying ...
- HDU 4468 Spy(KMP+贪心)(2012 Asia Chengdu Regional Contest)
Description “Be subtle! Be subtle! And use your spies for every kind of business. ”― Sun Tzu“A spy w ...
- HDU - 4054 Hexadecimal View (2011 Asia Dalian Regional Contest)
题意:按要求输出.第一列是表示第几行.每行仅仅能有16个字节的字母,第二列是16进制的ASCII码.第三列大写和小写转换 思路:纯模拟,注意字母的十六进制是2位 #include <iostre ...
- 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)
HDU 4115 Eliminate the Conflict pid=4115">题目链接 题意:Alice和Bob这对狗男女在玩剪刀石头布.已知Bob每轮要出什么,然后Bob给Al ...
- HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...
- UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- HDU 4115 Eliminate the Conflict
2-SAT,拆成六个点. #include<cstdio> #include<cstring> #include<cmath> #include<stack& ...
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)
Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...
随机推荐
- Office365完整离线安装包下载及自定义安装教程
Office 365是微软打造的一款适用于教育机构使用的office办公软件,这里为大家提供了一个Office 365离线安装包下载工具,让office 365离线包下载到本地再安装,而不是联网下载安 ...
- C++练习 | 创建并正序输出不带头结点的链表
#include <iostream> #include <cstdio> #include <stdlib.h> using namespace std; str ...
- Mybartis逆向工程
Mybartis逆向工程 0.创建工程项目,切记莫用中文,亲测在运行时报错 1.Pom文件,使用mybatis-generator插件 <?xml version="1.0" ...
- MySQL学习之用户管理
用户权限管理 用户权限管理:在不同的项目中给不同的角色(开发者)不同的操作权限,为了保证数据库数据的安全. 简单点说:有的用户可以访问并修改这个数据,而有些用户只能去查看数据,而不能修改数据.就如同博 ...
- easyui datagrid 异步加载数据时滚动条有时会自动滚到最底部的问题
在使用easyui 的datagrid异步加载数据时发现滚动条有时会自动滚到最底部.经测试发现,如果加载数据前没有选中行则不会出现这个问题.这样我们可以在重新异步加载数据前取消选中行就可以避免这个问题 ...
- 解决 LLVM 错误提示 may only occur zero or one times!
使用 LLVM 混淆器添加参数进行编译提示如下错误:clang (LLVM option parsing): for the -bcf option: may only occur zero or o ...
- window安装ubuntu系统
- Laravel 生成二维码
(本实例laravel 版本 >=5.6, PHP版本 >=7.0) 1.首先,添加 QrCode 包添加到你的 composer.json 文件的 require 里: "re ...
- Spark 加载数据库mysql表中数据进行分析
1.工程maven依赖包 <properties> <spark_version>2.3.1</spark_version> <!-- elasticsear ...
- rails中发送ajax请求
最近在写一个blog系统练练手,遇到一个一个问题,用户添加评论的时候想发送ajax请求,但是rails里的ajax和Python中的不太一样,Python中的ajax是用js,jquery实现的和ra ...