Uva10129(欧拉回路)

Posted zuiaimiusi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Uva10129(欧拉回路)相关的知识,希望对你有一定的参考价值。

Play on Words UVA - 10129

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 ?rst line of the input ?le. 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 ?rst 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.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 const int maxn=26;
 5 using namespace std;
 6 /*
 7 欧拉回路
 8 图是否连通
 9 是否存在联通块1.并查集2.dfs
10 判断入度出度
11 */
12 int G[maxn][maxn],vis[maxn][maxn];
13 int n,m,in[maxn],out[maxn];
14 bool dfs(int u)
15 
16     bool findd=false;//是否找到连通块
17     for(int v=0;v<maxn;v++)
18     
19         if(G[u][v]&&!vis[u][v])//有边且没有被访问
20         
21             vis[u][v]=vis[v][u]=1;
22             findd=true;
23             dfs(v);
24         
25     
26     return findd;
27 
28 bool communication()
29 
30     bool ok=false;//是否有连通块
31     for(int i=0;i<maxn;i++)
32     
33         if(dfs(i))
34             if(ok)
35             return false;//如果已有连通块,则图不连通
36             ok=true;
37             
38     
39     return true;
40 
41 bool check()
42 
43     bool innode=true;
44     bool outnode=true;
45     for(int i=0;i<maxn;i++)
46     
47         if(in[i]==out[i])continue;//入度==出度
48         else if(in[i]-1==out[i]&&innode)//存在入度比出度大一,最多只能有一个这样的点
49             innode=false;
50             continue;
51         
52         else if(out[i]-1==in[i]&&outnode)
53             outnode=false;
54             continue;
55         
56         else
57             return false;
58     
59     if(innode!=outnode)//两个点一个存在一个不存在,则不能构成欧拉回路
60         return false;
61     return true;
62 
63 int main()
64 
65     scanf("%d",&n);
66     while(n--)
67     
68         scanf("%d",&m);
69         memset(vis,0,sizeof vis);
70         memset(G,0,sizeof G);
71         memset(in,0,sizeof in);
72         memset(out,0,sizeof out);
73         bool flag=true;
74         while(m--)
75         
76             string a;
77             cin>>a;
78             int beginn=a[0]-a;
79             int endd=a[a.length()-1]-a;
80             in[beginn]++;
81             out[endd]++;
82             G[beginn][endd]=G[endd][beginn]=1;
83         
84             if(communication())
85                 
86                     if(!check())
87                         flag=false;
88                 
89                 else flag=false;
90         if(flag)
91             printf("Ordering is possible.\n");
92         else printf("The door cannot be opened.\n");
93     
94     return 0;
95 

欧拉回路:该回路遍历了一个图中所有的边,并且每条边只遍历一次。(一笔画)

欧拉路径:从起点开始到终点,遍历了图中所有的边,并且每条边只遍历一次。

度数:一个点连接了几条边。

入度和出度分别指:进入该点的边的数量,走出该点的边的数量。

连通无向图存在欧拉回路的充要条件:所有点的度数都为偶数。

连通无向图存在欧拉路径的充要条件:仅存在两个度数为奇数的点,其他点的度数都为偶数。(这两个度数为奇数的点,一个为奇数,一个为偶数)

连通有向图存在欧拉回路的充要条件:对于所有的点,入度等于出度。

连通有向图存在欧拉路径的充要条件: 仅存在两个点,其中一个点的入度比出度大一,另一个店的出度比入度大一。(出度大的为起点,入度大的为终点)

根据连通性和度数可判断出无向图和有向图是否存在欧拉回路和欧拉路径,可用 dfs 构造欧拉回路和欧拉路径。 

 

以上是关于Uva10129(欧拉回路)的主要内容,如果未能解决你的问题,请参考以下文章

Play on Words UVA - 10129 欧拉路径

UVA10129:Play on Words(欧拉回路)

6_16 单词(UVa10129)<欧拉回路>

紫书Play on Words UVA - 10129 欧拉回路

UVA 10129 Play on Words(欧拉道路)

uva-10596-欧拉回路