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都不要必败的点,网上有很多都是这个思路的,不过写起来比较麻烦我就不写啦~~

最新文章

  1. React Native IOS源码初探
  2. HTML5音频视频-视频播放
  3. SQLiteDatabase浅谈
  4. jboss部署出现jboss.naming.context.java.rmi找不到错误
  5. NFC规范学习之一 ---整体结构
  6. 如何设置Cookie 的值为中文的内容
  7. 用Go校验下载文件之SHA256
  8. 【dedecms】DEDE列表页调用文章内容第一张图片(非缩略图)方法
  9. 20162328蔡文琛week05
  10. 搭建vue环境
  11. 题解-UOJ284 快乐游戏鸡
  12. rabbitmq heartbeat missing with heartbeat = N seconds原因总结
  13. Tanks!Tutorial 学习
  14. bzoj千题计划194:bzoj2115: [Wc2011] Xor
  15. Oracle12c中性能优化&amp;amp;功能增强新特性之重大突破——内存列存储新特性
  16. 分别用C/C++ 和 C#实现简单的观察者模式
  17. python开发_tkinter_图形随鼠标移动
  18. IDEA新建Web项目
  19. 2019年猪年海报PSD模板-第七部分
  20. iPad开发简单介绍

热门文章

  1. [ZJOI2009]假期的宿舍(二分图匹配)
  2. Linux 只显示目录或者文件方法
  3. cut 的用法
  4. 【读书笔记 - Effective Java】01. 考虑用静态工厂方法代替构造器
  5. 纯JS实现前端动态分页码
  6. 使用Scala开发Apache Kafka的TOP 20大好用实践
  7. 大数据&amp;人工智能&amp;云计算
  8. java中子类会继承父类的构造方法吗?
  9. Python学习 :迭代器&amp;生成器
  10. Python基础、条件语句和基本数据类型