Uva10129 - Play on Words 欧拉通路 DFS

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=1070

 

题目类型: 欧拉道路

题目:

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.

 

题目大意翻译:

有一些秘密的门包含着非常有趣的单词迷题, 考古学家队伍必须解决它们才能够打开大门。 因为没有其他方法能偶打开这些门, 所以解决那些迷题对我们非常重要。

在每个门上有很多个有磁力的盘子,盘子上面写着单词。 必须重新移动放置这些盘子,让它们形成一个队列:队列中,除了第一个单词,每个单词的开头和上一个单词的结尾字母

一样。例如, motorola的后面可以接上acm。 

你的任务是写一个程序, 读入一系列单词,然后计算确定它们是否有可能被排成这样的队列。

样例输入:

3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok

样例输出:

The door cannot be opened.
Ordering is possible.
The door cannot be opened.

 

解题思路:

  把字母看作结点,单词看成有向边,则当且仅当图中有欧拉通路时问题有解。

  欧拉通路的两个条件:①基图(忽略边的方向后得到的无向图)联通

            ②最多只能有两个点的入度不等于出度,而且必须是其中一个点的出度恰好比入度大1,另一个入度比出度大1

AC代码如下

 1 #include <iostream>
 2 #include <cstring>
 3 #define maxn 100
 4 using namespace std;
 5 int kase;
 6 int n;
 7 int G[maxn][maxn];
 8 int vis[maxn];
 9 int in[maxn],out[maxn];
10  
11 void dfs(int u)
12 {
13     vis[u] = true;
14     for(int i = 0; i < maxn; ++i)
15         if(G[u][i] && !vis[i])
16             dfs(i);
17 }
18  
19 int main()
20 {
21     cin >> kase;
22     while(kase--)
23     {
24         memset(G,0,sizeof(G));
25         memset(vis,0,sizeof(vis));
26         memset(in,0,sizeof(in));
27         memset(out,0,sizeof(out));
28         cin >> n;
29         char str[1001];
30         for(int i = 0; i < n; ++i)
31         {
32             cin >> str;
33             int a = str[0] - 'a';
34             int b = str[strlen(str)-1]-'a';
35             //注意这里转化为无向图 
36             ++G[a][b];
37             ++G[b][a];
38             ++out[a];
39             ++in[b];
40         }
41         
42         //若存在欧拉通路,最多只能有两个点的入度不等于出度
43         //而且必须是其中一个点的出度恰好比入度大1 
44         //另一个的入度比出度大1
45         bool flag = true;
46         int num1 = 0,num2 = 0;
47         for(int i = 0;i < maxn; ++i)
48         {
49             if(in[i] != out[i])
50             {
51                 if(in[i] == out[i] + 1)        num1++;
52                 else if(out[i] == in[i] + 1)     num2++;
53                 else{ flag = false; break;    }
54             }
55         }
56         if( num1 && num2 && num1 + num2 > 2)    flag = false;
57         if(flag)
58         {
59             for(int i = 0; i < maxn; ++i)  //转化成无向图,dfs判断是否联通 
60                 if(out[i])    { dfs(i); break; }
61             bool flag2 = true;
62             for(int i = 0; i < maxn; ++i)
63                 if((in[i] || out[i]) && !vis[i])
64                 {
65                     flag2 = false;
66                     break;
67                 }
68             if(flag2)
69                 cout << "Ordering is possible." <<endl;
70             else
71                 cout << "The door cannot be opened." << endl;
72         }
73         else
74             cout << "The door cannot be opened." << endl;
75         
76     }
77     return 0;
78 }

 

posted @ 2017-11-29 11:14  SeanLiao  阅读(253)  评论(0编辑  收藏  举报