标签:oss space mem put 有向图 names stream col strlen
///单词连接,欧拉回路通路都可以(有向图) ///主要构图:比如possibilities就构造p->s的边 #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; const int N=26; char s[1003]; int f[28],outd[28],ind[28]; int find(int x){ return x==f[x]?x:f[x]=find(f[x]); } int main(){ int t; scanf("%d",&t); while(t--){ int n; for(int i=0;i<N;i++) f[i]=i; memset(outd,0,sizeof(outd)); memset(ind,0,sizeof(ind)); scanf("%d",&n); while(n--){ scanf("%s",s); int u=s[0]-‘a‘; int v=s[strlen(s)-1]-‘a‘; ///u-->v int a=find(u); int b=find(v); if(a!=b) f[a]=b; outd[u]++; ind[v]++; } int flag=0,sum=0; int sum_in=0,sum_out=0; for(int i=0;i<N;i++){ if(f[i]==i&&(outd[i]+ind[i])>0) sum++; if(outd[i]!=ind[i]){ if(outd[i]+1==ind[i]) sum_in++; else if(outd[i]-1==ind[i]) sum_out++; else flag=1; } } if(!flag&&sum==1&&(sum_out==0&&sum_in==0||sum_in==1&&sum_out==1)) puts("Ordering is possible."); else puts("The door cannot be opened."); } return 0; }
标签:oss space mem put 有向图 names stream col strlen
原文地址:https://www.cnblogs.com/starve/p/10713734.html