• USACO3.3.1Riding the Fences


    Riding the Fences

    Farmer John owns a large number of fences that must be repaired annually. He traverses the fences by riding a horse along each and every one of them (and nowhere else) and fixing the broken parts.

    Farmer John is as lazy as the next farmer and hates to ride the same fence twice. Your program must read in a description of a network of fences and tell Farmer John a path to traverse each fence length exactly once, if possible. Farmer J can, if he wishes, start and finish at any fence intersection.

    Every fence connects two fence intersections, which are numbered inclusively from 1 through 500 (though some farms have far fewer than 500 intersections). Any number of fences (>=1) can meet at a fence intersection. It is always possible to ride from any fence to any other fence (i.e., all fences are "connected").

    Your program must output the path of intersections that, if interpreted as a base 500 number, would have the smallest magnitude.

    There will always be at least one solution for each set of input data supplied to your program for testing.

    PROGRAM NAME: fence

    INPUT FORMAT

    Line 1: The number of fences, F (1 <= F <= 1024)
    Line 2..F+1: A pair of integers (1 <= i,j <= 500) that tell which pair of intersections this fence connects.

    SAMPLE INPUT (file fence.in)

    9
    1 2
    2 3
    3 4
    4 2
    4 5
    2 5
    5 6
    5 7
    4 6
    

    OUTPUT FORMAT

    The output consists of F+1 lines, each containing a single integer. Print the number of the starting intersection on the first line, the next intersection's number on the next line, and so on, until the final intersection on the last line. There might be many possible answers to any given input set, but only one is ordered correctly.

    SAMPLE OUTPUT (file fence.out)

    1
    2
    3
    4
    2
    5
    4
    6
    5
    7
    题解:很明显,就是求欧拉通路或者欧拉回路。
    由于之前没遇见过这个问题,所以看了一下《离散数学及其应用》关于欧拉回路的部分。以下内容摘自《离散数学及其应用》
    定理1:连通多重图具有欧拉回路当且仅当它的每个顶点都有偶度数。
    定理2:连通多重图具有欧拉通路但无欧拉回路当且仅当它恰好有2个奇度数顶点。
    再看了下IOI2007年仇荣琦的论文《欧拉回路性质与应用探究》

     求欧拉图G的欧拉回路的算法:

    1 在图G中任意找一个回路C;

    2 将图G中属于回路的边删除;

    3 在残留图的各极大连通子图中分别寻找欧拉回路;

    4 将各极大连通子图的欧拉回路合并到C中得到图G的欧拉回路。

    该算法的伪代码如下:

    Procedure Euler-circuit (start);

    Begin

      For 顶点start的每个邻接点v Do

      If 边(start,v)未被标记 Then 

        Begin 

          将边(start,v)作上标记;

          将边(v,start)作上标记;                   

          Euler-circuit (v);

        End;

    将边(start,v)加入栈;

    End;

    最后依次取出栈S每一条边而得到图的欧拉回路。

    由于该算法执行过程中每条边最多访问两次,因此该算法的时间复杂度为O(|E|)。

    在实际应用中,以上的实现可能导致堆栈溢出(递归的深度最多可以达到|E|层),因此常常需要将其改造成非递归的形式。

    View Code
     1 /*
     2 ID:spcjv51
     3 PROG:fence
     4 LANG:C
     5 */
     6 #include<stdio.h>
     7 #include<string.h>
     8 #define MAXS 500
     9 int graph[MAXS+5][MAXS+5];
    10 int stack[2*MAXS+5],top;
    11 void Euler(int start)
    12 {
    13     int i;
    14     if(graph[start][0]>0)
    15     {
    16         for(i=1; i<=MAXS; i++)
    17             if(graph[start][i]>0)
    18             {
    19                 graph[start][0]--;
    20                 graph[i][0]--;
    21                 graph[start][i]--;
    22                 graph[i][start]--;
    23                 Euler(i);
    24             }
    25     }
    26     stack[top++]=start;
    27 }
    28 int main(void)
    29 {
    30     freopen("fence.in","r",stdin);
    31     freopen("fence.out","w",stdout);
    32     int n,i,x,y;
    33     scanf("%d",&n);
    34     for(i=1; i<=n; i++)
    35     {
    36         scanf("%d%d",&x,&y);
    37         graph[x][y]++;
    38         graph[y][x]++;
    39         graph[x][0]++;
    40         graph[y][0]++;
    41     }
    42     top=1;
    43     for(i=1; i<=MAXS; i++)
    44         if(graph[i][0]%2==1)
    45         {
    46             Euler(i);
    47             break;
    48         }
    49     if(i>MAXS)
    50         Euler(1);
    51     for(i=top-1; i>=1; i--)
    52         printf("%d\n",stack[i]);
    53     return 0;
    54 
    55 }
  • 相关阅读:
    灌注和宝石性道法价比分析
    bzoj1912
    bzoj3504
    poj3580
    bzoj1251
    bzoj3223
    bzoj1212
    bzoj3790
    记一次惨痛的比赛
    bzoj2734
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2937220.html
Copyright © 2020-2023  润新知