欧拉回路或通路路径输出(Fleury算法)
2015-12-14 20:49
357 查看
在下面的代码中,首先判断是否存在回路或通路,如果存在则选择一个正确的顶点按照Fleury算法输出欧拉回路或通路
代码如下:
代码如下:
#include<stdio.h> #include<string.h> #define MAXN 200 using namespace std; int Edge[MAXN][MAXN];//邻接矩阵 int n;//顶点个数 struct stack{ int top,node[MAXN]; }s; void dfs(int x){ int i; s.top++; s.node[s.top]=x; for(i=0;i<n;i++){ if(Edge[i][x]){ Edge[i][x]=0;Edge[x][i]=0;//走过的边删除,保证每条边只走一次 dfs(i); break; } } } void Fleury(int x){ int i,b; s.top=0; s.node[s.top]=x;//入栈; while(s.top>=0){//栈不为空 b=0; for(i=0;i<n;i++){//b!=0则说明还可以扩展 if(Edge[s.node[s.top]][i]){ b=1;break; } } if(b==0){ printf("%d",s.node[s.top]+1);//因为输入时,顶点都减一了 s.top--;//出栈 } else{ s.top--;//出栈 dfs(s.node[s.top+1]); } } printf("\n"); } int main(){ int i,j; int m,s,t; int degree,num,star; memset(Edge,0,sizeof(Edge)); scanf("%d%d",&n,&m); for(i=0;i<m;i++){ scanf("%d%d",&s,&t); Edge[s-1][t-1]=1;Edge[t-1][s-1]=1; } num=0;star=0; for(i=0;i<n;i++){ degree=0; for(j=0;j<n;j++) degree+=Edge[i][j]; if(degree%2==1){ star=i;num++; } } if(num==0||num==2)Fleury(star); else printf("No Euler path\n"); return 0; }
相关文章推荐
- 二分查找
- 黑马程序员——Java语言的环境搭建
- Codeforces #218 (Div. 2) Fox Dividing Cheese
- 使用Hyper-V PowerShell
- quartz定时器任务调度实现
- There are no resources that can be added or removed from the server.
- 菜鸟总结(1)
- AQL Subset Compiler:手把手教你如何写一个完整的编译器
- Hadoop 2.x属性修改
- Redis操作命令
- Dynamically filled ListView in QML
- .gitignore不起作用的问题
- MyReport报表系统v1.2
- UI UIControl
- Cache简介
- MulticastSocket 多点广播
- ScrollView与ListView嵌套冲突问题
- iOS MJRefresh上拉加载更多
- leetcode Contains Duplicate II
- Windows下的PHP开发环境搭建——PHP线程安全与非线程安全、Apache版本选择,及详解五种运行模式。