poj 1386 欧拉路径


 1 /*
2 题意:给出N个单词,一个单词的头字母和另一个单词的尾字母相同则可以相连,问这N个单词是否能完全相连成一行
3
4 题解:求欧拉路径
5 首先以每个单词的首字母和尾字母为点并且连边,然后用DFS求该图是否连通,然后根据点的入度和出度判断是否存在
6 欧拉路径或者欧拉回路(存在回路也是符合要求的)
7 */
8 #include <cstdio>
9 #include <cstring>
10
11 int map[30][30];
12 int in[30],out[30];
13
14 bool exist[30],vis[30];
15
16 void dfs(int u)
17 {
18 vis[u] = true;
19 for(int i=0; i<26; i++)
20 if (map[u][i] && exist[i] && !vis[i])
21 dfs(i);
22 }
23
24 int main(void)
25 {
26 int t;
27 scanf("%d",&t);
28 char str[1005];
29 while (t--)
30 {
31 int n;
32 scanf("%d",&n);
33 memset(map,0,sizeof(map));
34 memset(in,0,sizeof(in));
35 memset(out,0,sizeof(out));
36 memset(exist,false,sizeof(exist));
37
38 for(int i=0; i<n; i++)
39 {
40 scanf("%s",str);
41 int len = strlen(str);
42 int u = str[0]-‘a‘, v = str[len-1]-‘a‘;
43 map[u][v]++;
44 in[v] ++;
45 out[u] ++;
46 exist[u] = exist[v] = true;
47 }
48
49
50 // 找出合适的起点进行DFS,入度小于出度的肯定是起点,要么就任意起点
51 memset(vis,false,sizeof(vis));
52 int i;
53 for(i=0; i<26; i++)
54 if (exist[i])
55 break;
56 for(int j=i+1; j<26; j++)
57 if (exist[j] && in[j] < out[j])
58 {
59 i = j;
60 break;
61 }
62 dfs(i);
63 for(i=0; i<26; i++) // 连通则所有点都遍历了
64 if (exist[i] && !vis[i])
65 break;
66
67 bool flag = false;
68 if (i >= 26)
69 {
70 int head = 0,tail = 0,mid = 0, sum = 0;
71 for(int i=0; i<26; i++)
72 {
73 if (exist[i])
74 {
75 sum++;
76 if (in[i] == out[i])
77 mid++;
78 if (in[i] - out[i] == -1)
79 head++;
80 if (in[i] - out[i] == 1)
81 tail++;
82 }
83 }
84 if (sum == mid || (head == 1 && tail == 1 && mid == sum-2)) // 判断欧拉路径或回路
85 flag = true;
86 }
87
88 if (flag)
89 printf("Ordering is possible.\n");
90 else
91 printf("The door cannot be opened.\n");
92 }
93 return 0;
94 }

poj 1386 欧拉路径

时间: 2024-08-24 00:13:53

poj 1386 欧拉路径的相关文章

poj 1386 Play on Words 有向图欧拉路径判断

题意: 给n个单词,问是否可以将他们排成一排,使得前一个单词的末字符和后一个单词的首字符相同. 分析: 把每个单词看成一条边,转化为有向图是否存在欧拉路径问题. 代码: //poj 1386 //sep9 #include <iostream> #include <vector> #include <algorithm> #include <map> #include <string> using namespace std; const int

POJ 1386 Play on Words(欧拉图的判断)

Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11838   Accepted: 4048 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 ther

poj 1386 Play on Words(有向图欧拉回路)

1 /* 2 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 3 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 4 5 再次强调有向图欧拉路或欧拉回路的判定方法: 6 (1)有向图G为欧拉图(存在欧拉回路),当且仅当G的基图连通,且所有顶点的入度等于出度. 7 (2)有向图G为半欧拉图(存在欧拉道路),当且仅当G的基图连通,且存在顶点u的入度比出度大1.v的入度比出度小1, 8 其它所有顶点的入度等于出度(顶点u,v的个数必须都是

POJ 1386 Play on Words

欧拉回路问题. 题意是说给你一些字符串,类似于成语接龙,上一个字符串尾字母必须和下一个字符串首字母相同. 把所有字符串连成一串. 根据定理判断欧拉通路,然后DFS判连通(并查集也可) 没注意题意 字符串开了str[100] 结果RE.结果字符串最长有1000.改了就AC了. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm>

HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有首字母指向尾字母的有向边,每个字母看成一个点,题中要求等效于判断图中是否存在一条路径经过每一条一次且仅一次,就是有向欧拉通路.统计个顶点的出入度,如果每个点的出入度都相同,那就是欧拉回路,如果有两个奇数度,那就是欧拉通路,除此之外,都不能满足要求.还有别忘了判断是否连通,此时用到并查集,图中所有的边

HDU 1116 &amp;&amp; POJ 1386 Play on Words(欧拉路径)

按字母来建边,每个单词的首字母和尾字母加边.先判断是否连通,然后判断每个字母的入度和出度不能出现差的绝对值大于2,然后入度和出度差的绝对值为1的不能超过两个.就可以形成欧拉路径 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include

POJ 1386 Play on Words(欧拉路)

http://poj.org/problem?id=1386 题意: 给出多个单词,只有单词首字母与上一个单子的末尾字母相同时可以连接,判断所有字母是否可以全部连接在一起. 思路: 判断是否存在欧拉道路,每个单词只需要处理首字母和尾字母就可以了. 还有需要注意的就是需要判断图是否连通,我在这里用了并查集来判断. 有向图D存在欧拉道路的充要条件是:D为有向图,D的基图连通,并且所有顶点的出度与入度都相等:或者除两个顶点外,其余顶点的出度与入度都相等,而这两个顶点中一个顶点的出度与入度之差为1,另一

poj 1386 Play on Words(有向图欧拉路+并查集)

题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指向尾字母的有向边, 则则所有的单词构成了一个有向图,问题变为判断该有向图中是否存在一条欧拉路:有向图中存在欧拉路的两个充分必要条件为:该图是联通的并且所有的点的入度等于出度或者只存在两个点的入度与出度不等,一个点的入度=出度+1,另一个点的入度=出度-1: 代码如下: #include <cstdi

[欧拉回路] poj 1386 Play on Words

题目链接: http://poj.org/problem?id=1386 Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9685   Accepted: 3344 Description Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve