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 ...
随机推荐
- Java并发包:AtomicBoolean和AtomicReference
AtomicBoolean AtomicBoolean是一个读和写都是原子性的boolean类型的变量.这里包含高级的原子操作,例如compareAndSet().AtomicBoolean位于J ...
- 简单几行代码使用百度地图API接口分页获取信息
首发于: 万能助手扩展开发:使用百度地图API接口分页获取信息_电脑计算机编程入门教程自学 http://jianma123.com/viewthread.aardio?threadid=426 使用 ...
- mysql对查出来的值,在sql里面拼接我们想要拼接的内容
MySQL中concat函数使用方法:CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. 注意:如果所有参数均为非二进制字符串 ...
- hashcode和equals区别
hashcode:对象的初始地址的整数表示 Java中的对象是JVM在管理,JVM会在她认为合适的时候对对象进行移动,比如,在某些需要整理内存碎片的GC算法下发生的GC.此时,对象的地址会变动,但ha ...
- [SHELL]软件管理
- 大数据学习--day06(Eclipse、数组)
Eclipse.数组 Eclipse 的基本设置 调节控制条字体大小. Window -> Preferences -> General -> Appearance -> ...
- S3C2440上LCD驱动(FrameBuffer)实例开发讲解(一)
一.开发环境 主 机:VMWare--Fedora 9 开发板:Mini2440--64MB Nand, Kernel:2.6.30.4 编译器:arm-linux-gcc-4.3.2 二.背景知识 ...
- S3C2440启动程序运行过程
s3c2440有两种启动方式,一种Nor flash 启动,一种Nand flash 启动. 由于NAND FLASH是接在NAND FLASH控制器上而不是系统总线上,所以没有在S3C2440A的8 ...
- ZYNQ的Linux Linaro系统镜像制作SD卡启动
ZYNQ的Linux Linaro系统镜像制作SD卡启动 0. 概述 ZYNQ生成uboot的时候和正常的ARM设备不太一样,ZYNQ属于二次辅助启动uboot然后由uboot启动内核,大概意思就是 ...
- python 爬虫 5i5j房屋信息 获取并存储到数据库
from lxml import etree from selenium import webdriver import pymysql def Geturl(fullurl):#获取每个招聘网页的链 ...