B - Play 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 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.
输入:
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.
输出:
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.”.
样例:
输入:
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
输出:
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
————————————————————————————————————————
题意:
给你一个n,接下来给你n个英文单词,现在有一种方法连接这些单词,就是当前单词的尾字母等于下一个单词的首字母。请你判断是否可以把所有的单词按这种方法连成一个欧拉通路。
思路:
首先要知道的是,欧拉通路中只有两个点是度数为奇数,其余点度数都是入度等于出度;并且这两个点一个出度比入度大一,另一个入度比出度大一。
接下来就是利用并查集合并欧拉图,然后判断只能有一个欧拉图,接下来判断头和尾的个数并且只能有一个或没有。
————————————————————————————————————————
代码:
//欧拉通路,起点可能出度比入度大一,终点入度比出度大一,
//其余顶点出度等于入度。
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<string.h>
#include<algorithm>
#define inf 99999999
using namespace std;
const int maxx=1010;
int f[maxx],book[maxx],tou[maxx],wei[maxx];//头,尾
char e[maxx];
int n;
int getf(int v)
{
if(f[v]==v)
return v;
else
{
f[v]=getf(f[v]);
return f[v];
}
}
void merge(int a,int b)
{
int t1,t2;
t1=getf(a);
t2=getf(b);
if(t1!=t2)
{
f[t2]=t1;
}
}
int main()
{
int i,j,z,t,a,b,l;
int s1,s2,s3,f1,f2;
cin>>t;
while(t--)
{
memset(book,0,sizeof(book));
memset(tou,0,sizeof(tou));
memset(wei,0,sizeof(wei));
cin>>n;
for(i=1;i<=26;i++)
{
f[i]=i;
}
for(i=1;i<=n;i++)
{
cin>>e;
a=e[0]-'a'+1;//头
l=strlen(e);
b=e[l-1]-'a'+1;//尾
book[a]=1;book[b]=1;//表示该字母在头或尾出现过
tou[a]++;wei[b]++;//记录出现次数
merge(a,b);
}
s1=s2=s3=0;f1=f2=1;
for(i=1;i<=26;i++)
{
if(book[i]==1)//出现过
{
if(f[i]==i)
{
s1++;//有几个欧拉图
}
if(tou[i]!=wei[i])//度数是否入度等于出度
{
if(tou[i]-wei[i]==1)
s2++;//头
else if(wei[i]-tou[i]==1)
s3++;//尾
else
f1=0;
}
}
if(s1>1||f1==0)//只能有一个欧拉图
{
f2=0;
break;
}
}
if(f2&&f1)
{
if((s2==0&&s3==0)||(s2==1&&s3==1))
cout<<"Ordering is possible."<<endl;
else
cout<<"The door cannot be opened."<<endl;
}
else
cout<<"The door cannot be opened."<<endl;
}
return 0;
}