hdoj 1116 Play on Words 【並查集】+【歐拉路】,hdoj歐拉路
Play on WordsTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5622 Accepted Submission(s): 1850
Problem DescriptionSome 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.
InputThe 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.
OutputYour 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
32acmibm3acmmalformmouse2okok
Sample Output
The door cannot be opened.Ordering is possible.The door cannot be opened.
題意:給出幾個字串,如果一個字串的首字元(尾子符)等於另外一個字串的尾子符(首字元),就讓他們串連起來,問最後能不能把所有的字串都串連起來。
分析:很明顯的是要用到並查集的只是,但是處理首尾字元的時候會有點麻煩,我們不妨將沒一個字元的首尾字元都視為一個點,一個字串就是一條邊,那麼該題就轉化為了求邊能不能形成一條連通圖,之後就要用歐拉路來判斷改圖是否連通就好了。
註:歐拉路分為歐拉迴路和歐拉通路;
歐拉通路:滿足從一點出發經過每一條邊且只經過一次,能把所有的邊都經過的路
歐拉迴路:歐拉通路並且最後回到原點的路;
如果是歐拉迴路那麼圖中每個點的入讀和處度都相等
如果是通路那麼起始點的出度減入度為1, 終點處入度減出度為1。
代碼:
/*hdoj 1116 並查集+歐拉通/迴路*/#include <stdio.h>#include <string.h>#include <algorithm>#define M 1005int out[26], in[26], fat[26];bool vis[26];char s[M];int f(int x){if(x != fat[x]) fat[x] = f(fat[x]);return fat[x];}void merge(int x, int y){int a = f(x);int b = f(y);if(a != b) fat[a] = b;}int main(){int n, t, i;scanf("%d", &t);while(t --){memset(vis, 0, sizeof(vis));memset(out, 0, sizeof(out));memset(in, 0, sizeof(in));scanf("%d", &n);for(i = 0; i < 26; i ++) fat[i] = i;for(i = 0; i < n; i ++){scanf("%s", s);int x = s[0]-'a';int y = s[strlen(s)-1]-'a';merge(x, y);++out[x]; ++in[y];vis[x] = vis[y] = 1;}int flag1 = 0;for(i = 0; i < 26; i ++){ //判斷是否聯連通if(vis[i]&&fat[i] == i) ++flag1; }if(flag1 > 1){printf("The door cannot be opened.\n"); continue;}int flag2, flag3; //flag1是判斷是否是全部出入度都相等,flag2是判讀起始點有幾個,flag3是終點有幾個flag1 = flag2 = flag3 = 0;for(i = 0; i < 26; i ++){if(vis[i]&&out[i] != in[i]){++flag1;if(out[i]-in[i] == 1) ++flag2;if(in[i] - out[i] == 1) ++flag3;}}if(flag1 == 0) printf("Ordering is possible.\n");else if(flag1 == 2&&flag2 == 1&&flag3 == 1) printf("Ordering is possible.\n");else printf("The door cannot be opened.\n");}return 0;}