zoukankan      html  css  js  c++  java
  • poj 2230 Watchcow (欧拉回路的应用)

    http://poj.org/problem?id=2230

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

    对路径进行双向处理之后,直接进行欧拉回路的DFS搜索

    代码:

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<stdio.h>
     4 #include<string.h>
     5 #include<math.h>
     6 #include<queue>
     7 #include<stack>
     8 using namespace std;
     9 #define maxn 2*50000+10
    10 #define maxv 10000+10
    11 
    12 struct Nod
    13 {
    14     int next,to;
    15 }node[maxn];
    16 
    17 int used[maxn],adj[maxv];
    18 
    19 void init()
    20 {
    21     memset(used,0,sizeof(used));
    22     memset(adj,-1,sizeof(adj));
    23 }
    24 
    25 void Eular(int idex)
    26 {
    27     int i;
    28     for(i=adj[idex];i!=-1;i=node[i].next)
    29     {
    30         if(!used[i])
    31         {
    32             used[i]=1;
    33             Eular(node[i].to);
    34         }
    35     }
    36     printf("%d\n",idex);
    37 }
    38 
    39 
    40 int main()
    41 {
    42     int n,m;
    43     while(~scanf("%d%d",&n,&m))
    44     {
    45         init();
    46         int i,u,v;
    47         for(i=0;i<m;i++)
    48         {
    49             scanf("%d%d",&u,&v);
    50             //u->v
    51             node[i<<1].to=v;
    52             node[i<<1].next=adj[u];
    53             adj[u]=i<<1;
    54             //v->u
    55             node[i*2+1].to=u;
    56             node[i*2+1].next=adj[v];
    57             adj[v]=i*2+1;
    58         }
    59         Eular(1);
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    用户控件的缓存技术之二【共三篇】
    .NET获取URL的各种方式及其区别
    图片上传封装类【包括图片上传和缩略图上传】.NET
    .NET抓取数据范例 抓取页面上所有的链接
    JQuery基础 学习的一些例子以及手册
    呵呵呵呵。。。系统还原了,终于可以用IE登陆百度了
    不用框架使用ajax 纯js使用ajax post,get范例及其区别
    用ashx还是aspx写ajax响应
    repeater绑定数组、哈希表、字典 ArrayList/HashTable,Dictionary为datasource
    Access数据库访问类 帮助类
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3134750.html
Copyright © 2011-2022 走看看