POJ 1041 John's trip 无向图的【欧拉回路】路径输出
欧拉回路第一题TVT
本题的一个小技巧在于:
【建立一个存放点与边关系的邻接矩阵】
1.先判断是否存在欧拉路径
无向图:
欧拉回路:连通 + 所有定点的度为偶数
欧拉路径:连通 + 除源点和终点外都为偶数
有向图:
欧拉回路:连通 + 所有点的入度 == 出度
欧拉路径:连通 + 源点 出度-入度=1 && 终点 入度 - 出度 = 1 && 其余点 入度 == 出度;
2.求欧拉路径 :
step 1:选取起点(如果是点的度数全为偶数任意点为S如果有两个点的度数位奇数取一个奇数度点为S)
step 2:对当前选中的点的所有边扩展,扩展条件(这条边为被标记),若可扩展 ->step 2;否则 step 3;
step 3:将次边计入path结果保存。
思路还是很清晰的。
贴代码了:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm> #define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
int vis[], indegree[], map[][], n;
stack <int> s; void init(){
n = ;
memset(vis, , sizeof(vis));
memset(map, , sizeof(map));//define the relationship between vertex and edge
memset(indegree, , sizeof(indegree));
} int euler(int u){
int i;
for(i = ; i <= n; ++i){// n represents edges
if(map[u][i] && !vis[i]){
vis[i] = ;
euler(map[u][i]);
s.push(i);
}
}
return ;
} int main(){
int first, i, j, x, y, w;
while(cin >> x >> y){
if(x == && y == ) break;
cin >> w;
init();
map[x][w] = y;
map[y][w] = x;
++indegree[x];
++indegree[y];
first = x > y ? y : x;//get first vertex , but speacil judge as u casual
n = n > w ? n : w;//get numbered largest edge
while(true){
cin >> x >> y;
if(!x && !y)
break;
cin >> w;
map[x][w] = y;
map[y][w] = x;
++indegree[x];
++indegree[y];
n = n > w ? n : w;
}
for(i = ; i < ; ++i)//judge if exists solution
if(indegree[i] % ) break;
if(i < )
cout << "Round trip does not exist.\n";
else{
euler(first);
while(!s.empty()){
cout << s.top() << ' ';
s.pop();
}
cout << endl;
}
}
return ;
}
POJ 1041 John's trip 无向图的【欧拉回路】路径输出的更多相关文章
- poj 1041 John's trip——欧拉回路字典序输出
题目:http://poj.org/problem?id=1041 明明是欧拉回路字典序输出的模板. 优先队列存边有毒.写跪.学习学习TJ发现只要按边权从大到小排序连边就能正常用邻接表了! 还有一种存 ...
- poj 1041 John's trip 欧拉回路
题目链接 求给出的图是否存在欧拉回路并输出路径, 从1这个点开始, 输出时按边的升序输出. 将每个点的边排序一下就可以. #include <iostream> #include < ...
- [POJ 1041] John's Trip
[题目链接] http://poj.org/problem?id=1041 [算法] 欧拉回路[代码] #include <algorithm> #include <bitset&g ...
- UOJ 117 欧拉回路(套圈法+欧拉回路路径输出+骚操作)
题目链接:http://uoj.ac/problem/117 题目大意: 解题思路:先判断度数: 若G为有向图,欧拉回路的点的出度等于入度. 若G为无向图,欧拉回路的点的度数位偶数. 然后判断连通性, ...
- POJ 1041 John's trip Euler欧拉回路判定和求回路
就是欧拉判定,判定之后就能够使用DFS求欧拉回路了.图论内容. 这里使用邻接矩阵会快非常多速度. 这类题目都是十分困难的.光是定义的记录的数组变量就会是一大堆. #include <cstdio ...
- POJ 2631 DFS+带权无向图最长路径
http://poj.org/problem?id=2631 2333水题, 有一个小技巧是说随便找一个点作为起点, 找到这个点的最远点, 以这个最远点为起点, 再次找到的最远点就是这个图的最远点 证 ...
- POJ 3436 ACM Computer Factory(最大流+路径输出)
http://poj.org/problem?id=3436 题意: 每台计算机包含P个部件,当所有这些部件都准备齐全后,计算机就组装完成了.计算机的生产过程通过N台不同的机器来完成,每台机器用它的性 ...
- poj 1041(欧拉回路+输出字典序最小路径)
题目链接:http://poj.org/problem?id=1041 思路:懒得写了,直接copy吧:对于一个图可以从一个顶点沿着边走下去,每个边只走一次,所有的边都经过后回到原点的路.一个无向图存 ...
- UVA302 John's trip(欧拉回路)
UVA302 John's trip 欧拉回路 attention: 如果有多组解,按字典序输出. 起点为每组数据所给的第一条边的编号较小的路口 每次输出完额外换一行 保证连通性 每次输入数据结束后, ...
随机推荐
- sping+maven+mybatis+ehcache续之实现mapper
配置接着上一篇文章 新建UserMapper.java和UserMapper.xml 其中UserMapper.xml的namespace以及文件名要和UserMapper.java一致 <ma ...
- spss
编辑 SPSS(Statistical Product and Service Solutions),“统计产品与服务解决方案”软件.最初软件全称为“社会科学统计软件包” (SolutionsStat ...
- Symfony Composer icu requires lib-icu
运行php compser.phar 的时候出现此问题的时候解决办法 问题描述Problem 1 -Installation request for symfony/icu v1.2.1 -> ...
- Warning: Unable to send packet: Error with PF_PACKET send() [11]: Message too long (errno = 90)
今天在使用tcpreplay重放流量时,发现有的数据包没有发送成功: Warning: Unable to send packet: Error with PF_PACKET send() [215] ...
- Android应用--简、美音乐播放器增加音量控制
Android应用--简.美音乐播放器增加音量控制 2013年6月26日简.美音乐播放器继续完善中.. 题外话:上一篇博客是在6月11号发的,那篇博客似乎有点问题,可能是因为代码结构有点乱的原因,很难 ...
- CentOS, 高速设置ssh无password登录
首先.保证能够ping通 然后运行例如以下命令, master登录slave master上面运行例如以下指令: 2.4 确认本机sshd的配置文件(root) $ vi/etc/ssh/sshd_c ...
- ecshop标签大全 各个页面常用标签大全
先从index.php主页开始 页面关键字 {$keywords } 页面标题 {$page_title} 产品分类 父分类列表 {foreach from=$categories item=cat ...
- WinRT Toolkit 介绍--Control篇
WinRT toolkit是组针对Windows Runtime XAML开发的一系列Control,extension和helper类.它和Windows Phone Toolkit一样,也是由很多 ...
- mac下面xcode+ndk7配置cocos2dx & box2d的跨ios和android平台的游戏教程
这篇教程是介绍如何使用cocos2d-x和box2d来制作一个demo,且此demo能同时运行于ios和android平台.在继续阅读之前,建议您先阅读上一篇教程. 首先,按照上一篇教程,搭建好mac ...
- [LeetCode]题解(python):014-Longest Common Prefix
题目来源: https://leetcode.com/problems/longest-common-prefix/ 题意分析: 这道题目是要写一个函数,找出字符串组strs的最长公共前缀子字符串. ...