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 ...
随机推荐
- mui的openWindowWithTitle()参数及说明
mui.openWindowWithTitle({ url: 'xxx.html', //String类型,要打开的界面的地址 id: 'id', //String类型,要打开的界面的id style ...
- 【补】英语对IT工作者的重要性
浅谈程序员的英语学习 作为在中国工作的程序员,不懂得英语似乎也不妨碍找到好工作,升职加薪.但程序员这个工种则稍有不同,因为程序,尤其是高级语言,基本上都是由英语和数字表达式构成的.英语对于程序员十 ...
- javascript 时间倒计时效果
<div id="divdown1"></div> <script language="javascript" type=&quo ...
- jquery输入框动态查询l<li></li>列表
1.html代码如下: 2.js代码如下: $('#search').bind('input propertychange', function () { searchKpoint(); }); fu ...
- angularjs中控制器之间的通信----$on、$emit和$broadcast解析
$on.$emit和$broadcast使得event.data在controller之间的传递变的简单. $emit只能向parent controller传递event与data $broadca ...
- spring-mvc高级技术
Spring MVC高级技术包括但不限于web.xml配置.异常处理.跨重定向请求传递数据 1.web.xml文件的配置 <!DOCTYPE web-app PUBLIC "-//Su ...
- MySQL5.7主从同步--点位方式及GTID方式
MySQL5.6加入了GTID的新特性,其全称是Global Transaction Identifier,可简化MySQL的主从切换以及Failover.GTID用于在binlog中唯一标识一个事务 ...
- Laravel 生成二维码
(本实例laravel 版本 >=5.6, PHP版本 >=7.0) 1.首先,添加 QrCode 包添加到你的 composer.json 文件的 require 里: "re ...
- 20190118-自定义实现replace方法
1.自定义实现replace方法 Python replace() 方法把字符串中的 old(旧字符串) 替换成 neange(新字符串),如果指定第三个参数max,则替换不超过 max 次.考虑ol ...
- Java 数字用二进制表示,以及原码,反码,补码、负数的二进制表示
首先我们要对原码.反码和补码有个了解: 1.所谓原码就是二进制定点表示法,即最高位为符号位,"0"表示正,"1"表示负,其余位表示数值的大小. 2.反码表示法规 ...