zoukankan      html  css  js  c++  java
  • Watchcow(POJ2230+双向欧拉回路+打印路径)

    题目链接:http://poj.org/problem?id=2230

    题目:

    题意:给你m条路径,求一条路径使得从1出发最后回到1,并满足每条路径都恰好被沿着正反两个方向经过一次。

    思路:由于可以回到起点,并且题目保证有解,所以本题是欧拉回路。输出路径有两种方法,一种是递归实现,一种是用栈处理,不过两种速度差了1s,不知道是不是我递归没处理好~

    代码实现如下(第一种为栈输出路径,对应797ms,第二种为递归,对应1782ms):

     1 #include <set>
     2 #include <map>
     3 #include <queue>
     4 #include <stack>
     5 #include <cmath>
     6 #include <bitset>
     7 #include <cstdio>
     8 #include <string>
     9 #include <vector>
    10 #include <cstdlib>
    11 #include <cstring>
    12 #include <iostream>
    13 #include <algorithm>
    14 using namespace std;
    15 
    16 typedef long long ll;
    17 typedef pair<ll, ll> pll;
    18 typedef pair<ll, int> pli;
    19 typedef pair<int, ll> pil;;
    20 typedef pair<int, int> pii;
    21 typedef unsigned long long ull;
    22 
    23 #define lson i<<1
    24 #define rson i<<1|1
    25 #define bug printf("*********
    ");
    26 #define FIN freopen("D://code//in.txt", "r", stdin);
    27 #define debug(x) cout<<"["<<x<<"]" <<endl;
    28 #define IO ios::sync_with_stdio(false),cin.tie(0);
    29 
    30 const double eps = 1e-8;
    31 const int mod = 10007;
    32 const int maxn = 5e4 + 7;
    33 const double pi = acos(-1);
    34 const int inf = 0x3f3f3f3f;
    35 const ll INF = 0x3f3f3f3f3f3f3f;
    36 
    37 int n, m, u, v, tot, top, t;
    38 int head[maxn], stc[maxn*10], ans[maxn*10], vis[maxn*10];
    39 
    40 struct edge {
    41     int v, next;
    42 }ed[maxn*10];
    43 
    44 void addedge(int u, int v) {
    45     ed[tot].v = v;
    46     ed[tot].next = head[u];
    47     head[u] = tot++;
    48     ed[tot].v = u;
    49     ed[tot].next = head[v];
    50     head[v] = tot++;
    51 }
    52 
    53 void eulergraph() {
    54     stc[++top] = 1;
    55     while(top > 0) {
    56         int x = stc[top], i = head[x];
    57         while(i != -1 && vis[i]) i = ed[i].next;
    58         if(i != -1) {
    59             stc[++top] = ed[i].v;
    60             head[x] = ed[i].next;
    61             vis[i] = 1;
    62         } else {
    63             top--;
    64             ans[++t] = x;
    65         }
    66     }
    67 }
    68 
    69 int main() {
    70     //FIN;
    71     scanf("%d%d", &n, &m);
    72     tot = top = t = 0;
    73     memset(stc, 0, sizeof(stc));
    74     memset(ans, 0, sizeof(ans));
    75     memset(vis, 0, sizeof(vis));
    76     memset(head, -1, sizeof(head));
    77     for(int i = 1; i <= m; i++) {
    78         scanf("%d%d", &u, &v);
    79         addedge(u, v);
    80     }
    81     eulergraph();
    82     for(int i = t; i; i--) printf("%d
    ", ans[i]);
    83 }
     1 #include <set>
     2 #include <map>
     3 #include <queue>
     4 #include <stack>
     5 #include <cmath>
     6 #include <bitset>
     7 #include <cstdio>
     8 #include <string>
     9 #include <vector>
    10 #include <cstdlib>
    11 #include <cstring>
    12 #include <iostream>
    13 #include <algorithm>
    14 using namespace std;
    15 
    16 typedef long long ll;
    17 typedef pair<ll, ll> pll;
    18 typedef pair<ll, int> pli;
    19 typedef pair<int, ll> pil;;
    20 typedef pair<int, int> pii;
    21 typedef unsigned long long ull;
    22 
    23 #define lson i<<1
    24 #define rson i<<1|1
    25 #define bug printf("*********
    ");
    26 #define FIN freopen("D://code//in.txt", "r", stdin);
    27 #define debug(x) cout<<"["<<x<<"]" <<endl;
    28 #define IO ios::sync_with_stdio(false),cin.tie(0);
    29 
    30 const double eps = 1e-8;
    31 const int mod = 10007;
    32 const int maxn = 1e4 + 7;
    33 const double pi = acos(-1);
    34 const int inf = 0x3f3f3f3f;
    35 const ll INF = 0x3f3f3f3f3f3f3f;
    36 
    37 int n, m, u, v, tot, top, t;
    38 int head[maxn], vis[maxn*10];
    39 
    40 struct edge {
    41     int v, next;
    42 }ed[maxn*10];
    43 
    44 void addedge(int u, int v) {
    45     ed[tot].v = v;
    46     ed[tot].next = head[u];
    47     head[u] = tot++;
    48     ed[tot].v = u;
    49     ed[tot].next = head[v];
    50     head[v] = tot++;
    51 }
    52 
    53 void eulergraph(int x) {
    54     for(int i = head[x]; ~i; i = ed[i].next) {
    55         if(!vis[i]) {
    56             vis[i] = 1;
    57             eulergraph(ed[i].v);
    58         }
    59     }
    60     printf("%d
    ", x);
    61 }
    62 
    63 int main() {
    64     //FIN;
    65     scanf("%d%d", &n, &m);
    66     tot = top = t = 0;
    67     memset(vis, 0, sizeof(vis));
    68     memset(head, -1, sizeof(head));
    69     for(int i = 1; i <= m; i++) {
    70         scanf("%d%d", &u, &v);
    71         addedge(u, v);
    72     }
    73     eulergraph(1);
    74     return 0;
    75 }
  • 相关阅读:
    强化学习 相关资源
    Log4j输出文件到目的地
    httpclient 封装post 和get
    Cookie 和Session区别
    day09 request 和response
    Jmeter 断言
    Jmeter自学笔记10----性能测试基础实战
    Jmeter 目录
    性能测试解惑之并发压力
    设计模式,就是那个抽象工厂没写
  • 原文地址:https://www.cnblogs.com/Dillonh/p/9395734.html
Copyright © 2011-2022 走看看