PKU1386 Play on Words【并查集+欧拉思想】
题目链接:http://poj.org/problem?id=1386
这题主要是欧拉思想,有向图,出度与入度之间的恩怨。。。有兴趣自行搜之~
代码:
pku 1386
1 #include<cstdio> 2 #include<cstring> 3 4 int root[30]; 5 char str[1010]; 6 int In[30],Out[30];//入度与出度 7 8 void Init(){ 9 memset(In,0,sizeof(In)); 10 memset(Out,0,sizeof(Out)); 11 for(int i=0;i<30;i++) root[i]=i; 12 } 13 14 inline int Find(int x){ return root[x]=root[x]==x?x:Find(root[x]);} 15 16 inline void Union(int a,int b){ 17 int ra=Find(a),rb=Find(b); 18 root[rb]=ra; 19 } 20 21 int main(){ 22 int n,i,t,len,u,v,start; 23 scanf("%d",&t); 24 while(t--){ 25 Init();//预处理 26 scanf("%d",&n); 27 for(i=0;i<n;i++){ 28 scanf("%s",str); 29 u=str[0]-'a',v=str[strlen(str)-1]-'a'; 30 In[u]++; 31 Out[v]++; 32 Union(u,v); 33 } 34 int ia=0,ib=0,res=Find(str[0]-'a'); 35 for(i=0;i<30;i++){ 36 if(!In[i]&&!Out[i]) continue; 37 if(Find(i)!=res) break;//如果没连通,停 38 if(In[i]==Out[i]) continue; 39 if(In[i]-Out[i]==1) continue; 40 if(Out[i]-In[i]==1) continue; 41 break;//其他情况,停。 42 } 43 if(i<30) puts("The door cannot be opened."); 44 else puts("Ordering is possible."); 45 } 46 return 0; 47 }


浙公网安备 33010602011771号