• COJ1184格林布的地雷


    求欧拉道路的题,可能是回路,也可能不是回路,若存在奇度点,则应从奇度点开始找路径,为保证最后的路径是字典序最小的,找路径时必须遵循小结点优先的原则。

    View Code
     1 #include <stdio.h>
     2 #include <string.h>
     3 #define MAX(a,b) ((a)>(b)?(a):(b))
     4 #define N 501
     5 int n,m,g[N][N],d[N],path[N],top;
     6 void dfs(int u)
     7 {
     8     int v;
     9     for(v=1;v<=n;v++)
    10     {
    11         if(g[u][v])
    12         {
    13             g[u][v]--;
    14             g[v][u]--;
    15             dfs(v);
    16         }
    17     }
    18     path[top++]=u;
    19 }
    20 int main()
    21 {
    22     int i,a,b,start;
    23     while(~scanf("%d",&m))
    24     {
    25         memset(g,0,sizeof(g));
    26         memset(d,0,sizeof(d));
    27         n=0;
    28         for(i=0;i<m;i++)
    29         {
    30             scanf("%d%d",&a,&b);
    31             g[a][b]++;
    32             g[b][a]++;
    33             d[a]++;
    34             d[b]++;
    35             n=MAX(n,MAX(a,b));
    36         }
    37         for(start=1;d[start]==0 && start<=n;start++);
    38         for(i=start;(d[i]&1)==0 && i<=n;i++);
    39         if(i<=n)    start=i;
    40         top=0;
    41         dfs(start);
    42         for(i=top-1;i>=0;i--)   printf("%d\n",path[i]);
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    【Python之路Day1】基础篇
    C语言转义字符表和ASCII码表
    strcpy函数学习
    Linux C遇到的常见错误
    静态内存
    指针
    #define学习
    枚举
    搭建Linux C语言开发环境
    centos下php环境安装redis
  • 原文地址:https://www.cnblogs.com/algorithms/p/2582125.html
Copyright © 2020-2023  润新知