Problem 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 ( <= N <= ). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 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
acm
ibm acm
malform
mouse ok
ok
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
Source
先将头尾转换为数字。算出各个点的入出度数。标记0-26哪些点有出现。
判断是否为欧拉通路。1、根<=1
2、有0个入出度数不同 || 2个入出度数不同且相差1
其余全部不是欧拉通路。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1006
#define inf 1e12
int n;
char s[N];
int a[];
int b[];
int vis[],fa[];
void init(){
for(int i=;i<;i++){
fa[i]=i;
}
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int x,int y){
int root1=find(x);
int root2=find(y);
if(root1==root2) return;
fa[root1]=root2;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
init();//尼玛又给忘了。。。
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s",s);
int len=strlen(s);
int num1=s[]-'a';
int num2=s[len-]-'a';
merge(num1,num2);
b[num1]++;
a[num2]++;
vis[num1]=;
vis[num2]=;
}
int cnt=;
for(int i=;i<;i++){
if(find(i)==i && vis[i]){
cnt++;
}
}
if(cnt>){
printf("The door cannot be opened.\n");
continue;
}
int tmp=;
int p[];
for(int i=;i<;i++){
if(a[i]!=b[i] && vis[i]){
p[tmp]=i;
tmp++;
}
}
if(tmp==){
printf("Ordering is possible.\n");
continue;
}
if(tmp== && (a[p[]]-b[p[]]== && b[p[]]-a[p[]]== || b[p[]]-a[p[]]== && a[p[]]-b[p[]]==)){
printf("Ordering is possible.\n");
continue;
}
printf("The door cannot be opened.\n");
}
return ;
}