Play on Words
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9676 | Accepted: 3341 |
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.
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.".
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.
建图规则:对每个单词首字母到尾字母连一条有向边,如果单词x1(a1->b1)的尾字母等于单次y1(b1->c1),可以发现a1经b1到达c1,因此若干的单词首尾相连,从首字符走到尾字符,中间经过若干字符,每个字符都是k次进,k次出,立马想到欧拉通路,注意一下特殊情况:最后一个单词的尾字符和第一个单词的首字符相同,那么就是欧拉回路了。
而有向图欧拉通路的充要条件:基图连通,并且除了两个顶点,一个入度-出度=1,另一个出度-入度=1,其余点入度=出度。
有向图欧拉回路的充要条件:基图连通,所有顶点的入度=出度。
然后用并查集维护一下连通性,开个vis数组表示字符是否在图中出现即可。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int ideg[30],odeg[30],fa[30],vis[30];
char s[1010];
void init(){
for(int i=0;i<26;i++)
fa[i]=i;
}
int findset(int x){
return fa[x]==x?x:(fa[x]=findset(fa[x]));
}
void unionset(int a,int b){
a=findset(a),b=findset(b);
fa[a]=b;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(ideg,0,sizeof ideg);
memset(odeg,0,sizeof odeg);
memset(vis,0,sizeof vis);
init();
for(int i=0;i<n;i++){
scanf("%s",s);
int len=strlen(s);
int st=s[0]-'a',e=s[len-1]-'a';
vis[st]=vis[e]=1;
odeg[st]++,ideg[e]++;
unionset(st,e);
}
bool flag=true;
int t;
for(int i=0;i<26;i++)
if(vis[i]) {t=findset(i);break;}
for(int i=1;i<26;i++)
if(vis[i]&&findset(i)!=t) {flag=false;break;}
int in=0,out=0;
for(int i=0;i<26;i++){
t=ideg[i]-odeg[i];
if(t==1) in++;
else if(t==-1) out++;
if(t>1||t<-1||in>1||out>1) {flag=false;break;}
}
if(flag) puts("Ordering is possible.");
else puts("The door cannot be opened.");
}
return 0;
}