Play on Words(POJ 1386)

Topic Link

Title 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 Format

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 Format

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.

Tips / Description

analysis

Subject to the effect that a set of total t test, each test contains a number n and the n plates, wherein each plate has a top word. Solitaire now required word, two words connection condition is the same as before and the last letter of a word after the last letter of a word, it can be used to ask whether to pick up all words. If you can output "Ordering is possible.", Otherwise output "The door can not be opened. ".
Obviously, this is a judgment whether there is a way to Tu Oula, one from each word is the first letter of the last letter pointing to the side, so the lowercase letters vertex, its statistics to determine the degree of access.

Euler path / decision circuit

Directed graph

  • Euler passage: FIG communication is, FIG singularity of only two points, two endpoints are Euler path. For Euler path, in addition to the start and end points, each point if they go into, obviously you must go out, and therefore are even point.
  • Euler: FIG communication is, even points are of degree. For Euler, enter each point is equal to the number of times out, so there is no singular point.

Undirected graph

  • Euler passage: FIG communication is, in addition to the two vertices of the remaining points is equal to a degree, and the two vertices, one vertex of the big one (the starting point), the other is smaller than the ratio of the degree of 1 (end)
  • Euler: graph is connected, the figures for all points equal to the degree.

Source

//#include <bits/stdc++.h>	//poj不能用万能头
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define MAXN 1005
using namespace std;
char str[MAXN];
int t,n,in[30],out[30],father[30];
int find(int x)
{
	if(x==father[x])return x;
	return father[x]=find(father[x]);
}
int Union(int x,int y)
{
	x=find(x);
	y=find(y);
	if(x!=y)father[x]=y;
}
int main()
{
	scanf("%d",&t);	//测试组数 
	while(t--){
		for(int i=0;i<26;i++){	//初始化 
			in[i]=out[i]=0;
			father[i]=i;
		}
		scanf("%d",&n);
		for(int i=1;i<=n;i++){	//统计度数并更新并查集 
			scanf("%s",str);
			int len=strlen(str);
			int x=str[0]-'a',y=str[len-1]-'a';
			out[x]++;	 
			in[y]++;
			Union(x,y);
		}
		int cnt=0;	//统计连通分量 
		for(int i=0;i<26;i++)
			if((in[i]||out[i])&&i==father[i])cnt++;
		if(cnt>1)
			printf("The door cannot be opened.\n");
		else{
			bool flag=false;
			int s=0,t=0; //统计起点和终点个数 
			for(int i=0;i<26;i++){
				if(in[i]==out[i])continue;
				else if(out[i]==in[i]+1) s++;	//起点数增加 
				else if(in[i]==out[i]+1) t++;	//终点数增加 
				else {		//不存在欧拉路 
					flag=1;
					break; 
				}
			}
			if(!flag&&(s==0&&t==0||s==1&&t==1))printf("Ordering is possible.\n");
			else printf("The door cannot be opened.\n");
			
		} 
	}
}
Published 19 original articles · won praise 0 · Views 136

Guess you like

Origin blog.csdn.net/weixin_43960284/article/details/105188623