2013
11-27

# 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.

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.

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.”.

3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok

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

①首先看欧拉回路存在性的判定：

三.混合图欧拉回路

②.欧拉路径存在性的判定

1.并查集判断连通

2.将每个单词取出首字母和尾字母，转换为一条边，然后加入对应的连通分量中。如果这个字母出现过，visit数组标记为true。同时起点出度加1，终点入度加1.

3.判断一下：

1）这个图必须是连通的，即根结点只有一个。如果不是，直接结束本次算法。

2）如果这个图是连通的，判断每个结点的入度和出度情况。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define MAXN 30
int pre[MAXN], in[MAXN], out[MAXN];
bool visit[MAXN];

int find(int x)
{
return x == pre[x] ? x : find(pre[x]);
}

void join(int x, int y)
{
int root1, root2;
root1 = find(x);
root2 = find(y);
if(root1 != root2)
pre[root2] = root1;
}

int main()
{
int ncase;
int wordnum, len; //单词个数，每个单词长度
int start, end; //转化为边
char str[1010];
int innum, outnum; //记录入度出度不相等顶点个数
int root; //根结点个数
bool flag; //判断连通性
bool flag1; //判断入度和出度是否是1或者0
scanf("%d", &ncase);
while(ncase--)
{
memset(in, 0, sizeof(in));
memset(out, 0, sizeof(out));
memset(visit, false, sizeof(visit));
for(int i = 1; i < MAXN; ++i)
pre[i] = i;
innum = outnum = root = 0;
flag = flag1 = true;
scanf("%d", &wordnum);
for(int i = 1; i <= wordnum; ++i)
{
scanf("%s", str);
len = strlen(str);
start = str[0] - 'a' + 1;
end = str[len - 1] - 'a' + 1;
visit[start] = true;
visit[end] = true;
out[start]++;
in[end]++;
join(start, end);
}
for(int i = 1; i < MAXN; ++i)
{
if(visit[i])
{
if(pre[i] == i)
root++;
if(in[i] != out[i])
{
if(in[i] - out[i] == 1)
innum++;
else if(out[i] - in[i] == 1)
outnum++;
else
flag1 = false;
}
}
if(root > 1)
{
flag = false;
break;
}
}
if((flag && innum == 0 && outnum == 0 && flag1) || (flag && innum == 1 && outnum == 1 && flag1))
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return 0;
}

1. 题本身没错，但是HDOJ放题目的时候，前面有个题目解释了什么是XXX定律。
这里直接放了这个题目，肯定没几个人明白是干啥