UVa 10129 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.

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.

题意

给你n个字符串,判断是否能接成一串(上一串的尾==下一串的头)

题解

判断有向图是否成欧拉路径需要2个条件

1.当前图连通(这里可以用并查集,可以用dfs)

2.最多只能有2个点入度In!=出度Out,而且必须是其中1个点出度=入度+1(起点),另1个点入度=出度+1才行(终点)

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int F[26];
 4 int Find(int x)
 5 {
 6     return F[x]==x?x:F[x]=Find(F[x]);
 7 }
 8 void Join(int x,int y)
 9 {
10     if(F[x]==-1)F[x]=x;
11     if(F[y]==-1)F[y]=y;
12     int fx=Find(x);
13     int fy=Find(y);
14     if(fx!=fy)
15         F[fx]=fy;
16 }
17 int main()
18 {
19     int n,T;
20     char s[1005];
21     scanf("%d",&T);
22     while(T--)
23     {
24         int In[26]={0},Out[26]={0};
25         memset(F,-1,sizeof(F));
26         scanf("%d",&n);
27         for(int i=0;i<n;i++)
28         {
29             scanf("%s",s);
30             int l=strlen(s);
31             int a=s[0]-'a',b=s[l-1]-'a';
32             In[a]++;Out[b]++;
33             Join(a,b);
34         }
35         int flag=1,flag1=0,flag2=0,flag3=0;
36         for(int i=0;i<26;i++)//图连通
37             if(F[i]!=-1&&F[i]==i)
38                 if(flag3==0)flag3=1;
39                 else flag=0;
40         for(int i=0;i<26;i++)
41         {
42             int h=In[i],t=Out[i];
43             if(h==t)//入度=出度
44                 continue;
45             else if(h-t==1)//入度=出度+1
46                 if(flag1==1)flag=0;
47                 else flag1=1;
48             else if(t-h==1)//出度=入度+1
49                 if(flag2==1)flag=0;
50                 else flag2=1;
51             else
52                 flag=0;
53         }
54         if(flag)printf("Ordering is possible.\n");
55         else printf("The door cannot be opened.\n");
56     }
57     return 0;
58 }

posted on 2018-02-26 00:18  大桃桃  阅读(180)  评论(0编辑  收藏  举报

导航