很简单的一道求欧拉路的题。用矩阵的话最好用关联矩阵,因为可能两点间连好几条路,用vector存的话记着要sort
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#include<stack>
using namespace std;
int map[46][2000],d[46],m;
bool vis[2000];
stack<int>st;
int max(int a,int b){
return a>b?a:b;
}
void euler(int u){
int i;
for(i=1;i<=m;i++)
if(map[u][i] && !vis[i]){
vis[i]=1;
euler(map[u][i]);
st.push(i);
}
}
int main(){
int x,y,z,i,j;
while(scanf("%d %d",&x,&y) && !(x==0 && y==0)){
m=0;
memset(map,0,sizeof(map));
memset(d,0,sizeof(d));
memset(vis,0,sizeof(vis));
scanf("%d",&z);
d[x]++,d[y]++;
map[x][z]=y,map[y][z]=x,m=max(m,z);
int first=x>y?y:x;
while(scanf("%d %d",&x,&y) && !(x==0 && y==0)){
d[x]++,d[y]++;
scanf("%d",&z);
m=max(m,z);
map[x][z]=y,map[y][z]=x;
}
for(i=1;i<=45;i++)
if(d[i]%2)
break;
if(i<=45){
printf("Round trip does not exist.\n");
}
else{
euler(first);
bool f=1;
while(!st.empty()){
if(f){
printf("%d",st.top());
f=0;
}
else
printf(" %d",st.top());
st.pop();
}
printf("\n");
}
}
}