杭电1116 Play on Words(并查集+欧拉路)

Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5743    Accepted Submission(s): 1899


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

Source
Central Europe 1999 
/*
欧拉路,只有起点入度比出度大1,终点入度比出度小1,其余相等
		或者均为0 
欧拉回路,所有顶点 都是偶数度 
一笔画问题:奇点的个数只能为0或2个 
我去,今天什么情况啊,,,做搜索把x写成y,这道题也是不x写成y。。。。。无语了
Time:2014-11-27 21:21
*/
#include
#include
#include
#include
using namespace std;
const int MAX=30;
int father[MAX];
int in[MAX],out[MAX];
bool vis[MAX];
int find(int x){
	return father[x]==x?x:father[x]=find(father[x]);
}
void Init(){
	memset(in,0,sizeof(in));
	memset(out,0,sizeof(out));
	memset(vis,0,sizeof(vis));
	for(int i=0;i<=30;i++){
		father[i]=i;
	}
}
int main(){
	int T,n;
	char s[1002];
	int u,v,i,j,a,b;
	scanf("%d",&T);
	while(T--){
		Init();
		scanf("%d",&n);
		for(i=0;i1){
			puts("The door cannot be opened.");
			continue;
		}
		int x,y,z;x=y=z=0;
		for(i=1;i<=26;i++){
			if(vis[i]&&in[i]!=out[i]){
				if(in[i]==out[i]+1){
					x++;
				}else if(in[i]+1==out[i]){
					y++;
				}else{
					z++;
				}
			}
		}
		if(z)puts("The door cannot be opened.");
		else if((x==1&&y==1)||(x==0&&y==0)){
			puts("Ordering is possible.");
		}else{
			puts("The door cannot be opened.");
		} 
		
	} 
return 0;
} 

你可能感兴趣的:(并查集)