标签:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7080 Accepted Submission(s): 2398
#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> using namespace std; const int N = 30; bool vis[N]; ///判断当前字母是否出现过 int father[N]; int indegree[N],outdegree[N]; ///入度和出度 int _find(int x){ if(x==father[x]) return x; return _find(father[x]); } int Union(int a,int b){ int x = _find(a); int y = _find(b); if(x==y) return 0; father[x] = y; return 1; } void init(){ memset(vis,false,sizeof(vis)); memset(indegree,0,sizeof(indegree)); memset(outdegree,0,sizeof(outdegree)); for(int i=0;i<N;i++) father[i]=i; } int main() { int tcase; scanf("%d",&tcase); while(tcase--){ int n; char str[1005]; scanf("%d",&n); init(); while(n--){ scanf("%s",str); int len = strlen(str); int s = str[0]-‘a‘; int e = str[len-1]-‘a‘; Union(s,e); vis[s] = vis[e] = true; indegree[e]++; outdegree[s]++; } int ans = 0; bool flag=false,flag1=false; int in=0,out=0; for(int i=0;i<N;i++){ if(vis[i]){ if(father[i]==i) ans++; if(indegree[i]!=outdegree[i]){ if(indegree[i]-outdegree[i]==1) in++; else if(outdegree[i]-indegree[i]==1) out++; else flag1 = true; } } if(ans>1){ flag = true; } } if(!flag&&!flag1&&in==0&&out==0) printf("Ordering is possible.\n"); else if(!flag&&!flag1&&in==1&&out==1) printf("Ordering is possible.\n"); else printf("The door cannot be opened.\n"); } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5479129.html