zoukankan      html  css  js  c++  java
  • fluery算法

     1 #include<stdio.h>
     2 #include<string.h>
     3 struct stack
     4 {
     5     int top;
     6     int node[100];
     7 }s;
     8 int n,map[100][100];
     9 void dfs(int x)
    10 {
    11     int i,j;
    12     s.top++;
    13     s.node[s.top]=x;
    14     for(i=0;i<n;i++)
    15     {
    16         if(map[i][x])
    17         {
    18             map[i][x]=map[x][i]=0;
    19             dfs(i);
    20             break;
    21         }
    22     }
    23 }
    24 void fleury(int start)
    25 {
    26     int i,j;
    27     s.top=0;s.node[s.top]=start;
    28     while(s.top>=0)
    29     {
    30         int flag=0;
    31         for(i=0;i<n;i++)
    32         {
    33             if(map[s.node[s.top]][i])
    34             {
    35                 flag=1;break;
    36             }
    37         }
    38         if(!flag)
    39         {
    40             printf("%d ",s.node[s.top]+1);
    41             s.top--;
    42         }
    43         else
    44         {
    45             s.top--;
    46             dfs(s.node[s.top+1]);
    47         }
    48     }
    49 }
    50 int main()
    51 {
    52     int i,j,m,dgree;
    53     while(scanf("%d%d",&n,&m)!=EOF)
    54     {
    55         memset(map,0,sizeof(map));
    56         for(i=0;i<m;i++)
    57         {
    58             int s,t;
    59             scanf("%d%d",&s,&t);
    60             map[s-1][t-1]=map[t-1][s-1]=1;
    61         }
    62         int num=0,start=0;
    63         for(i=0;i<n;i++)
    64         {
    65             dgree=0;
    66             for(j=0;j<n;j++)
    67             {
    68                 if(map[i][j])
    69                     dgree++;
    70             }
    71             if(dgree%2)
    72             {
    73                 num++;
    74                 start=i;
    75             }
    76         }
    77         if(num==0||num==2)
    78             fleury(start);
    79         else printf("NO
    ");
    80     }
    81     return 0;
    82 }
    83 /*
    84 9 14
    85 1 2
    86 1 8
    87 2 3
    88 2 8
    89 2 9
    90 3 4
    91 4 5
    92 4 6
    93 4 9
    94 5 6
    95 6 7
    96 6 9
    97 7 8
    98 8 9
    99 */
  • 相关阅读:
    Tips for Hoops 3D & ACIS
    把书读薄TICPP(2)
    Software Toolbox EasyOPC简介
    Wonderware InSQL and Incurity安装心得
    Linux root password reset
    SQL Server 2005 的搞笑
    SVG 简介
    M0n0wall 是什么?
    Solaris 上调试系统 hang 的总结
    DDNS简介
  • 原文地址:https://www.cnblogs.com/sweat123/p/4506948.html
Copyright © 2011-2022 走看看