zoukankan      html  css  js  c++  java
  • POJ2230 Watchcow【欧拉回路】

    Watchcow
    Time Limit: 3000MS Memory Limit: 65536K
    Total Submissions: 6172Accepted: 2663 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...


    题目大意:给你一个N个点的图,M条双向边,从原点1出发,两个方向各走

    一遍。最后回到原点。输出整个路径。

    从1開始。到1结束。

    共2*M+1行。

    思路:DFS遍历,vis数组标记已遍历的边。

    DFS的思想等效于先找一个环。后对环上全部点递归DFS。而且把这些递归

    产生的路插入这个环中。

    最重要的地方是在哪里保存路径。由于DFS函数的结

    束顺序就是点的回溯顺序。所以应该在DFS回溯完之后再记录当前点的序号,

    也就是now的值。


    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int MAXN = 10010;
    const int MAXM = 100010;
    
    int head[MAXN],N,M;
    struct EdgeNode
    {
        int to;
        int w;
        int next;
    };
    
    EdgeNode Edges[MAXM];
    
    int ans[MAXM],ansi;
    bool vis[MAXM];
    void DFS(int now)
    {
        int k;
        for(k = head[now]; k != -1; k = Edges[k].next)
        {
            if(!vis[k])
            {
                vis[k] = true;
                DFS(Edges[k].to);
            }
        }
        ans[ansi++] = now;
    }
    
    int main()
    {
        while(cin >> N >> M)
        {
            int x,y;
            memset(Edges,0,sizeof(Edges));
            memset(head,-1,sizeof(head));
            memset(ans,0,sizeof(ans));
            memset(vis,0,sizeof(vis));
            int j = 0;
            for(int i = 0; i < M; ++i)
            {
                cin >> x >> y;
                Edges[j].to = y;
                Edges[j].w = 1;
                Edges[j].next = head[x];
                head[x] = j;
                j++;
                Edges[j].to = x;
                Edges[j].w = 1;
                Edges[j].next = head[y];
                head[y] = j;
                j++;
            }
            ansi = 0;
            DFS(1);
            for(int i = 0; i < ansi; ++i)
                cout << ans[i] << endl;
        }
    
        return 0;
    }
    



  • 相关阅读:
    GAN生成对抗网络-DCGAN原理与基本实现-深度卷积生成对抗网络03
    【我的遇见】
    【笔记汇总贴】
    vtr搭建并使用ipv6免流上校园网教程(CentOs8避坑指南)
    国科大抢课避坑+选课指南+教务系统操作
    论文:Bottom-Up and Top-Down Attention for Image Captioning and Visual Question Answering-阅读总结
    bernoulli, multinoulli distributions 讲解
    论文:Show, Attend and Tell: Neural Image Caption Generation with Visual Attention-阅读总结
    论文:Show and Tell: A Neural Image Caption Generator-阅读总结
    【吴恩达课程使用】pip安装pandas失败-anaconda各种玄学T-T-从新开始搭建环境
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5266199.html
Copyright © 2011-2022 走看看