欧拉回路——John's trip ( POJ 1041 )
2016-10-05 19:21
330 查看
题目链接:
http://poj.org/problem?id=1041
分析:
一共最多44个路口,1995个街道,每次给出x,y,z表示x路口和y路口由z街道相连接,第一行给出的两个路口里最小的为起点,并且每条路口最多走一次。求从起点出发,能否走遍所有路口,若能则输出路径即街道序号(要求字典序最小)
题解:
这是一道很明显的欧拉回路,并且是无向边。
判定:无向边直接记录度数,如果有度数为奇数的路口,则不可能形成欧拉回路。
求路径:如果有解,则直接从起点开始DFS,从1开始遍历街道的序号,如果能走则继续DFS另一头的点,然后倒着把街道序号加入栈中,最后倒着输出。
注意:
因为需要直接查询街道和路口,并且范围并不大,所以用二维数组存储图。
AC代码:
![](http://img.blog.csdn.net/20161005191343293)
http://poj.org/problem?id=1041
分析:
一共最多44个路口,1995个街道,每次给出x,y,z表示x路口和y路口由z街道相连接,第一行给出的两个路口里最小的为起点,并且每条路口最多走一次。求从起点出发,能否走遍所有路口,若能则输出路径即街道序号(要求字典序最小)
题解:
这是一道很明显的欧拉回路,并且是无向边。
判定:无向边直接记录度数,如果有度数为奇数的路口,则不可能形成欧拉回路。
求路径:如果有解,则直接从起点开始DFS,从1开始遍历街道的序号,如果能走则继续DFS另一头的点,然后倒着把街道序号加入栈中,最后倒着输出。
注意:
因为需要直接查询街道和路口,并且范围并不大,所以用二维数组存储图。
AC代码:
/************************************************************************* > File Name: 1041.cpp > Author: Akira > Mail: qaq.febr2.qaq@gmail.com > Created Time: 2016年10月05日 星期三 19时15分12秒 ************************************************************************ #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cmath> #include <vector> #include <set> #include <list> #include <ctime> typedef long long LL; typedef unsigned long long ULL; typedef long double LD; #define MST(a,b) memset(a,b,sizeof(a)) #define CLR(a) MST(a,0) #define Sqr(a) ((a)*(a)) using namespace std; #define MaxN 233 #define MaxM MaxN*20 #define INF 0x3f3f3f3f #define bug cout<<88888888<<endl; #define MIN(x,y) (x<y?x:y) #define MAX(x,y) (x>y?x:y) int degree[MaxN]; int mp[MaxN][MaxM]; int street[MaxM][2]; int vis[MaxM]; stack <int> S; int cnt; void dfs(int x) { for(int i=1;i<=cnt;i++) { if( !vis[i]&& mp[x][i]) { vis[i] = 1; dfs(mp[x][i]); S.push(i); } } } int home; int main() { int x,y,z; while(~scanf("%d%d", &x, &y)) { if(x==0 && y ==0) { int flag = 0; for(int i=1;i<=44;i++) { if(degree[i] & 1) { flag = 1; break; } } if(flag) { cout << "Round trip does not exist.\n"; } else { dfs(home); while(!S.empty()) { cout << S.top(); if(S.size() != 1) cout << " "; S.pop(); } cout << endl; } CLR(vis); CLR(degree); CLR(street); CLR(mp); cnt = 0; } else { if(cnt==0) home = x<y?x:y; scanf("%d", &z); degree[x]++; degree[y]++; mp[x][z] = y; mp[y][z] = x; street[z][0] = x; street[z][1] = y; cnt++; } } }
相关文章推荐
- POJ 1041 John's trip(欧拉回路)
- POJ 1041 John's trip (欧拉回路)
- POJ 1041 John's trip(欧拉回路)
- POJ 1041 John's trip 欧拉回路(输出路径)
- (POJ 1041)John's trip [无向欧拉图] 输出字典序最小的欧拉回路
- POJ 1041 - John's trip(欧拉回路)
- POJ 1041 John's trip(欧拉回路)
- POJ 1041 John's trip (欧拉回路)
- poj1041 John's trip(欧拉回路+输出路径)
- Poj 1041 John's trip (Fleury算法求欧拉回路路径)
- POJ 1041 John's trip (无向图欧拉回路)
- POJ 1041 John's trip(无向图欧拉回路 && 路径)
- poj 1041 John's trip(欧拉回路入门题)
- poj 1041 John's trip 欧拉回路
- POJ 1041 John's trip(欧拉回路+输出路径)
- poj1041 John's trip (欧拉回路)
- POJ1041 John's trip(欧拉回路 + 并查集 + dfs)
- POJ1041 John's trip(欧拉回路+打印路径)
- POJ 1041 - John's trip 输出欧拉回路路径边..通用做法
- poj 1041 John's trip(欧拉回路)