hdu1116 欧拉回路+并查集

Posted lyj1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;

  

以上是关于hdu1116 欧拉回路+并查集的主要内容,如果未能解决你的问题,请参考以下文章

hdu--1878--欧拉回路(并查集判断连通,欧拉回路模板题)

hdu 1878 欧拉回路+并查集

hdu1878-并查集,欧拉回路

hdu1878 欧拉回路 并查集

hdu 3018 Ant Trip 欧拉回路+并查集

Play on Words HDU - 1116(欧拉路判断 + 并查集)