标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10056 | Accepted: 3447 |
Description
Input
Output
Sample Input
3 2 acm ibm 3 acm malform mouse 2 ok ok
Sample Output
The door cannot be opened. Ordering is possible. The door cannot be opened.
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<cstdlib> #include<string> #include<vector> using namespace std; vector<int> e[27]; int tt,n,in[27],out[27],vis[27]; char s[100010]; void dfs(int x) { vis[x]=1; for(int i=0;i<e[x].size();i++) if(vis[e[x][i]]==0) dfs(e[x][i]); } bool ok(int x) { dfs(x); for(int i=0;i<26;i++) if(vis[i]==0&&e[i].size()!=0) return false; return true; } int main() { scanf("%d",&tt); while(tt--) { memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(vis,0,sizeof(vis)); int len,a,b; scanf("%d",&n); for(int i=0;i<27;i++) e[i].clear(); for(int i=0;i<n;i++) { scanf("%s",s); len=strlen(s); a=s[0]-‘a‘,b=s[len-1]-‘a‘; out[a]++,in[b]++; e[a].push_back(b); e[b].push_back(a); } if(!ok(a)) { printf("The door cannot be opened.\n"); continue; } bool flag=false; int j; for(j=0;j<26;j++) { if(in[j]!=out[j]) { if(in[j]<out[j]) { if(in[j]-out[j]==-1&&flag==0) { flag=1; } else { break; } } } } if(j<26) printf("The door cannot be opened.\n"); else printf("Ordering is possible.\n"); } return 0; }
注意连通性
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4245524.html