zoukankan      html  css  js  c++  java
  • 欧拉回路输出(DFS,不用回溯!)Watchcow POJ 2230

    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 8109   Accepted: 3551   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

    Hint

    OUTPUT DETAILS: 

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

    Source

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<sstream>
    #include<algorithm>
    #include<queue>
    #include<deque>
    #include<iomanip>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<stack>
    #include<set>
    #include<fstream>
    #include<memory>
    #include<list>
    #include<string>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    #define MAXN 10009
    #define N 50009
    #define MOD 10000007
    #define INF 1000000009
    const double eps = 1e-9;
    const double PI = acos(-1.0);
    
    struct edge
    {
        edge(int _v, bool _vis) :v(_v), vis(_vis){}
        int v;
        bool vis;
    };
    vector<edge> E[MAXN];
    int n, m;
    void DFS(int cur)
    {
        for (int i = 0; i < E[cur].size(); i++)
        {
            if (!E[cur][i].vis)
            {
                E[cur][i].vis = true;    
                DFS(E[cur][i].v);
            }
        }
        printf("%d
    ", cur);
    }
    int main()
    {
        while (scanf("%d%d", &n, &m) != EOF)
        {
            for (int i = 1; i <= n; i++)
                E[i].clear();
            int f, t;
            for (int i = 0; i < m; i++)
                scanf("%d%d", &f, &t), E[f].push_back(edge(t,false)), E[t].push_back(edge(f,false));
            DFS(1);
        }
    }

     上面是有向图的回溯

    下面是无向图。从度最大的点往前回溯

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<map>
    #include<cstring>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define MAXN 55
    #define N 2000
    typedef long long LL;
    
    /*
    无向图的欧拉回路
    从度最大的点开始回溯
    */
    int T, n;
    int g[MAXN][MAXN];
    int degree[MAXN];
    void dfs(int k)
    {
        for (int i = 1; i <= 50; i++)
        {
            if (g[k][i])
            {
                g[k][i]--, g[i][k]--;
                dfs(i);
                printf("%d %d
    ", i, k);
            }
        }
    }
    int main()
    {
        scanf("%d", &T);
        for (int cas = 1; cas <= T; cas++)
        {
            memset(degree, 0, sizeof(degree));
            memset(g, 0, sizeof(g));
            scanf("%d", &n);
            for (int i = 0; i < n; i++)
            {
                int a, b;
                scanf("%d%d", &a, &b);
                g[a][b]++, g[b][a]++;
                degree[a]++, degree[b]++;
            }
            int Max = -1, k = -1;
            bool f = true;
            for (int i = 0; i < MAXN; i++)
            {
                if (degree[i] > Max)
                {
                    Max = degree[i], k = i;
                }
                if (degree[i] % 2 == 1)
                {
                    f = false;
                    break;
                }
            }
            printf("Case #%d
    ", cas);
            if (f)
                dfs(k);
            else
                printf("some beads may be lost
    ");
            if (cas <= T)
                printf("
    ");
        
        }
    }
  • 相关阅读:
    家庭记账本app进度之对于登录和注册两个界面点击按钮的相互跳转
    家庭记账本app进度之复选框以及相应滚动条的应用
    家庭版记账本app之常用控件的使用方法
    android 软件(app)之家庭版记账本首次进行helloword等相关测试
    家庭记账本app进度之android中AlertDialog的相关应用以及对日期时间的相关操作(应用alertdialog使用的谈话框)
    家庭版记账本app进度之关于android界面布局的相关学习
    家庭记账本app进度之关于单选按钮的相关操作(添加点击按钮事件以及点击单选更改事件)
    家庭记账本app进度之ui相关概念控制ui界面与布局管理
    家庭版记账本app进度之对于按钮的点击事件以及线性布局以及(alertdialog)等相关内容的应用测试
    乱了
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7269170.html
Copyright © 2011-2022 走看看