欧拉回路
来源:互联网 发布:烈火软件官网 编辑:程序博客网 时间:2024/06/05 14:39
问题描述:给定一个无向图,是否存在这样一条路线:从无向图任意一个结点出发,遍历图的每条边恰好一次。若存在,则称这条路线为欧拉道路(因为它是由大数学家欧拉首先提出并给出完美的解答)。
解决思路: 在欧拉道路中,除起点和终点外,其他节点的进度和出度是相等的,即其他节点的度数为偶数。我们把一个节点度数为奇数的节点称为奇点。而在欧拉道路中,只可能存在0个或2个奇点,不可能存在1个奇点。
奇点数为2:
奇点数为0,也称欧拉回路:
推广:
有向图中的欧拉道路:在忽略边的方向后,有向图必须是连通的。
存在欧拉道路的充分条件:最多只能有两个点的入度不等于出度,而且,起点的出度恰好比其的入度大1,终点的入度比其出度恰好大1.
寻找欧拉道路或欧拉回路的算法,逆序打印路径,引用自刘汝佳《算法竞赛入门经典》第六章:
void eluer(int u){ for (int v=0;v<n;v++ ){ if(grid[u][v] && visited[u][v]){ visited[u][v] = visited[v][u] = 1; // 有向图中改为 visited[u][v]=1 即可 eluer(v); printf("%d %d\n", u, v); } }}
如何判断有向图和无向图是否存在欧拉道路和欧拉回路?
根据图的连通性和度数。图的连通性判断可用并查集和DFS判断。
例题 Play on Words, Uva 10129
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1070
类似于成语接龙:
输入n个单词,是否可以把所有这些单词排成一个序列,使得每个单词的第一个字母和上一个单词的最后一个字母相同,如 acm->malform->mouse.
// 题意:输入n个单词,是否可以排成一个序列,使得每个单词的第一个字母和上一个单词的最后一个字母相同// 算法:把字母看作结点,单词看成有向边,则有解当且仅当图中有欧拉路径。注意要先判连通#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <map>#include <queue>#include <set>#include <string>#include <vector>using namespace std;#define LL long longconst int maxn = 1000 + 5;int parent[256];// 并查集int findset(int x) { // 更新parent[x] 的值 return parent[x] != x ? parent[x] = findset(parent[x]) : x;}int used[256], degrees[256]; // 是否出现过:度数int main() {// freopen("/Users/yuxiao/XcodeProject/graph/graph/in", "r", stdin);// freopen("/Users/yuxiao/XcodeProject/graph/graph/out", "w", stdout); int T, t; cin >> T; t = 1; while (T--) { int n; char word[maxn]; cin >> n; memset(used, 0, sizeof(used)); memset(degrees, 0, sizeof(degrees)); for (int ch = 'a'; ch <= 'z'; ch++) { parent[ch] = ch; //初始化并查集 } int connected = 26; //连通块的个数 for (int i = 0; i < n; i++) { scanf("%s", word); char head_letter = word[0]; char tail_letter = word[strlen(word) - 1]; degrees[head_letter]++; //相关字母的入度+1 degrees[tail_letter]--; //相关字母的出度-1 used[head_letter] = used[tail_letter] = 1; int root1 = findset(head_letter); int root2 = findset(tail_letter); if (root1 != root2) { parent[root1] = root2; //合并 connected--; // 由于并查集实现了合并,因此连通块减一 } } vector<int> res; for (int ch = 'a'; ch <= 'z'; ch++) { if (!used[ch]) { connected--; //没出现过的字母 } else if (degrees[ch] != 0) { res.push_back(degrees[ch]); } } bool ok = false; // 前提条件:有向图去掉箭头的指向后是连通的,即connected ==1 // res 最后剩下的点集要么为空,要么只有两个点,一个度数为1,另一个为-1 if (connected == 1 && (res.empty() || (res.size() == 2 && (res[0] == 1 || res[0] == -1)))) { ok = true; } if (ok) { printf("Ordering is possible.\n"); } else { printf("The door cannot be opened.\n"); } } return 0;}
0 0
- HDU1878 欧拉回路【欧拉回路】
- 欧拉回路判断
- 欧拉回路 hdu1878
- 欧拉回路
- 欧拉回路
- 欧拉回路
- 欧拉回路,欧拉路
- 欧拉回路
- 欧拉回路
- 欧拉回路
- 欧拉回路
- 欧拉回路
- 欧拉回路
- 欧拉回路
- 图论 欧拉回路
- zju2008欧拉回路
- 欧拉回路
- hdu1878欧拉回路
- 在linux下如何进行mysql命令行 创建数据库
- Spring和Mybatis整合
- Shell运算符:Shell算数运算符、关系运算符、布尔运算符、字符串运算符等
- List、Set 区别
- 不用中间变量交换 a ,b
- 欧拉回路
- 控制台输入日期格式存储到数据库
- 关于css浮动的一些理解
- Strtus2的基本使用
- Shell注释
- Java类的初始化、变量的初始化
- Hive永久函数
- 轻松入门React和Webpack
- 第五周项目4-长方柱类