poj2230 欧拉回路
http://poj.org/problem?id=2230
Description
If she were a more observant cow, she might be able to just walk each of M (1 <= M <= 50,000) bidirectional trails numbered 1..M between N (2 <= N <= 10,000) fields numbered 1..N on the farm once and be confident that she's seen everything she needs to see. But since she isn't, she wants to make sure she walks down each trail exactly twice. It's also important that her two trips along each trail be in opposite directions, so that she doesn't miss the same thing twice.
A pair of fields might be connected by more than one trail. Find a path that Bessie can follow which will meet her requirements. Such a path is guaranteed to exist.
Input
* Lines 2..M+1: Two integers denoting a pair of fields connected by a path.
Output
Sample Input
4 5
1 2
1 4
2 3
2 4
3 4
Sample Output
1
2
3
4
2
1
4
3
2
4
1
Hint
Bessie starts at 1 (barn), goes to 2, then 3, etc...
题解:
一个图是欧拉图,那么他的子图也是一个欧拉图,只需dfs即可
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=1e5+10;
struct node{
int v;
int next;
}G[MAXN];
int head[MAXN],cnt;
bool vis[MAXN];
int ans[MAXN];
void add(int u,int v)
{
G[++cnt].v=v;
G[cnt].next=head[u];
head[u]=cnt;
}
int n,m,k=0;
int dfs(int u)
{
for (int i = head[u]; i!=-1 ; i=G[i].next) {
if(!vis[i])
{
vis[i]= true;
dfs(G[i].v);
ans[k++]=G[i].v;
}
}
}
int main() {
while (scanf("%d%d",&n,&m)!=EOF)
{
cnt=0,k=0;
memset(head,-1, sizeof(head));
memset(vis,false, sizeof(vis));
int u,v;
for (int i = 0; i <m ; ++i) {
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(1);
for (int i = 0; i <k ; ++i) {
printf("%d\n",ans[i]);
}
printf("1\n");
}
return 0;
}
//poj2230
poj2230 欧拉回路的更多相关文章
- 0x66 Tarjan算法与无向图联通性
bzoj1123: [POI2008]BLO poj3694 先e-DCC缩点,此时图就变成了树,树上每一条边都是桥.对于添加边的操作,相当于和树上一条路径构环,导致该路径上所有边都不成为桥.那么找这 ...
- Watchcow(POJ2230+双向欧拉回路+打印路径)
题目链接:http://poj.org/problem?id=2230 题目: 题意:给你m条路径,求一条路径使得从1出发最后回到1,并满足每条路径都恰好被沿着正反两个方向经过一次. 思路:由于可以回 ...
- POJ2230 Watchcow【欧拉回路】
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6172Accepted: 2663 Special Judge ...
- poj2230 Watchcow【欧拉回路】【输出路径】(遍历所有边的两个方向)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4392 题目大意: 一个图,要将每条边恰好遍历两遍,而且要以不同的方向,还要回到原点. dfs解法 ...
- POJ2230(打印欧拉回路)
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7473 Accepted: 3270 Specia ...
- POJ2230Watchcow[欧拉回路]
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7512 Accepted: 3290 Specia ...
- ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)
//网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...
- [poj2337]求字典序最小欧拉回路
注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...
- ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
FZU 2112 Tickets Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u P ...
随机推荐
- PWM----调节LED亮度
- - --调节两个LED灯亮度 module led_pwm ( clk, rst, //cnt1_pwm, out1, out2, out3, out4 ); input clk, rst; // ...
- tp5.0:替换修改js、css等样式文件路径
首先, 我们要知道,TP5已经不支持绝对路径访问样式文件啦!所以我们不必去花时间去找使用$_SERVER来获取 手册位置:模板->内置标签->资源文件加载 方法一: 过程: 1.首先在模块 ...
- March 13 2017 Week 11 Monday
A warm smile is the universal language of kindness. 温暖的笑容是善意的通用语. Face comes from the heart. Just sm ...
- Linux下elk安装配置
安装jdkJDK版本大于1.8 elk下载地址:https://www.elastic.co/products注意:elk三个版本都要保持一致. rpm -ivh elasticsearch-5.4. ...
- linux下vi的一些简单的操作
前言 在嵌入式linux开发中,进行需要修改一下配置文件之类的,必须使用vi,因此,熟悉 vi 的一些基本操作,有助于提高工作效率. 一,模式 vi编辑器有3种模式:命令模式.输入模式.末行模式.掌握 ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland (DFS或并查集)
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Python map/reduce/filter/sorted函数以及匿名函数
1. map() 函数的功能: map(f, [x1,x2,x3]) = [f(x1), f(x2), f(x3)] def f(x): return x*x a = map(f, [1, 2, 3, ...
- 【luogu P2580 于是他错误的点名开始了】 题解
题目链接:https://www.luogu.org/problemnew/show/P2580 我真的永远都爱stl #include <map> #include <cstdio ...
- Tomcat 服务器体系结构
connector 监听端口,监听到以后,交给 Engine 引擎 处理,引擎会根据请求找到对应的主机,找到主机后再去找对应的应用. 如果我们将 port 改为 80,那访问的时候就不用输入端口号,因 ...
- 获取APP地图权限
获取APP地图权限 NSLocationWhenUseUsageDescription,在info里面设置为空