HDU - 1116Play on Words(欧拉路径有向)

传送门
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 wordmotorola’’. 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.


题意:欧拉路径有向题:条件:所有顶点的入度和出度相等,或起点和终点的入度出度差值同时为1;
t组样例,每个样例n个字符串,要求所的字符串的首尾两个字母符合‘顶针’,连成一串,当然这个字符串的首尾字母可以不一样,问是否可以组成。

分析:先用两个数组分别记录下首尾字母,字符型数据-96转为int型,并标记上,然后统计入度与出度同时并查集走一遍。


AC代码:

#include<stdio.h>
#include<string.h>
char a[1010];
int f[3000],n;
int k1[3000],k2[3000],book[3000];
int getf(int u)
{
	if(f[u]!=u)
	f[u]=getf(f[u]);
	return f[u];
}
void merge(int x,int y)
{
	int t1=getf(x);
	int t2=getf(y);
	if(t1!=t2)
	f[t1]=t2;
	return ;
}
int init()
{
	for(int i=1;i<=26;i++)
	f[i]=i;
}
int main()
{
	int i,x1,x2,x,count,flag,t,ans;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		init();
		memset(book,0,sizeof(book));
		memset(k1,0,sizeof(k1));
		memset(k2,0,sizeof(k2));
		for(i=1;i<=n;i++)
		{
		scanf("%s",a);
		x1=a[0]-96;
		x2=a[strlen(a)-1]-96;
		book[x1]=1;
		book[x2]=1;
		merge(x1,x2);
		k1[x1]++;//入度 
		k2[x2]++;//出度 
		}
		for(i=1;i<=26;i++)//找到可连通的一个点。 
		if(book[i]) break;
		x=getf(i);//找路径顶点的最高级 
		for(i=i;i<=26;i++)
		{
			if(book[i]&&x!=getf(i))//找是否有单独的点 
			break;
		}
		if(i<26)
		{
			printf("The door cannot be opened.\n");
			continue;
		}
		count=0;
		ans=0;
	for(i=1;i<=26;i++)
		if(book[i])
		{
			if(k1[i]!=k2[i])
			ans++;
			if(k1[i]-k2[i]==1||k2[i]-k1[i]==1)
			count++;
		}
		if(!ans||(count==2&&ans==2))// 每个点都有相同的入度和出度或者起点终点同时差值为1 
		printf("Ordering is possible.\n");
		else printf("The door cannot be opened.\n");
	}
}
//如果字符出现在首尾,必须book标记,多次要从a到z遍历; 
/*
3 
4
ab 
bc 
ba
cb
关于组合的问题,ab可以和bc,ba组合;并查集发现是一个集合内的后,
就不再改变大哥,只需要统计入度和出度,只要数量的符合规律就一定满足 
*/
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值