• 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
  • 相关阅读:
    android 第三方开源库 学习汇总之Butter Knife
    android Gradle下载慢,使用阿里镜像
    android 第三方开源库 学习汇总
    <Android Studio> 4.Adapter的那些事 <一>
    <Android Studio> 3.打包APK
    <Android Studio> 2.APP开机启动
    <Android Studio> 1.如何APP配置权限
    Android源码分析(十七)----init.rc文件添加脚本代码
    Android源码分析(十六)----adb shell 命令进行OTA升级
    Android源码分析(十五)----GPS冷启动实现原理分析
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/11285674.html
Copyright © 2020-2023  润新知