POJ 1041 - John's trip 输出欧拉回路路径边..通用做法
2013-08-28 23:52
363 查看
题意:
给一个无向图..告诉哪些点之间有边相连..并且告诉边的标号..请输出任意一种欧拉回路边遍历的方案...
题解:
题目描述有问题..说是要输出字典序最小.又SJ...所以实际上只要是方案就行了...
由于题目保证了是连通图..所以不需要判连通性...那么先用度来判断该无向图是否存在欧拉回路...若存在..则任意一条路径出俩...方法是从任意一个点开始dfs...抽象的看就是把每一个环给拓展出来...最后倒过来输出...
Program:
给一个无向图..告诉哪些点之间有边相连..并且告诉边的标号..请输出任意一种欧拉回路边遍历的方案...
题解:
题目描述有问题..说是要输出字典序最小.又SJ...所以实际上只要是方案就行了...
由于题目保证了是连通图..所以不需要判连通性...那么先用度来判断该无向图是否存在欧拉回路...若存在..则任意一条路径出俩...方法是从任意一个点开始dfs...抽象的看就是把每一个环给拓展出来...最后倒过来输出...
Program:
#include<iostream> #include<stdio.h> #include<string.h> #include<cmath> #include<queue> #include<stack> #include<set> #include<time.h> #include<map> #include<algorithm> #define ll long long #define eps 1e-5 #define oo 1000000007 #define pi acos(-1.0) #define MAXN 55 #define MAXM 2005 using namespace std; int s[MAXN][MAXM],d[MAXN],ans[MAXM],m,num; bool used[MAXM]; bool legal(int n) { for (int i=1;i<=n;i++) if (d[i]%2) return false; return true; } void dfs(int x) { for (int t=1;t<=m;t++) if (s[x][t] && !used[t]) { used[t]=true; dfs(s[x][t]); ans[++num]=t; } } int main() { int x,y,id,i; while (~scanf("%d%d",&x,&y) && x && y) { memset(d,0,sizeof(d)); memset(s,0,sizeof(s)); scanf("%d",&id); d[x]++,d[y]++; s[x][id]=y,s[y][id]=x; m=id; while (~scanf("%d%d",&x,&y) && x && y) { scanf("%d",&id); d[x]++,d[y]++; s[x][id]=y,s[y][id]=x; m=max(m,id); } if (!legal(50)) { printf("Round trip does not exist.\n"); continue; } memset(used,false,sizeof(used)),num=0; dfs(1); for (i=num;i>=1;i--) printf("%d ",ans[i]); printf("\n"); } return 0; }
相关文章推荐
- POJ 1041 John's trip 欧拉回路(输出路径)
- POJ 1041 John's trip(欧拉回路+输出路径)
- poj1041 John's trip(欧拉回路+输出路径)
- POJ 1041 John's trip(无向图欧拉回路 && 路径)
- Poj 1041 John's trip (Fleury算法求欧拉回路路径)
- POJ 1041 John's trip (无向图欧拉回路)
- POJ1041 John's trip(欧拉回路 + 并查集 + dfs)
- POJ 1041 John's trip(欧拉回路)
- poj 1041 John's trip (边最小字典序欧拉路径 Fleury)
- poj 1041 John's trip(欧拉回路)
- John's trip 欧拉回路输出路径
- poj1041 John's trip【无向图欧拉回路判定+输出路径】
- POJ 1041 John's trip(欧拉回路)
- POJ 1041 John's trip(欧拉回路)
- POJ 1041 John's trip (在POJ首题~~经典的欧拉回路)
- poj 1041 John's trip 欧拉回路
- hdu 1041 John's trip 输出欧拉回路
- poj1041 John's trip,无向图求欧拉回路路径
- poj 1041 John's trip
- POJ - 1041 John's trip(欧拉回路+dfs输出路径)