标签:des c style class blog code
Description
Input
Output
Sample Input
Sample Output
Hint
‘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..
这个题有意思了啊,看着感觉是3-SAT,可是分析一下是2-SAT,简单,所谓的2-SAT不就是建立一个有向图,再求SCC,以就是强连通分量,再看看同一个点的两种状态在不在同一个SCC上,简单题,
#include<map> #include<set> #include<stack> #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<string> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define inf 0x0f0f0f0f using namespace std; int finger[10000+10][2]; struct SCC { static const int maxn=20000+10; vector<int>group[maxn],scc[maxn]; int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt,n,m; stack<int>S; void init() { for (int i=0;i<=n;i++) group[i].clear(); } void addedge(int from,int to) { group[from].push_back(to); } void dfs(int u) { pre[u]=lowlink[u]=++dfs_clock; S.push(u); for (int i=0;i<group[u].size();i++) { int v=group[u][i]; if (!pre[v]) { dfs(v); lowlink[u]=min(lowlink[u],lowlink[v]); } else if (!sccno[v]) { lowlink[u]=min(lowlink[u],pre[v]); } } if (lowlink[u]==pre[u]) { scc_cnt++; scc[scc_cnt].clear(); while (1) { int x=S.top(); S.pop(); scc[scc_cnt].push_back(x); sccno[x]=scc_cnt; if (x==u) break; } } } void find_scc() { dfs_clock=scc_cnt=0; memset(pre,0,sizeof(pre)); memset(sccno,0,sizeof(sccno)); for (int i=1;i<=n;i++) if (!pre[i]) dfs(i); } }; SCC Conflict; int main() { int T,N,M,x,y,c,cas=0; scanf("%d",&T); while (T--) { cas++; scanf("%d%d",&N,&M); Conflict.n=N*2; Conflict.init(); for (int i=0;i<N;i++) { scanf("%d",&x); if (x==1) { finger[i][0]=1; finger[i][1]=2; } if (x==2) { finger[i][0]=2; finger[i][1]=3; } if (x==3) { finger[i][0]=3; finger[i][1]=1; } } for (int i=1;i<=M;i++) { scanf("%d%d%d",&x,&y,&c); x--;y--; if (c==0) { for (int j=0;j<2;j++) { for (int k=0;k<2;k++) if (finger[x][j]!=finger[y][k]) { Conflict.addedge(x*2+j,(y*2+k)^1); Conflict.addedge(y*2+k,(x*2+j)^1); } } } if (c==1) { for (int j=0;j<2;j++) { for (int k=0;k<2;k++) if (finger[x][j]==finger[y][k]) { Conflict.addedge(x*2+j,(y*2+k)^1); Conflict.addedge(y*2+k,(x*2+j)^1); } } } } Conflict.find_scc(); bool ans=true; for (int i=0;i<N;i+=2) { if (Conflict.sccno[i]==Conflict.sccno[i+1]) { ans=false; break; } } if (ans) printf("Case #%d: yes\n",cas); else printf("Case #%d: no\n",cas); } return 0; }
作者 chensunrise
hdu 4115 Eliminate the Conflict,布布扣,bubuko.com
hdu 4115 Eliminate the Conflict
标签:des c style class blog code
原文地址:http://www.cnblogs.com/chensunrise/p/3756025.html