题干:

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.

题目大意:

  给n个字符串,问你能否通过首尾相连来把所有串串成一串。n=1e5

解题报告:

别忘判连通!

•半欧拉图 :具有欧拉通路而无欧拉回路的图

•欧拉通路:从图的某一个顶点出发,图中每条边走且仅走一次,最后到达某一个点;如果这样的路径存在,则称之为欧拉路径。

•欧拉回路:从图的某一个顶点出发,图中每条边走且仅走一次,最后回到出发点;如果这样的回路存在,则称之为欧拉回路。

•无向图欧拉通路存在条件:至多有两个顶点的度数为奇数,其他顶点的度数均为偶数。

•有向图欧拉通路存在条件:至多有两个顶点的入度和出度绝对值差1(若有两个这样的顶点,则必须其中一个出度大于入度,另一个入度大于出度),其他顶点的入度与出度相等。

•无向图欧拉回路存在条件:所有顶点的度数均为偶数并且连通。

•有向图欧拉回路存在条件:所有顶点的入度和出度相等并且连通。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
char s[MAX];
int n;
int f[MAX],in[MAX],out[MAX]; 
int getf(int v) {
	return f[v] == v ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {
	int t1 = getf(u);
	int t2 = getf(v);
	f[t2] = t1;
}
int main()
{
	int t;
	cin>>t;
	while(t--) {
		scanf("%d",&n);
		for(int i = 1; i<=127; i++) f[i] = i,in[i]=out[i]=0;
		for(int i = 1; i<=n; i++) {
			scanf("%s",s+1);
			char st = s[1];
			char ed = s[strlen(s+1)];
			merge(st,ed);
			in[ed]++;
			out[st]++;		
		}
		int flag = 1,ru=0,chu=0,ans=0;
		for(int i = 'a'; i<='z'; i++) {
			if(!in[i] && !out[i]) continue;
			if(f[i] == i) ans++;
			if(in[i] == out[i]) continue;
			else if(in[i] - out[i] == 1) ru++;
			else if(out[i] - in[i] == 1) chu++;
			else {flag = 0;break;}
		}
		if(ans>1||flag == 0 || ru>1 || chu>1 || ru!=chu) puts("The door cannot be opened.");
		else puts("Ordering is possible.");
	} 


	return 0 ;
}