日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UVA10129 Play on Words (并查集判连通+欧拉回路)

發(fā)布時間:2023/12/4 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UVA10129 Play on Words (并查集判连通+欧拉回路) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目解析:

輸入一些英文單詞,根據(jù)該單詞的首尾字母,判斷所有單詞能不能連成一串, 類似于成語接龍的意思。同樣如果有多個重復(fù)的單詞時,也必須滿足這樣的條件才能通過, 否則都是不可能的情況。輸入包括若干個案例,每個案例中最多有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 ok

Sample Output

The door cannot be opened. Ordering is possible. The door cannot be opened.

思路解析:

該題所涉及的知識點主要是并查集和歐拉回路,歐拉回路又是什么呢?它跟我們 今天所做的“成語接龍”有什么聯(lián)系呢?學(xué)過離散的同學(xué)都知道,歐拉回路是指,在圖中,經(jīng) 過所有的邊一次且僅一次的回路就是歐拉回路。而經(jīng)過所有的頂點一次且僅一次的回路叫做 哈密頓回路。

現(xiàn)在我們考慮每個單詞有用的成分只有首尾字母,那么我們把單詞的首尾字母 提取出來,抽象成兩個頂點,而恰恰這兩個頂點又是有了聯(lián)系的,我們把它們放入一個集合 。實際上,無論輸入多少個單詞,首尾字母中所出現(xiàn)的字母無外乎a,b,c……y,z等26個英文字 母。所以反客為主,我們不用被動的根據(jù)輸入的單詞來變換,首先建立26個集合,將有聯(lián)系 的集合合并(merge)則矣。當(dāng)做完這個工作后,我們再來看看歐拉回路存在性的判定定理:

一、無向圖 每個頂點的度數(shù)都是偶數(shù),則存在歐拉回路。

二、有向圖(所有邊都是單向的) 每個節(jié)頂點的入度都等于出度,則存在歐拉回路。

?三.混合圖歐拉回路 混合圖歐拉回路用的是網(wǎng)絡(luò)流。

把該圖的無向邊隨便定向,計算每個點的入度和出度。如果有某個點出入度之差為奇數(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; }

?

總結(jié)

以上是生活随笔為你收集整理的UVA10129 Play on Words (并查集判连通+欧拉回路)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。