POJ 1385 Play on Words (有向图欧拉通路)

来源:互联网 发布:知乎人民币贬值 编辑:程序博客网 时间:2024/05/21 22:27

Play on Words
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 10123 Accepted: 3462

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.

Source

Central Europe 1999


题目链接:http://poj.org/problem?id=1386


题目大意:t组样例,每组n个字符串,如果一个字符串的尾字符和另一个的头字符相同则可以连接,问能不能将这n个都连接起来


题目分析:这题是poj2337的简单版,只需要判断即可,不用输出路径,但是坑爹的是内存只给了10000K,所以结构体肯定MLE,直接用整型变量存端点值即可,因为不用输出,边对我们没什么用,判断方法同poj2337


#include <cstdio>#include <cstring>#include <algorithm>using namespace std;bool has[30];int in[30], out[30], fa[30];int n, st, len;int abs(int x){    return x > 0 ? x : -x;}int change(char ch){    return int(ch - 'a' + 1);}void UF_set(){    for(int i = 0; i < 200; i++)        fa[i] = i;}int Find(int x){    return fa[x] == x ? x : fa[x] = Find(fa[x]);}void Union(int a, int b){    int r1 = Find(a);    int r2 = Find(b);    if(r1 != r2)        fa[r1] = r2;}bool exist(){    int t = -1;    for(int i = 1; i <= 26; i++)    {        if(has[i])        {            if(t == -1)                t = Find(i);            if(t != Find(i))                return false;        }    }    int sum = 0;    for(int i = 1; i <= 26; i++)    {        if(has[i])        {            if(in[i] != out[i])            {                if(abs(in[i] - out[i]) > 1)                    return false;                sum ++;            }        }    }    if(sum > 2)        return false;    return true;}int main(){    int T;    scanf("%d", &T);    while(T--)    {        UF_set();        memset(has, false, sizeof(has));        memset(in, 0, sizeof(in));        memset(out, 0, sizeof(out));        scanf("%d", &n);        char s[1005];        for(int i = 0; i < n; i++)        {            scanf("%s", s);            int x = change(s[0]);            int y = change(s[(int)strlen(s) - 1]);            has[x] = true;            has[y] = true;            in[x] ++;            out[y] ++;            Union(x, y);        }        if(exist())            printf("Ordering is possible.\n");        else            printf("The door cannot be opened.\n");    }}



0 0