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.
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.
题目大意:给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思。但是如果有多个重复的单词时,也必须满足这样的条件才能算YES。否则都是不可能的情况。
解题思路: 欧拉路的基本题。只要知道就可以做出来了。
关于欧拉回路和欧拉路径 定义:
欧拉回路:每条边恰好只走一次,并能回到出发点的路径
欧拉路径:经过每一条边一次,但是不要求回到起始点
①首先看欧拉回路存在性的判定:
一、无向图 每个顶点的度数都是偶数,则存在欧拉回路。
二、有向图(所有边都是单向的) 每个节顶点的入度都等于出度,则存在欧拉回路。
三.混合图欧拉回路 (做这道题不用了解这一条)
②.欧拉路径存在性的判定
一。无向图 一个无向图存在欧拉路径,当且仅当 该图所有顶点的度数为偶数 或者 除了两个度数为奇数外其余的全是偶数。
二。有向图 一个有向图存在欧拉路径,当且仅当 该图所有顶点的度数为零 或者 一个顶点的度数为1,另一个度数为-1,其他顶点的度数为0。
所以这道题的大题思路就是:
1.并查集判断连通
2.将每个单词取出首字母和尾字母,转换为一条边,然后加入对应的连通分量中。如果这个字母出现过,visit数组标记为true。同时起点出度加1,终点入度加1.
3.判断一下:
1)这个图必须是连通的,即根结点只有一个。如果不是,直接结束本次算法。
2)如果这个图是连通的,判断每个结点的入度和出度情况。
如果这个图是欧拉路,则每个顶点的出度等于入度。即out[i] = in[i] 如果这个图是半欧拉图,则起点的出度比入度大1,终点的入度比出度大1.其余顶点的出度等于入度。 如果满足上述条件,就可以将所有单词链接起来,否则不能。 当然,在判断出度入度的时候还有一点需要注意,那就是除了起点终点以外的顶点,出度必须等于入度(出度入度可以同时为2,即环),但是起点和终点必须保证出度和入度之差为1。
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#define MAX 26
using namespace std;
int out[MAX],in[MAX],flag[MAX],p[MAX],pre[MAX];
int Find(int x)
{
int r=x;
while (pre[r]!=r)
r=pre[r];
return r;
}
void join(int x,int y)
{
int fx,fy;
fx=Find(x);
fy=Find(y);
if(fx!=fy)
pre[fx]=fy;
}
int main ()
{
int n,i,a,b,k,num,t;
char str[1002];
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for(i=0;i<26;i++)//初始化26个字母
pre[i]=i;
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(flag,0,sizeof(flag));
for(i=1;i<=n;i++)
{
cin>>str;
a=str[0]-'a'; //用0,1,2...26分别表示26个英文字母,a表示各个单词首字母
b=str[strlen(str)-1]-'a';//b表示每个单词的尾字母
join(a,b); //将a,b捆绑
in[b]++; //入度加1
out[a]++; //出度加1
flag[a]=flag[b]=1; //标记该字母已存在
}
num=0;
for(i=0;i<26;i++)
{
pre[i]=Find(i);
if(flag[i]==1 && pre[i]==i) //如果不同源,则非同根
num++; //看看连起来了几个串
}
if(num>1) //num大于1 表示连不起来1个
{
cout<<"The door cannot be opened."<<endl;
continue;
}
k=0;
for(i=0;i<26;i++)
{
if(flag[i]==1 && in[i]!=out[i]) //计算存在字母的入度及出度不相等的节点数
p[k++]=i;
}
if(k==0) //不存在出度不等于入度的节点,是欧拉回路
{
cout<<"Ordering is possible."<<endl;
continue;
}
if(k==2 && ((out[p[0]]-in[p[0]]==1 && in[p[1]]-out[p[1]]==1) //入度与出度不相等的节点个数为2
|| (out[p[1]]-in[p[1]]==1 && in[p[0]]-out[p[0]]==1)) ) //且起点出度减入度等于1或者终点入度减出度等于1,则是欧拉回路
{
cout<<"Ordering is possible."<<endl;
continue;
}
else cout<<"The door cannot be opened."<<endl;
}
return 0;
}