[POJ | Nowcoder] Watchcow | 欧拉回路 点路径输出

简介: DescriptionBessie’s been appointed the new watch-cow for the farm. Every night, it’s her job to walk across the farm and make sure that no evildoers are doing any evil. She begins at the barn, makes her patrol, and then returns to the barn when she’s done.

Description


Bessie’s been appointed the new watch-cow for the farm. Every night, it’s her job to walk across the farm and make sure that no evildoers are doing any evil. She begins at the barn, makes her patrol, and then returns to the barn when she’s done.


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


  • Line 1: Two integers, N and M.


  • Lines 2…M+1: Two integers denoting a pair of fields connected by a path.


Output


  • Lines 1…2M+1: A list of fields she passes through, one per line, beginning and ending with the barn at field 1. If more than one solution is possible, output any solution.


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


OUTPUT DETAILS:


Bessie starts at 1 (barn), goes to 2, then 3, etc…

Source


USACO 2005 January Silver


欧拉回路的路径输出,记得要再遍历完所有的点之后在输出这个点,否则不正确


int n,m;
struct node{
  int u,v,nex;
  bool vis;
}e[maxn];
int cnt,head[maxn];
void _Init(){
  cnt = 0;
  for(int i=1;i<=n;i++) head[i] = -1;
}
void add(int u,int v){
  e[cnt].u = u;
  e[cnt].v = v;
  e[cnt].nex = head[u];
  head[u] = cnt ++;
  e[cnt].u = v;
  e[cnt].v = u;
  e[cnt].nex = head[v];
  head[v] = cnt ++;
}
deque<int> vet;
void dfs(int to){
  for(int i=head[to];~i;i=e[i].nex){
    int to = e[i].v;
    if(e[i].vis) continue;
    e[i].vis = true;
    dfs(to);
  }
  cout << to << endl;/// 
}
int main() {
  n = read,m = read;
  _Init();
  for(int i=1;i<=m;i++){
    int u = read,v = read;
    add(u,v);
  }
  dfs(1);
  return 0;
}




目录
相关文章
|
1月前
lanqiao OJ 644 方格分割
lanqiao OJ 644 方格分割
15 1
|
1月前
lanqiao OJ 1546 坐标搜寻
lanqiao OJ 1546 坐标搜寻
11 0
|
1月前
【九度 OJ 06】2.7输出梯形
【九度 OJ 06】2.7输出梯形
9 0
|
6月前
poj 3984 迷宫问题(BFS+输出路径)
poj 3984 迷宫问题(BFS+输出路径)
28 0
|
6月前
|
算法 C语言
【牛客-算法】NC56 回文数字
🚩 前言 🔥 该专栏作为算法题笔记,记录算法的思路、遇到的问题,以及能跑的代码,持续更新中! 🔥 推荐一款面试、刷题神器牛客网:👉开始刷题学习👈
57 0
|
移动开发 算法
DFS深度优先算法 —— AcWing 842. 排列数字AcWing 843. n-皇后问题
DFS深度优先算法 —— AcWing 842. 排列数字AcWing 843. n-皇后问题
103 0
|
算法
题目:输出n行杨辉三角形
题目:输出n行杨辉三角形
90 0
每日三题-最大子数组和、不同路径、最小路径和
每日三题-最大子数组和、不同路径、最小路径和
73 0
每日三题-最大子数组和、不同路径、最小路径和
递归题目练习---数独问题
递归题目练习---数独问题
111 0
递归题目练习---数独问题