1. 程式人生 > >poj1386--Play on Words--歐拉路

poj1386--Play on Words--歐拉路

相等 con ble cst -a put algorithm ons include

題意:給你若幹個單詞,一個單詞能拼接到前一個單詞的標準是:此單詞的首字母和前一個單詞的尾字母相同。

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.

題解:

  構圖:每個單詞對應一條有向邊,從此單詞的首字母連向尾字母。如果其中存在歐拉路,說明可以拼接成鏈(歐拉通路)或環(歐拉回路)。

  首先存儲字母出現的信息,因為是多組數據,註意每次要初始化記錄數組。

  同時用used數組標記該字母是否出現在圖中,in數組和out數組分別記錄入度和出度。

  用一個並查集對整個構成的圖做合並處理,判斷此圖是否沒有孤立的點,如果有,說明不連通,需要輸出“The door cannot be opend."

  如果整張圖聯通,進一步遍歷這張圖。

  接下來判斷是否是歐拉路:

  1.如果有任何一個點的出入度之差大於1,就不是歐拉路;

  2.記錄出入度不相等的點,如果入度-出度=1的點超過1個,說明終點超過1個,不是歐拉路;

  3.記錄出入度不相等的點,如果出度-入度=1的點超過1個,說明起點超過了1個,也不是歐拉路。

技術分享
  1
#include<algorithm> 2 #include<cmath> 3 #include<cstdio> 4 #include<cstring> 5 #include<iostream> 6 using namespace std; 7 const int maxn=100009; 8 9 int n,m,in[maxn],out[maxn],fa[maxn]; 10 bool used[maxn]; 11 12 void init() 13 { 14 memset(used,0
,sizeof(used)); 15 memset(in,0,sizeof(in)); 16 memset(out,0,sizeof(out)); 17 for(int i=1;i<=n;i++) 18 fa[i]=i; 19 } 20 21 int find(int x) 22 { 23 if(x!=fa[x]) 24 fa[x]=find(fa[x]); 25 return fa[x]; 26 } 27 28 void uni_set(int x,int y) 29 { 30 int u=find(x),v=find(y); 31 if(u==v) 32 return; 33 fa[u]=v; 34 } 35 36 bool judge_eular() 37 { 38 int sta=0,end=0; 39 for(int i=1;i<=26;i++) 40 { 41 if(used[i]) 42 { 43 if(abs(in[i]-out[i])>1)return 0; 44 if(in[i]-out[i]==1) 45 { 46 end++; 47 if(end>1)return 0; 48 } 49 if(out[i]-in[i]==1) 50 { 51 sta++; 52 if(sta>1)return 0; 53 } 54 } 55 } 56 return 1; 57 } 58 59 int main() 60 { 61 int T; 62 scanf("%d",&T); 63 while(T--) 64 { 65 scanf("%d",&n); 66 init(); 67 int tmp; 68 char a[1009]; 69 for(int i=1;i<=n;i++) 70 { 71 scanf("%s",a); 72 int len=strlen(a); 73 int u=a[0]-a+1, v=a[len-1]-a+1; 74 in[v]++;out[u]++; 75 used[u]=used[v]=1; 76 tmp=v; 77 uni_set(u,v); 78 } 79 tmp=find(tmp); 80 bool ok=1; 81 for(int i=1;i<=26;i++) 82 { 83 if(used[i]&&tmp!=find(i)) 84 { 85 printf("The door cannot be opened.\n"); 86 ok=0; 87 break; 88 } 89 } 90 if(ok) 91 { 92 if(!judge_eular()) 93 { 94 printf("The door cannot be opened.\n"); 95 ok=0; 96 } 97 else 98 { 99 printf("Ordering is possible.\n"); 100 ok=1; 101 } 102 } 103 } 104 return 0; 105 }
View Code

poj1386--Play on Words--歐拉路