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 }
  • 相关阅读:
    IBM Minus One(water)
    约瑟夫问题的循环链表实现
    双向链表(差不多)
    单向链表的建立,插入,删除(复习一下)
    找新朋友(欧拉函数)
    验证角谷猜想(hd1279)
    Wolf and Rabbit(gcd)
    Big Number(大数)
    字串数(高精度组合数)
    寻找素数对(hd1262)
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3134750.html
Copyright © 2011-2022 走看看