欧拉回路以及欧拉路径
来源:互联网 发布:cf宏鼠标一键瞬狙数据 编辑:程序博客网 时间:2024/04/28 12:43
欧拉回路是指在一个图中存在一个回路使得从一点出发又回到该点,并且不重复地走完图中所有的边。
解决方法:
1,运用并查集判断所有点是否构成一个图
2,判断点的度(如果是有向图,所有点的入度等于出度,如果是无向图,所有点的度都为偶数)
原理:如果要实现欧拉回路,那么对于有向图来说,通过一条边进入某点,肯定存在一条没走过边离开该点,所以入度自然等于出度,对于无向图,每通过一条边进入某点也必然存在一条没走过的边出去,因为无向图的边是完全相同的,所以每个点的度肯定是偶数。
欧拉路径是指在一个图中存在一条路径使得从一点出发到达另一点,并且不重复地走完图中所有的边。
解决方法:
1,同样使用并查集判断所有点是否构成一个图
2,判断点的度(如果是有向图,只有一个点出度比入度大1,也只有一个点入度比出度大1,其余的点入度等于出度,如果是无向图,只有两个点的度是奇数,其余均为偶数)
原理:如果要实现欧拉路径,只存在一个起点和一个终点,对于起点,出去的边比进来的边多1,终点进来的边比出去的边多1,对于有向图来说,起点就是出度比入度大1的点,而终点就是入度比出度大1的点,对于无向图,起点和终点的度为基数
理解了欧拉路径后就不难发现,终点和起点是成对出现的,那么通过图中没点的度就可以判断该图最少可以分成几个欧拉路径的组合,例如:无向图中存在4个奇数度的点,那么最少分成两个欧拉路径(思考一下,无论怎样都不会出现奇数个奇数度的点);有向图中出度比入度大1的点有4个,那么最少分成4个欧拉路径(入度比出度大1的点也一定是4个)
在欧拉路径题目中,有时会包含欧拉回路的情况,要想清楚。
在欧拉路径,欧拉回路的题目中还要注意以什么为边,以什么为点
上一道题:
Description
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
Output
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
32acmibm3acmmalformmouse2okok
Sample Output
The door cannot be opened.Ordering is possible.The door cannot be opened.
具体代码:
#include<stdio.h>#include<string.h>int father[26],r[26];int inde[26],outde[26];//记录出入度char s[1005];void make_set(){ memset(inde,0,sizeof(inde)); memset(outde,0,sizeof(outde)); for(int i=0;i<26;i++) { father[i]=i; r[i]=1; }}int find_set(int a){ if(a!=father[a]) father[a]=find_set(father[a]); return father[a];}void Union(int a,int b){ a=find_set(a); b=find_set(b); if(a==b) return ; if(r[a]>r[b]) { father[b]=a; r[a]+=r[b]; } else { father[a]=b; r[b]+=r[a]; } return ;}int f;int main(){ int n,t; scanf("%d",&t); while(t--) { make_set(); scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s",s); inde[s[0]-'a']++; outde[s[strlen(s)-1]-'a']++; Union((int)(s[0]-'a'),(int)(s[strlen(s)-1]-'a'));//建立并查集 } int in=0,out=0; bool flag=true; int count=0; for(int i=0;i<26;i++)//判断是否在同一集合 { if(count==0&&(inde[i]||outde[i])) { f=find_set(i); count=1; } if(count!=0&&(inde[i]||outde[i])&&find_set(i)!=f) { flag=false; break; } } if(!flag) { printf("The door cannot be opened.\n"); continue; } for(int i=0;i<26;i++)//出入度判断 { if(inde[i]-outde[i]==1) in++; else if(outde[i]-inde[i]==1) out++; else if(inde[i]-outde[i]>1||outde[i]-inde[i]>1) { flag=false; break; } if(in>1||out>1) { flag=false; break; } } if(flag) printf("Ordering is possible.\n"); else printf("The door cannot be opened.\n"); } return 0;}
- 欧拉回路以及欧拉路径
- 欧拉路径/回路
- 欧拉路径&&欧拉回路
- 欧拉回路、欧拉路径
- 欧拉路径与欧拉回路
- hdu5348(欧拉回路+欧拉路径)
- 欧拉路径/欧拉回路
- 欧拉回路(欧拉路径)
- 欧拉路径-欧拉回路
- 欧拉回路(欧拉路径)
- 欧拉路径-欧拉回路
- 欧拉回路路径求解
- 欧拉回路+路径 POJ
- 欧拉回路/路径【总结】
- 欧拉回路--输出欧拉回路的路径
- POJ 2337 欧拉回路+欧拉路径+判断欧拉回路和路径
- 欧拉路径, 欧拉回路 USACO Riding the Fences
- 欧拉路径,欧拉回路,并查集
- 康托展开/逆康托展开
- Android CursorLoader实例详解(附源码)
- sgu-163-Wise King (水到不忍直视)
- 九度OJ—题目1525:子串逆序打印
- 在js中if条件为null/undefined/0/NaN/""表达式时,统统被解释为false,此外均为true
- 欧拉回路以及欧拉路径
- 老罗Android开发 视频教程
- iOS字面量语法
- Django Step1 项目创建+连接MySQL+建表
- poj 2408 Anagram Groups(hash)
- SQL Server 服务由于登录失败而无法启动
- 王垠的过去和现状
- 《c程序设计语言》读书笔记-删除字符串中匹配的字符
- Android studio no jvm installation found错误