zoukankan      html  css  js  c++  java
  • Day4

    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...
     
    思路:打印欧拉通路,题目保证有解,直接DFS打印即可,代码如下:
    const int maxm = 10010;
    const int maxn = 50010;
    
    struct Node {
        int from, to;
        Node(int _from, int _to) : from(_from), to(_to){}
    };
    
    int N, M, vis[maxn*2];
    vector<int> ans, G[maxm];
    vector<Node> edges;
    
    void addedge(int u,int v) {
        edges.push_back(Node(u, v));
        G[u].push_back(edges.size() - 1);
    }
    
    void dfs(int x) {
        int len = G[x].size();
        for(int i = 0; i < len; ++i) {
            if(!vis[G[x][i]]) {
                vis[G[x][i]] = 1;
                dfs(edges[G[x][i]].to);
                ans.push_back(edges[G[x][i]].to);
            }
        }
    }
    
    int main() {
        scanf("%d%d", &N, &M);
        for (int i = 0; i < M; ++i) {
            int t1, t2;
            scanf("%d%d", &t1, &t2);
            addedge(t1, t2);
            addedge(t2, t1);
        }
        dfs(1);
        int len = ans.size();
        for(int i = 0; i < len; ++i)
            printf("%d
    ", ans[i]);
        printf("1
    ");
        return 0;
    }
    View Code
  • 相关阅读:
    重装系统后texstudio拼写检查不工作
    git bash使用端口转发连接服务器
    YCSB-mapkeeper
    编译thrift外篇-关于默认链接包-(使用mapkeeper运行leveldb成功)
    编译Thrift
    Could not resolve view with name 'sys/login' in servlet with name 'dispatcher'
    Eclipse创建一个Maven Web项目
    Maven安装配置
    使用Maven创建Web应用程序项目
    org.apache.jasper.JasperException: Unable to compile class for JSP:
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/11285674.html
Copyright © 2011-2022 走看看