zoukankan      html  css  js  c++  java
  • POJ 2230 Watchcow(有向图欧拉回路)

    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开始每个田经过2次的欧拉回路

    题解

    这里从1开始直接dfs(1),因为是欧拉回路,所以一定能走回到1,这里经过两次直接看成有向图

    然后这里n<=1e4,开1e4*1e4肯定爆了,只能看m<=5e4,所以可以用vector<int> Map[10001];

    代码

     1 #include<cstdio>
     2 #include<vector>
     3 using namespace std;
     4 vector<int> Map[10001];//这里Map[u][i]=v就是从u能走到v
     5 int n,m;
     6 void dfs(int u)
     7 {
     8     int l=Map[u].size();
     9     for(int i=0;i<l;i++)
    10     {
    11         int v=Map[u][i];
    12         if(v)
    13         {
    14             Map[u][i]=0;
    15             dfs(v);
    16         }
    17     }
    18     printf("%d
    ",u);//输出路径后序输出
    19 }
    20 int main()
    21 {
    22     int u,v;
    23     scanf("%d%d",&n,&m);
    24     for(int i=0;i<m;i++)
    25     {
    26         scanf("%d%d",&u,&v);
    27         Map[u].push_back(v);
    28         Map[v].push_back(u);
    29     }
    30     dfs(1);//从1开始
    31     return 0;
    32 }
  • 相关阅读:
    C#文件操作
    C#Web编程
    WMsg参数常量值
    IIS管理网站浏览
    课程视频网址
    CSS 学习质料
    Centos镜像使用帮助
    apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104))
    How to Configure Nginx for Optimized Performance
    luarocks install with lua5.1 and luajit to install lapis
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/8481200.html
Copyright © 2011-2022 走看看