hdu1116 欧拉回路+并查集

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.

 

最重要的是要判断是否是连通图!把字符串看成 a -> b 即 首字母 指向 尾字母

用并查集判断是否为连通图,然后看出入度是否符合欧拉定理

 

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int ind[maxn];  //每个字母的入度 
using namespace std;
int par[maxn];  //并查集的根节点   表示x的parent 
//int num[maxn];
int vis[maxn];
void init()
{
    for(int i=0;i<maxn;i++)
        par[i]=i,ind[i]=0,vis[i]=0;
	//,num[i]=i
}
 
int find(int x)
{
    if(par[x]!=x)
        par[x]=find(par[x]);
    return par[x];
}
 
void Union(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x!=y){
        par[y]=x;
        //num[y]=num[x];
    }
} 
int main(){
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		if(n==1){
			string zz;
			cin>>zz;
			puts("Ordering is possible.");
			continue;
		}
		init();
		for(int i=0;i<n;i++){
			string zz;
			cin>>zz;
			int a=zz[0]-'a';
			int b=zz[ zz.size()-1 ]-'a';
			//cout<<a<<" "<<b<<endl;
			Union(a,b);
			vis[a]=1;
			vis[b]=1;
			ind[a]++;
			ind[b]--;
		}
		int g=0;
		int cnt1=0,cnt0=0;
		for(int i=0;i<26;i++){
			if(vis[i]&& par[i]==i ){
				g++;      //g为1表示该图为连通   g大于1表示有多个分图! 
			}
			if(ind[i]==1)  cnt1++;
			else if( ind [i]%2==-1 ) cnt0++;
			else if( ind[i]!=0 ) {
				g=0;break;
			}
		}
		if(( (cnt1==1&&cnt0==1)||(cnt1==0&&cnt0==0)) && g==1 )  puts("Ordering is possible.");
		else puts("The door cannot be opened.");
	}


	return 0;
}

  

转载于:https://www.cnblogs.com/lyj1/p/11373703.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值