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

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.

欧拉道路的定义是: 除了起点和终点外, 其他点的“进出” 次数应该相等。 换句话说,除了起点和终点外, 其他点的度数应该是偶数。

对于有向图, 则必须其中一个点的出度恰好比入度大1, 另一个的入度比出度大。

如果奇点数不存在的话, 则可以从任意点出发,最终一定会回到该点(成为欧拉回路)。

一共26个字母 , 把字母转化成数字 , 存入图中。

代码:

#include <stdio.h>
#include <cstring>
#include <string> 
#include <iostream>
using namespace std;
const int maxn = 100;
int G[maxn][maxn] , vis[maxn] , in[maxn] , out[maxn] , T , n;
void dfs(int u)//用dfs判断是否为连通图 
{
	vis[u] = 1;
	for(int i = 0 ; i < 26 ; i++)
	{
		if(!vis[i] && G[u][i])
		{
			dfs(i);
		}
	}
}
int main()
{
	string s;
	scanf("%d" , &T);
	while(T--)
	{	
	memset(vis , 1 , sizeof(vis));//注意 , vis的初始标记是 1; 
	memset(G , 0 , sizeof(G));
	memset(in , 0 , sizeof(in));
	memset(out , 0 , sizeof(out));
		scanf("%d" , &n);
		while(n--)
		{
			cin >> s;
			int r = s[0] - 'a';
			int t = s[s.length()-1] - 'a';
			G[r][t] = 1;
			in[t]++;out[r]++;
			vis[r] = vis[t] = 0;//取消标记 , 用dfs时再将经过的点标记为1 
		}
		int p = 0 , cnt1 = 0 , cnt2 = 0 , cnt3 = 0 , flag = 0;
		for(int i = 0 ; i < 26 ; i++)
		{
			if(in[i] == out[i])//出度 = 入度 处于中间 
			{
				continue;
			}
			if(in[i] == out[i]+1)//起始点 
			{
				cnt1++;
			}
			else if(out[i] == in[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(p);
		for(int i = 0 ; i < 26 ; i++)//循环一遍 , 如果有vis[i]=0则表示不是连通图 
		{
			if(!vis[i])
			{
				flag = 0;
			}
		}
		if(flag == 1)
		{
			printf("Ordering is possible.\n");
		}
		else
		{
			printf("The door cannot be opened.\n");
		}
	}
	return 0;
}