poj 1041 Eulerian Circuit

/* source code of submission 363011, Zhongshan University Online Judge System */
/* source code of submission 362793, Zhongshan University Online Judge System */
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;

int n,m,k;
//poj 1041 Eulerian Circuit
//由于本题是SPJ,所以只要输出回路就行
struct node
{
       int adj;
       int edge;
       node (){}
       node ( int a,int e):adj(a),edge(e){}
};

int mark[2000];
vector<node> con[50];
int p[50];
int num;
int ans[2000];
void euler(int v)
{
     int a,e;
     while (p[v]<con[v].size())
     {
         e=con[v][p[v]].edge;
         a=con[v][p[v]].adj;
         p[v]++;
         if (mark[e]) continue;
         mark[e]=1;
         euler(a);
       
        ans[num++]=e;
     }
 
}

int main()
{
    int x,y,e;
    int s;
    while (scanf("%d%d",&x,&y) && x+y)
    {
          for (int i=1;i<=44;i++)
           con[i].clear();
          s=x;
          if (y<x) s=y;
          n=0;m=0;
          n=max(x,n);
          n=max(y,n);
          int rmax=0;
          while (x+y)
          {
          scanf("%d",&e);
          rmax++;
          con[x].push_back(node(y,e));
          con[y].push_back(node(x,e));
          n=max(x,n);n=max(y,n);m=max(m,e);
          scanf("%d%d",&x,&y);
          }
         /* printf("%d %d",n,m);
          for (int i=1;i<=n;i++)
          {printf("/n");
          for (int j=0;j<con[i].size();j++)
          printf("%d ",con[i][j].adj);}*/
          bool flag=true;
          for (int i=1;i<=n;i++)
          if (con[i].size()%2)
          {
                               flag=false;
                               printf("Round trip does not exist./n");
                               break;
          }
         
          if (!flag) continue;
          memset(mark,0,sizeof(mark));
          for (int i=1;i<=n;i++)
                   p[i]=0;
          num=0;
         
          euler(s);  
          if (num<rmax)
                 printf("Round trip does not exist./n");
          else
          {
               printf("%d",ans[num-1]);
               for (int i=num-2;i>=0;i--)
               printf(" %d",ans[i]);
               printf("/n");
               }
              
         
         
         
         
         
    }
    return 0;
   
}

你可能感兴趣的:(poj 1041 Eulerian Circuit)