poj 1386 Play on Words
2014-05-14 01:34
323 查看
Play on Words
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. Source Central Europe 1999 |
思路:本题主要有两个关键,一是判断欧拉回路,二是判断基图是否连通,判断图连通的时候可以用并查集来平判断。将每个单词的首尾字母作为图的结点,每个单词为图中的一条边。
#include<algorithm> #include<iostream> #include<cstdio> #include<cstring> using namespace std; int in[26] , out[26];///每个字母的出度和入度。 #define maxn 100001 int T , N , used[26] , parent[26];///used数组表示改字母是否作为首尾字母 char word[1001]; struct node { int u , v; }edges[maxn]; void UFset() { for(int i=0; i<26; ++i) parent[i] = -1; } int Find(int x)///赤裸裸的并查集,无话可说,随便怎么写=。= { int s; for(s=x; parent[s] >= 0; s=parent[s]); while(s != x) { int tmp = parent[x]; parent[x] = s; x = tmp; } return s; } void Union(int r1 , int r2) { int root1 = Find(r1) , root2 = Find(r2); int tmp = parent[root1] + parent[root2]; if(parent[root1] > parent[root2]) { parent[root1] = root2; parent[root2] = tmp; } else { parent[root2] = root1; parent[root1] = tmp; } } bool bconnect()///判断图是否连通 { int u , v , i; UFset(); for(int i=0; i<N; ++i)///对每条边(u,v),如果u和v不属于同一个连通分量,将其合并。 { u = edges[i].u; v = edges[i].v; if(u != v && Find(u) != Find(v)) Union(u , v); } int root = -1;///第一个used[i]不为0的顶点。 for(i=0; i<26; ++i) { if(used[i] == 0) continue; if(root == -1) root = i; else if(Find(i) != Find(root)) break;///只有一个树根 } if(i < 26) return false;///不连通。 else return true; } int main() { int u , v; scanf("%d", &T); while(T--) { memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out)); memset(used, 0, sizeof(used)); scanf("%d", &N); for(int i=0; i<N; ++i) { scanf("%s", word); u = word[0] - 'a'; v = word[strlen(word) - 1] - 'a'; out[u]++; in[v]++; used[u] = used[v] = 1; edges[i].u = u;///建图 edges[i].v = v; } bool flag = true; int start = 0; ///有向图中如果有欧拉通路,最多只能有两个点出度与入度差为1或-1,并且如果有这种点,这两个点一定分别是起点和终点,这里start和end就是表示这两个点。 int End = 0; for(int i=0; i<26; ++i) { if(used[i] == 0) continue; if(out[i] - in[i] >= 2 || in[i] - out[i] >= 2) { flag = false; break; }///出度与入度的差的绝对值如果大于2,一定没有欧拉通路。 if(out[i] - in[i] == 1) { start++; if(start > 1) { flag = false; break; } } if(in[i] - out[i] == 1) { End++; if(End > 1) { flag = false; break; } } } if(start != End) flag = false;///判断起点数和终点数,因为可能出现只有一个点出度与入度差的绝对值为1的情况,这种情况是没有欧拉通路的。 if(!bconnect()) flag = false;///判断图是否连通。 if(flag) printf("Ordering is possible.\n"); else printf("The door cannot be opened.\n"); } return 0; }
相关文章推荐
- HDU 1116 && UVA 10129 && POJ 1386 ------ Play on Words(欧拉路径 + 并查集)
- POJ-1386 Play on Words
- poj1386 Play on Words【欧拉图】
- poj Play on Words(1386)
- poj1386 Play on words
- poj 1386 Play on Words(有向图欧拉回路)
- poj 1386 Play on Words (欧拉路判断)
- poj 1386 Play on Words 有向图欧拉路径判断
- poj1386 Play on Words (欧拉回路)
- HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)
- POJ 1386 Play on Words
- UVa 10129 & HDU 1116 & POJ 1386 - Play on Words
- poj 1386 Play on Words(欧拉通路)
- zoj 2016 || poj 1386 Play on Words
- poj 1386 Play on Words
- poj 1386 Play on Words
- poj 1386 Play on Words
- poj 1386 Play on Words
- poj_1386 Play on Words(欧拉路)
- POJ 1386 Play on Words