Play on Words
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 4
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.
Source
Central Europe 1999
字符串的首尾字符看成是图的一条边,运用并查集判断是否连通,但是还不够,比如像ok,ok这种情况,于是需要用到欧拉路。
如果图G中的一个路径包括每个边恰好一次,则该路径称为欧拉路。欧拉路分为欧拉回路和欧拉通路;
如果一个回路是欧拉路径,则称为欧拉回路。
欧拉通路:满足从一点出发经过每一条边且只经过一次,能把所有的边都经过的路
如果是欧拉回路那么图中每个点的入读和出度都相等
如果是通路那么起始点的出度减入度为1, 终点处入度减出度为1。
#include<bits/stdc++.h>
using namespace std;
char a[1005];
int in[27],out[27],vis[27],parent[27];
void init()
{
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(vis,0,sizeof(vis));
for(int i=1;i<=26;i++) parent[i]=i;
}
int Find(int x)
{
while(x!=parent[x]) x=parent[x];
return x;
}
void mer(int x,int y)
{
int xx=Find(x);
int yy=Find(y);
if(xx!=yy) parent[xx]=yy;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
init();
while(n--)
{
scanf("%s",a);
int len=strlen(a);
int L=a[0]-'a'+1,R=a[len-1]-'a'+1;
mer(L,R);
in[L]++,out[R]++;
vis[L]=1,vis[R]=1;
}
int k=0,x=0,y=0,z=0;
for(int i=1;i<=26;i++)
if(vis[i]&&parent[i]==i) k++;
if(k>1)
{
printf("The door cannot be opened.\n");
continue;
}
for(int i=1;i<=26;i++)
{
if(!vis[i]||in[i]==out[i]) continue;
x++;
if(in[i]-out[i]==1) y++;
if(out[i]-in[i]==1) z++;
}
if(!x) printf("Ordering is possible.\n");
else if(x==2&&y==1&&z==1) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
}