Play on Words

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.

/*
  题目大意:给你一些英文单词,判断所有单词能不能连成一串。但是如果有多个重复的单词时,也必须满足这样的条件才能算是。否则都是不可能的情况。
  题解:关于欧拉回路和欧拉路径:
        定义:
        欧拉回路:每条边恰好只走一次,并能回到出发点的路径
        欧拉路径:经过每一条边一次,但是不要求回到起始点
        ①首先看欧拉回路存在性的判定:
        一、无向图  每个顶点的度数都是偶数,则存在欧拉回路。
        二、有向图(所有边都是单向的) 每个节顶点的入度都等于出度,则存在欧拉回路。
        三.混合图欧拉回路  混合图欧拉回路用的是网络流。
      把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。
        因为欧拉回路要求每点入度 = 出度,也就是总度数为偶数,存在奇数度点必不能有欧拉回路。
      现在每个点入度和出度之差均为偶数。那么将这个偶数除以2,得x。也就是说,对于每一个点,只要将x条边改变方向(入>出就是变
        入,出>入就是变出),就能保证出 = 入。如果每个点都是出 = 入,那么很明显,该图就存在欧拉回路。
       ②.欧拉路径存在性的判定
        一、无向图
               一个无向图存在欧拉路径,当且仅当   该图所有顶点的度数为偶数   或者  除了两个度数为奇数外其余的全是偶数。
        二、有向图
               一个有向图存在欧拉路径,当且仅当 该图所有顶点的度数为零     或者 一个顶点的度数为1,另一个度数为-1,其他顶点的度数为0。
        三、混合图欧拉路径
              求欧拉路径的第一步一定是求欧拉回路,在混合图上也不例外,如何判断混合图欧拉回路问题的存在性呢?首先,
              我们用上文所说的方法判断该图是否存在欧拉回路,如果存在,欧拉路径一定存在。如果欧拉回路不存在,那么我们枚举欧拉路径的
              起点和终点,连接一条无向边,然后再用最大流判断是否存在欧拉回路即可。
*/
//标程:
#include<stdio.h>
#include<string.h>
#define N 30
int p[N],in[N],out[N],vis[N];
int find(int x)
{
    return x==p[x]?x:find(p[x]);
}
void join(int x,int y)
{
	int x1,y1;
	x1=find(x);
	y1=find(y);
	if(x1!=y1) p[y1]=x1;
}
int main()
{
	//freopen("a.txt","r",stdin);
    int t, n, len, start, end;
	char s[1010];
	int In, Out, root, i, flag, flag1;
	scanf("%d",&t);
	while(t--)
	{
		memset(in,0,sizeof(in));
		memset(out,0,sizeof(out));
		memset(vis,0,sizeof(vis));
		for(i=0;i<N;i++)  p[i]=i;
		In=Out=root=0;
		flag=flag1=1;
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			scanf("%s",s);
			len=strlen(s);
			start=s[0]-'a'+1;
			end=s[len-1]-'a'+1;
			vis[start]=vis[end]=1;
			out[start]++;
			in[end]++;
			join(start,end);
		}
		for(i=1;i<N;i++)
		{
			if(vis[i])
			{
				if(p[i]==i)	root++;
				if(in[i]!=out[i])
				{
					if(in[i]-out[i]==1) In++;
					else if(out[i]-in[i]==1) Out++;
					else flag1=0;
				}
			}
			if(root>1) { flag=0; break; }
		}
        if((flag && In==0 && Out==0 && flag1) || (flag && In==1 && Out==1 && flag1))  
            printf("Ordering is possible.\n");  
        else  
            printf("The door cannot be opened.\n");  
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值