原题目:
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 N that indicates the number of plates (1 ≤ N ≤ 100000). Then exactly N lines 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.’
中文概要:
输入n个单词,是否可以把所有这些单词排成一个序列,使得每个单词的第一个字母和上一个单词的最后一个字母相同,每个单词最多包含1000个小写字母,输入中可以有重复的单词。
【分析】
把字母看成结点,单词看成有向边,则问题有解,有向图存在欧拉路径的条件有两个,一个是出入度满足要求,另一个是满足底图联通。
#include<algorithm>
#include<vector>
#include<queue>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX = 10000;
int vis[MAX], G[MAX][MAX], in[MAX], out[MAX];//出度入度数组
char c[MAX];
void dfs(int u) {
vis[u] = 1;
for (int v = 0; v < 26; v++) {
if (G[u][v] && !vis[v]) {
dfs(v);
}
}
}
int main() {
int T,n;
scanf("%d", &T);
while (T--) {
memset(vis, 1, sizeof(vis));
memset(G, 0, sizeof(G));
memset(in, 0, sizeof(in));
memset(out, 0, sizeof(out));
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", c);
int len = strlen(c);
int r = c[0] - 'a';//首位
int t = c[len - 1] - 'a';//末尾
in[t]++;//入度
out[r]++;//出度
G[r][t] = 1;
vis[r] = vis[t] = 0;
}
int cnt1=0, cnt2=0, cnt3=0;
int flag = 0;
int start = 0;//起始点
for (int i = 0; i < 26; i++) {
if (in[i] == out[i])
continue;
if (out[i] == in[i] + 1) {
cnt1++;
start = i;
}
else if (in[i] == out[i] + 1) cnt2++;
else cnt3++;
}
if (cnt3 > 0) {
printf("The door cannot be opened.\n");
continue;
}//只是判断完出度入度是否符合条件,但是还要判断图的连通性
if (cnt1 == 1 && cnt2 == 1 || cnt1 == 0 && cnt2 == 0) flag = 1;
else flag = 0;
dfs(start);//判断图的连通性
for (int i = 0; i < 26; i++) {
if (!vis[i]) {
flag = 0;
}
}
if(flag) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
return 0;
}