您的位置:首页 > 其它

uva 10129 Play on Words

2016-01-05 12:34 323 查看
原题:

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 N that indicates the number of

plates (1 ≤ N ≤ 100000). Then exactly N lines 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.

题目大意:

先给你一个数字t表示有多少个样例,然后给你一个数字n,让你输入n个单词。然后问题,如果这些单词首尾字母相连,能否组成一个通路。相同的单词可能会重复出现,每个单词至少两个字母。

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iomanip>
#include<set>
#include<fstream>
using namespace std;
fstream output,input;

int father[27];//根节点
int vis[27];//该字母是否使用过
int io[27];//记录每个字母的出度入度
string s;
int Find(int x)
{
if(x!=father[x])
{
father[x]=Find(father[x]);
}
return father[x];
}
set<int> ans;//用来判断根节点的数目

void ini()
{
memset(vis,0,sizeof(vis));
memset(io,0,sizeof(io));
for(int i=1;i<=26;i++)
father[i]=i;
ans.clear();
}
int main()
{
ios::sync_with_stdio(false);
int t,n,flag;
int a,b,x,y;
cin>>t;
while(t--)
{
cin>>n;
flag=0;
ini();
for(int i=0;i<n;i++)
{
cin>>s;
a=s[0]-'a'+1;
b=s[s.size()-1]-'a'+1;
vis[a]=1;
vis=1;
io[a]++;
io[b]--;
x=Find(a);
y=Find(b);
father[y]=x;
}
if(n==1)
{
cout<<"Ordering is possible."<<endl;
continue;
}
for(int i=1;i<=26;i++)//所有节点再次寻找根
if(vis[i])
Find(i);
x=a=b=0;
for(int i=1;i<=26;i++)
{
if(vis[i])
ans.insert(father[i]);
if(io[i]!=0)//出度和入度不相等的节点
x++;
if(io[i]==-1)
a++;//出度
if(io[i]==1)
b++;//入度
}
//      cout<<ans.size()<<" "<<x<<" "<<a<<" "<<b<<endl;
if((ans.size()==1&&x==2&&a==1&&b==1)||(ans.size()==1&&x==0&&a==0&&b==0))
//如果根节点只有一个,且出度和入度不相等的节点只有两个
//或者根节点只有一个,但是连成一个环
cout<<"Ordering is possible."<<endl;
else
cout<<"The door cannot be opened."<<endl;

}
//  input.close();
//  output.close();
return 0;
}


[b]思路:


随便在老刘的白书上找了个图论的题~~~

上来一看就知道这题是欧拉回路,刚想写搜索。一看数据里面有100000个单词,不能贸然行事。因为给的是单词,总共就26个字母,所以从这里着手。把所有出现的单词的首尾字母看成是一个节点的出度和入度。

首先如果能形成一个通路,根据欧拉通路的定义且能连成一条线,经过所有相同字母出度入度相抵消后,必定是只有一个字母有一个出度和一个字母有一个入度,但是得考虑到这个图不是连通的情况。

例如如下数据:

ab

ef

fe

这组数据里面出度和入度的条件满足上述条件,e和f字母分别出现两次,一次e当做入度f当出度,另外一次e当出度f当入度,相互抵消后剩下ab,a当入度,b当出度。正好满足上述条件,不过ab不和e与f连通,所以要判断连通性!这里用并查集来实现,father[i]用来存储所有入度字母当成的根节点,比如例子中的ab就是father[b]=a;相当于让b连接到a上,并把a和b分别用vis[i]标记,表示26个字母里面出现过ab这两个字母,没出现过的字母当然就不要去判断。如果最后能形成一条通路,那必然是所有在vis[i]的条件下father[i]里面存的都是一个值!否则就相当于有多个起点。这里再啰嗦两句,这里被连接上的节点只是挂在被连接的节点上面,不是直接挂在根节点上。所以要再走一次Find函数,用来路径压缩,使每个节点找到的根节点都是最终的根节点。

此外,还要考虑成环的条件,比如如下数据。

hk

kh

根节点同样只有一个,但是图的总出度和总入度相互抵消以后都变成0,所以此条件也要判定。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: