HDU1116 Play on Words 【歐拉圖】+【並查集】,hdu1116歐拉圖

來源:互聯網
上載者:User

HDU1116 Play on Words 【歐拉圖】+【並查集】,hdu1116歐拉圖

Play on WordsTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5736    Accepted Submission(s): 1897

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.
 
SourceCentral Europe 1999 經典題。
題意:給出N個字串,當一個字串的末尾單詞跟另一個字串的首單詞相等時,這兩個字串可以首尾串連。問是否存在一種方案使得這些字串連成一個序列。題解:比賽時破天荒的往SPFA鬆弛最長路徑上想去了,結果在構圖上糾結了很久,反而把以前學過的歐拉圖忘得精光,阿西吧,好在這道題算是複習了一下並查集和歐拉圖。無向歐拉圖:在圖聯通的情況下所有點的度為偶數或者有且只有兩個點的度為奇數。有向歐拉圖:在圖聯通的情況下所有的點入度等於出度,或者只有一個點入度比出度大1同時只有一個點入度比出度小1,不得出現入度跟出度絕對值大於1的點。

#include <stdio.h>#include <string.h>#define maxn 28int pre[maxn], vis[maxn], in[maxn], out[maxn];char str[1002];int abs(int k) { return k < 0 ? -k : k; }int ufind(int k) {int a = k, b;while(pre[k] != -1) k = pre[k];while(a != k) {b = pre[a];pre[a] = k;a = b;}return k;}bool unite(int a, int b) {a = ufind(a);b = ufind(b);if(a == b) return false;else {pre[a] = b;return true;}}int main() {int T, N, i, j, flag1, flag2, cnt, a, b;scanf("%d", &T);while(T--) {scanf("%d", &N);memset(pre, -1, sizeof(pre));memset(vis, 0, sizeof(vis));memset(in, 0, sizeof(in));memset(out, 0, sizeof(out));cnt = flag1 = flag2 = 0;while(N--) {scanf("%s", str);a = *str - 'a';b = str[strlen(str)-1] - 'a';if(!vis[a]) {vis[a] = 1; ++cnt;}if(!vis[b]) {vis[b] = 1; ++cnt;}++out[a]; ++in[b];if(unite(a, b)) --cnt;}if(cnt != 1) { // 森林printf("The door cannot be opened.\n");} else {for(i = 0; i < 26; ++i) {if(in[i] + 1 == out[i])++flag1;else if(out[i] + 1 == in[i])++flag2;else if(abs(in[i] - out[i]) > 1)break;}if(i < 26) printf("The door cannot be opened.\n");else if(!flag1 && !flag2 || flag1 == 1 && flag2 == 1)printf("Ordering is possible.\n");}}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.