POJ1368 & HDOJ1116 Play on Words(欧拉回路 + 并查集)

来源:互联网 发布:淘宝店铺人群消费层级 编辑:程序博客网 时间:2024/06/05 05:43

Play on Words
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 10844 Accepted: 3695

Description

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

32acmibm3acmmalformmouse2okok

Sample Output

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


给出n个字符串,首位相连,问你能不能做成成语接龙游戏。


此题的关键就是将字符串转化为边,记录首尾字母。


AC代码:


#include "iostream"#include "cstdio"#include "cstring"#include "algorithm"using namespace std;const int MAXN = 30;int n, par[MAXN], ran[MAXN], in[MAXN], out[MAXN];bool vis[MAXN];int find(int x){if(x == par[x]) return x;return par[x] = find(par[x]);}void merge(int x, int y){x = find(x);y = find(y);if(x == y) return;if(ran[x] > ran[y]) par[y] = x;else {if(ran[x] == ran[y]) ran[y]++;par[x] = y;}}int main(int argc, char const *argv[]){int t;scanf("%d", &t);while(t--) {memset(ran, 0, sizeof(ran));memset(vis, false, sizeof(vis));memset(in, 0, sizeof(in));memset(out, 0, sizeof(out));scanf("%d", &n);char s[1010];for(int i = 0; i < MAXN; ++i)par[i] = i;for(int i = 0; i < n; ++i) {scanf("%s", s);int a = s[0] - 'a', b = s[strlen(s) - 1] - 'a';vis[a] = vis[b] = true;out[a]++, in[b]++;merge(a, b);}int cnt = 0;for(int i = 0; i < 26; ++i) {if(vis[i] && find(i) == i) cnt++;if(cnt > 1) break;}if(cnt > 1) {printf("The door cannot be opened.\n");continue;}int s1 = 0, s2 = 0;bool flag = true;for(int i = 0; i < 26; ++i)if(vis[i] && in[i] != out[i]) {if(out[i] == in[i] + 1) s1++;else if(in[i] == out[i] + 1) s2++;else {flag = false;break;}}if(flag && s1 <= 1 && s2 <= 1) printf("Ordering is possible.\n");else printf("The door cannot be opened.\n");}return 0;}


1 0