poj1041 John's trip,无向图求欧拉回路路径

点击打开链接

无向图求欧拉回路:

1、图连通

2、所有顶点的度数位偶数

#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;
const int mt = 2000;
const int ms = 50;

bool vis[mt+5];
int g[ms][mt+5];
int deg[ms+5];
int	f[ms+5];
stack<int> s;
int street, startv;

int findset(int x){
	return f[x]==x?f[x]:f[x]=findset(f[x]);
}

void Union(int x, int y){
	int fx = findset(x);
	int fy = findset(y);
	if(fx!=fy){
		f[fx] = fy;
	}
}

bool used[ms];

bool check(){
	for(int i=1,cnt=0; i<=ms; ++i) if(used[i]){
		if(deg[i]&1) return false;
		if(f[i]==i){
			if(cnt++==1) return false;
		}
	}
	return true;
}

void euler(int u){
	for(int i=1; i<=street; ++i) if(g[u][i]){
		if(!vis[i]){
			vis[i] = 1;
			euler(g[u][i]);
			s.push(i);
		}
	}
}	

//ps:其实给出的图已经连通。。。。
int main()
{
	int x, y, z;
	while(~scanf("%d%d", &x, &y)){
		if(x==0&&y==0) break;
		scanf("%d", &z);
		memset(g, 0, sizeof g );
		for(int i=0; i<=ms; ++i) f[i] = i;
		memset(used, 0, sizeof used );
		memset(deg, 0, sizeof deg );
		street = z;
		startv = min(x, y);
		deg[x]++; deg[y]++;
		g[x][z] = y;
		g[y][z] = x;
		Union(x, y);
		used[x] = used[y] = 1;
		while(~scanf("%d%d", &x, &y)){
			if(x==0&&y==0) break;
			scanf("%d", &z);
			street = max(z, street);
			deg[x]++; deg[y]++;
			g[x][z] = y;
			g[y][z] = x;
			Union(x, y);
			used[x] = used[y] = 1;
		}

		if(!check()){
			puts("Round trip does not exist.");
			continue;
		} else {
			memset(vis, 0, sizeof vis );
			euler(startv);
			while(!s.empty()){
				printf("%d ", s.top());
				s.pop();
			}
			printf("\n");
		}
	}
	return 0;
}

poj1041 John's trip,无向图求欧拉回路路径

时间: 2024-08-26 02:42:01

poj1041 John's trip,无向图求欧拉回路路径的相关文章

poj1041 John&#39;s trip (无向图求欧拉回路方案)

John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5950   Accepted: 1946   Special Judge Description Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his frien

POJ 1041 John&#39;s trip 无向图的【欧拉回路】路径输出

欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为偶数 有向图: 欧拉回路:连通 + 所有点的入度 == 出度 欧拉路径:连通 + 源点 出度-入度=1 && 终点 入度 - 出度 = 1 && 其余点 入度 == 出度: 2.求欧拉路径 : step 1:选取起点(如果是点的度数全为偶数任意点为S如果有两个点的度数位奇数取一

POJ 1041 John&#39;s trip (无向图欧拉回路)

John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7433   Accepted: 2465   Special Judge Description Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his frien

UVa10054 The Necklace,无向图求欧拉回路

无向图求欧拉回路: 1.图连通 2.所有顶点的度数位偶数 随便从一个点开始递归遍历即可求出路径 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxcolor = 50; int n, G[maxcolor+1][maxcolor+1], deg[maxcolor+1]; struct Edge{ int from, to; Edge(int f

The Necklace UVA - 10054 (无向图的欧拉回路)

The Necklace UVA - 10054 题意:每个珠子有两个颜色,给n个珠子,问能不能连成一个项链,使得项链相邻的珠子颜色相同. 把颜色看做点,珠子内部连一条边,无向图求欧拉回路. 这里我用的并查集. 输出路径就dfs就行了 1 #include <bits/stdc++.h> 2 using namespace std; 3 int g[55][55]; 4 int f[55]; 5 int deg[55]; 6 int n; 7 8 int gf(int x) 9 { 10 re

POJ 1041 John&#39;s trip Euler欧拉回路判定和求回路

就是欧拉判定,判定之后就可以使用DFS求欧拉回路了.图论内容. 这里使用邻接矩阵会快很多速度. 这类题目都是十分困难的,光是定义的记录的数组变量就会是一大堆. #include <cstdio> #include <cstring> #include <stack> #include <vector> using namespace std; struct Edge { int ed, des; Edge(int e = 0, int d = 0) : ed

poj 1041 John&#39;s trip 欧拉回路

题目链接 求给出的图是否存在欧拉回路并输出路径, 从1这个点开始, 输出时按边的升序输出. 将每个点的边排序一下就可以. 1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath> 7 #include <map> 8 #include

HDU - 6311 Cover(无向图的最少路径边覆盖 欧拉路径)

题意 给个无向图,无重边和自环,问最少需要多少路径把边覆盖了.并输出相应路径 分析 首先联通块之间是独立的,对于一个联通块内,最少路径覆盖就是  max(1,度数为奇数点的个数/2).然后就是求欧拉路径了,先将块内度数为奇数的点找出来,留下两个点,其余两两连上虚边,这样我们选择从一个奇数点出发到另一个奇数点,求出一条欧拉路径,统计总路径数.接着就dfs,注意一些细节. 附赠一个求欧拉回路的fleury算法:https://blog.csdn.net/u011466175/article/deta

John&#39;s trip

[题目描述] John想要拜访每位朋友,且每条道路他都只走一次. John希望从家里出发,拜访完所有朋友后回到自己的家,且总的路程最短.John意识到如果可以然后返回起点应该是最短的路径.写一个程序帮助John找到这样的路径.给出的每条街连接两个路口,最多有1995条街,最多44个路口.街编号由1到n, 路口分别编号1到m. 输入:每个用例一个数据块:每行表示一条街,由三个整数组成:x,y,z. z为这条街的编号,x和y表示这条街连接的两个路口的编号.(实际数据中可能是自环).John住在一个输