UVA10129 Play on Words (并查集判连通+欧拉回路)
題目解析:
輸入一些英文單詞,根據(jù)該單詞的首尾字母,判斷所有單詞能不能連成一串, 類似于成語接龍的意思。同樣如果有多個重復的單詞時,也必須滿足這樣的條件才能通過, 否則都是不可能的情況。輸入包括若干個案例,每個案例中最多有100000個單詞。
題目:
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us.?
There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.?
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list.?
Output
Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times.?
If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".?
Sample Input
3 2 acm ibm 3 acm malform mouse 2 ok okSample Output
The door cannot be opened. Ordering is possible. The door cannot be opened.思路解析:
該題所涉及的知識點主要是并查集和歐拉回路,歐拉回路又是什么呢?它跟我們 今天所做的“成語接龍”有什么聯(lián)系呢?學過離散的同學都知道,歐拉回路是指,在圖中,經 過所有的邊一次且僅一次的回路就是歐拉回路。而經過所有的頂點一次且僅一次的回路叫做 哈密頓回路。
現(xiàn)在我們考慮每個單詞有用的成分只有首尾字母,那么我們把單詞的首尾字母 提取出來,抽象成兩個頂點,而恰恰這兩個頂點又是有了聯(lián)系的,我們把它們放入一個集合 。實際上,無論輸入多少個單詞,首尾字母中所出現(xiàn)的字母無外乎a,b,c……y,z等26個英文字 母。所以反客為主,我們不用被動的根據(jù)輸入的單詞來變換,首先建立26個集合,將有聯(lián)系 的集合合并(merge)則矣。當做完這個工作后,我們再來看看歐拉回路存在性的判定定理:
一、無向圖 每個頂點的度數(shù)都是偶數(shù),則存在歐拉回路。
二、有向圖(所有邊都是單向的) 每個節(jié)頂點的入度都等于出度,則存在歐拉回路。
?三.混合圖歐拉回路 混合圖歐拉回路用的是網絡流。
把該圖的無向邊隨便定向,計算每個點的入度和出度。如果有某個點出入度之差為奇數(shù),那 么肯定不存在歐拉回路。因為歐拉回路要求每點入度 = 出度,也就是總度數(shù)為偶數(shù),存在 奇數(shù)度點必不能有歐拉回路。
AC代碼:
#include<iostream> #include<string.h> #define inf 0x3f3f3f3f using namespace std; int t,m,book[110]; char w[1010]; int dp[110],u[110],v[110]; int gets(int xx) {if(xx==dp[xx])return xx;elsereturn gets(dp[xx]); } void dfs(int x,int y) {int dx=gets(x);int dy=gets(y);if(dx!=dy)dp[dy]=dx; } int main() {cin>>t;while(t--){for(int i=0; i<26; i++)dp[i]=i;cin>>m;memset(book,0,sizeof(book));memset(u,0,sizeof(u));memset(v,0,sizeof(v));/*care*/for(int i=0; i<m; i++){cin>>w;int l=strlen(w);int a=w[0]-'a';int b=w[l-1]-'a';book[a]=1;book[b]=1;u[a]++;v[b]++;dfs(a,b);}int da=0,db=0;int flag=1;for(int i=0; i<26; i++){if(!flag)break;if(!book[i])continue;if(u[i]-v[i]>1||u[i]-v[i]<-1)flag=0;if(u[i]-v[i]==1){da++;if(da>1)flag=0;}if(u[i]-v[i]==-1){db++;if(db>1)flag=0;}}int ans=0;for(int i=0; i<26; i++)if(book[i]&&dp[i]==i){ans++;if(ans>1){flag=0;break;}}if(!flag)cout<<"The door cannot be opened."<<endl;elsecout<<"Ordering is possible."<<endl;}return 0; }?
總結
以上是生活随笔為你收集整理的UVA10129 Play on Words (并查集判连通+欧拉回路)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 刮痧之后可以洗澡吗
- 下一篇: Sorting It All Out