Problem Description
Conflicts are everywhere in the world, from the young to the elderly, from families to countries. Conflicts cause quarrels, fights or even wars. How wonderful the world will be if all conflicts can be eliminated.
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?
 
Input
The first line contains an integer T(1 <= T <= 50), indicating the number of test cases.
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.
 
Output
For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is "yes" or "no" represents whether Alice has a chance to win.
 
题目大意:现在你和别人玩石头剪刀布,你已经预知了别人第几次出什么,但是你有所限制:若A,B,K,K=0则ROUND A、B要出一样的,若A,B,K,K=1则ROUND A、B不能出一样的,完成n ROUND如果没有输过就算赢了。现在问能不能赢。
思路:2-SAT问题,首先别人出布,你肯定不能出石头,那么你只能在剩下的两个之中选一个,那么 剪刀 OR 布 = 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(){
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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. HDU - 4054 Hexadecimal View (2011 Asia Dalian Regional Contest)

    题意:按要求输出.第一列是表示第几行.每行仅仅能有16个字节的字母,第二列是16进制的ASCII码.第三列大写和小写转换 思路:纯模拟,注意字母的十六进制是2位 #include <iostre ...

  4. hdu 4115 Eliminate the Conflict ( 2-sat )

    Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. HDU 4115 Eliminate the Conflict(2-sat)

    HDU 4115 Eliminate the Conflict pid=4115">题目链接 题意:Alice和Bob这对狗男女在玩剪刀石头布.已知Bob每轮要出什么,然后Bob给Al ...

  6. 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 ...

  7. UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  8. HDU 4115 Eliminate the Conflict

    2-SAT,拆成六个点. #include<cstdio> #include<cstring> #include<cmath> #include<stack& ...

  9. 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 ...

随机推荐

  1. 【2013 ICPC亚洲区域赛成都站 F】Fibonacci Tree(最小生成树+思维)

    Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do s ...

  2. STL专题·vector容器

    1.构造函数 vector():创建一个空vector vector(int nSize):创建一个vector,元素个数为nSize (vector<int> a(10);) vecto ...

  3. chromium之MessagePump.h

    上代码,注释已经写得很详细了. 粗看一下,这是个纯虚类,用于跨平台的通用接口. MessagePump,Pump的意思是泵,,MessagePump也就是消息泵,输送消息 namespace base ...

  4. 利用SoapUI 测试web service的一些问题总结

    总结两个利用SoapUI 测试web service的一些问题: 1.请求一个soap service 请求的时候:按照下面的配置输入请求地址后, 2.根据实际service接口的需要,传入相应的参数 ...

  5. Android中的AutoCompleteTextView(随笔提示文本)组件的简单使用

    Android中的随笔提示文本组件AutoCompleteTextView的使用,此组件用于输入文本,然后就会在所配置的适配器中的数据进行查找显示在组件下面. 这里值得注意的是AutoComplete ...

  6. hive常见的几种优化手段

    Hive调优的几个入手点: Hive是基于Hadoop框架的,Hadoop框架又是运行在JVM中的,而JVM最终是要运行在操作系统之上的,所以,Hive的调优可以通过如下几个方面入手: 操作系统调优 ...

  7. MapReduce之Reduce Join

    一 介绍 Reduce Join其主要思想如下: 在map阶段,map函数同时读取两个文件File1和File2,为了区分两种来源的key/value数据对,对每条数据打一个标签(tag), 比如:t ...

  8. 基于TCP/IP的局域网聊天室---C语言

    具备注册账号,群聊,查看在线人员信息,私发文件和接收文件功能,因为每个客户端只有一个属于自己的socket,所以无论客户端是发聊天消息还是文件都是通过这一个socket发送, 这也意味着服务器收发任何 ...

  9. ruby做接口测试

    一. 工具选择 IDE:rubymine:http接口请求:Unirest,ruby单元测试框架:rspec 二.工程创建 新建工程,在工程目录下,执行:rspec --init:初始化rspec工程 ...

  10. [Real World Haskell翻译]第23章 GUI编程使用gtk2hs

    第23章 GUI编程使用gtk2hs 在本书中,我们一直在开发简单的基于文本的工具.虽然这些往往是理想的接口,但有时图形用户界面(GUI)是必需的.有几个Haskell的GUI工具包是可用的.在本章中 ...