• 欧拉回路求路径POJ 2230


    Watchcow
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 8841   Accepted: 3854   Special Judge

    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


    代码分为递归和非递归版本

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <stack>
     6 
     7 using namespace std;
     8 const int MAXN = 1e5 + 7;
     9 const int MAXM = 5e5 + 7;
    10 
    11 int n, m, first[MAXN], sign, vis[MAXN];
    12 
    13 struct Edge {
    14     int to, w, next;
    15 } edge[MAXM * 4];
    16 
    17 inline void init() {
    18     for(int i = 0; i <= n; i++ ) {
    19         first[i] = -1;
    20         vis[i] = 0;
    21     }
    22     sign = 0;
    23 }
    24 
    25 inline void add_edge(int u, int v, int w) {
    26     edge[sign].to = v;
    27     edge[sign].w = w;
    28     edge[sign].next = first[u];
    29     first[u] = sign++;
    30 }
    31 
    32 void dfs(int x) {
    33     for(int i = first[x]; ~i; i = edge[i].next) {
    34         int to = edge[i].to;
    35         if(!vis[i]) {
    36             vis[i] = 1;
    37             dfs(to);
    38         }
    39     }
    40     printf("%d
    ", x);
    41 }
    42 
    43 int main()
    44 {
    45     while(~scanf("%d %d", &n, &m)) {
    46         init();
    47         for(int i = 1; i <= m; i++ ) {
    48             int u, v;
    49             scanf("%d %d", &u, &v);
    50             add_edge(u, v, 1);
    51             add_edge(v, u, 1);
    52         }
    53         dfs(1);
    54     }
    55 
    56     return 0;
    57 }
    View Code



     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <stack>
     6 
     7 using namespace std;
     8 const int MAXN = 1e5 + 7;
     9 const int MAXM = 5e5 + 7;
    10 
    11 int n, m, first[MAXN], sign, vis[MAXN];
    12 
    13 struct Edge {
    14     int to, w, next;
    15 } edge[MAXM * 4];
    16 
    17 inline void init() {
    18     for(int i = 0; i <= n; i++ ) {
    19         first[i] = -1;
    20         vis[i] = 0;
    21     }
    22     sign = 0;
    23 }
    24 
    25 inline void add_edge(int u, int v, int w) {
    26     edge[sign].to = v;
    27     edge[sign].w = w;
    28     edge[sign].next = first[u];
    29     first[u] = sign++;
    30 }
    31 
    32 stack<int>st, ans;
    33 
    34 void eulur(int start) {
    35     while(!st.empty()) {
    36         st.pop();
    37     }
    38     while(!ans.empty()) {
    39         ans.pop();
    40     }
    41     st.push(start);
    42     while(!st.empty()) {
    43         int x = st.top(), i = first[x];
    44         while(~i && vis[i]) {
    45             i = edge[i].next;
    46         }
    47         if(~i) {
    48             st.push(edge[i].to);
    49             //vis[i] = vis[i ^ 1] = 1;
    50             vis[i] = 1;
    51             first[x] = edge[i].next;
    52         } else {
    53             st.pop();
    54             ans.push(x);
    55         }
    56     }
    57 }
    58 
    59 
    60 int main()
    61 {
    62     while(~scanf("%d %d", &n, &m)) {
    63         init();
    64         for(int i = 1; i <= m; i++ ) {
    65             int u, v;
    66             scanf("%d %d", &u, &v);
    67             add_edge(u, v, 1);
    68             add_edge(v, u, 1);
    69         }
    70         eulur(1);
    71         while(!ans.empty()) {
    72             printf("%d
    ", ans.top());
    73             ans.pop();
    74         }
    75     }
    76 
    77     return 0;
    78 }
    View Code



  • 相关阅读:
    二、JVM内存模型
    一、计算机内存与JVM内存简析
    Centos7编译openjdk7
    linux查看CPU内核信息
    HashMap源码解析
    windows下安装MongoDB要注意的问题
    Javascript理解this对象
    关于javascript闭包中的this对象
    mac系统 IDEA+JFinal+Tomcat+Maven搭建
    Mac下python3配置Sklearn
  • 原文地址:https://www.cnblogs.com/Q1143316492/p/9194298.html
Copyright © 2020-2023  润新知