23 31 1 11 2 11 3 12 3 15 51 2 3 2 11 2 11 3 11 4 11 5 12 3 0 Sample OutputCase #1: noCase #2: yesHint'Rock, Paper and Scissors' is a game which played by two person. They should play Rock, Paper or Scissors by their hands at the same time. Rock defeats scissors, scissors defeats paper and paper defeats rock. If two people play the same item, the game is tied.. Source2011 Asia ChengDu Regional Contest
题意:
Bob和Alice玩剪刀石头布,一个玩n轮,Alice已经知道了Bob每次要出什么,1代表剪刀,2代表石头,3代表布,然后Bob对Alice作出了一些限制:
给m行,每行是a b k,如果k是0,表示Alice第a次和b次出的拳必须相同,如果k是1,表示Alice第a次和b次出的拳必须不相同。
一但Alice破坏了这个限制规则,或者输了一局,那么Alice就彻底输了。
问Alice可不可能赢?
思路:每一次出拳Alice只能选择赢或则平两种选择,所以2-sat算法来做;
i 表示赢,i+n表示平,根据限制,我们可以建立关系图,然后2-sat判断是否矛盾
代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=20005;const int M=50005;struct node{ int now,next;}str[M];int map[N];int tot,head[N],vis[N],top,s[N];void add(int x,int y){ str[tot].now=y; str[tot].next=head[x]; head[x]=tot++;}bool dfs(int u,int n){ if(vis[u+n]) return false; if(vis[u]) return true; s[top++]=u; vis[u]=1; for(int i=head[u];i!=-1;i=str[i].next) { int v=str[i].now; if(!dfs(v,n)) return false; } return true;}bool ok(int n){ memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { if(!vis[i]&&!vis[i+n]) { top=0; if(!dfs(i,n)) { while(top>0) vis[s[--top]]=0; if(!dfs(i+n,n)) return false; } } } return true;}int main(){ int t,T=1; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); memset(head,-1,sizeof(head)); tot=0; for(int i=0;i<n;i++) scanf("%d",&map[i]); for(int i=0;i<m;i++) { int a,b,k; scanf("%d%d%d",&a,&b,&k); a--;b--; if(k==0) { if(map[a]==map[b]) { add(a,b); //a表示赢,k=0,出拳一样则b也必定赢 add(b,a); add(b+n,a+n); add(a+n,b+n); } else { if(map[a]==1&&map[b]==2) { add(a,b+n); add(b+n,a); add(a+n,a); add(b,b+n); } else if(map[a]==1&&map[b]==3) { add(a,a+n); add(b,a+n); add(a+n,b); add(b+n,b); } else if(map[a]==2&&map[b]==3) { add(a,b+n); add(a+n,a); add(b,b+n); add(b+n,a); } else if(map[a]==2&&map[b]==1) { add(b,a+n); add(a+n,b); add(b+n,b); add(a,a+n); } else if(map[a]==3&&map[b]==1) { add(b,b+n); add(a,b+n); add(b+n,a); add(a+n,a); } else if(map[a]==3&&map[b]==2) { add(b,a+n); add(b+n,b); add(a,a+n); add(a+n,b); } } } else { if(map[a]==map[b]) { add(a,b+n); add(b,a+n); add(b+n,a); add(a+n,b); } else { if(map[a]==1&&map[b]==2) { add(a,b); add(b+n,a+n); } else if(map[a]==1&&map[b]==3) { add(b,a); add(a+n,b+n); } else if(map[a]==2&&map[b]==3) { add(a,b); add(b+n,a+n); } else if(map[a]==2&&map[b]==1) { add(b,a); add(a+n,b+n); } else if(map[a]==3&&map[b]==1) { add(a,b); add(b+n,a+n); } else if(map[a]==3&&map[b]==2) { add(b,a); add(a+n,b+n); } } } } printf("Case #%d: ",T++); if(ok(n)) printf("yes/n"); else printf("no/n"); }}
新闻热点
疑难解答