Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 6993 | Accepted: 2302 | Special Judge |
Description
Input
Output
Sample Input
1 2 1 2 3 2 3 1 6 1 2 5 2 3 3 3 1 4 0 0 1 2 1 2 3 2 1 3 3 2 4 4 0 0 0 0
Sample Output
1 2 3 5 4 6 Round trip does not exist.题意:无向图,要经过每一条路线仅且一次,并且回到原点,起始点是编号最小的点;
首先判断是否联通
其次判断该连通图是否满足欧拉回路的条件,即所有点的度为偶数
然后用dfs判定
题目还要求输出路径的编号要求字典序最小,先把编号从大到小排列即可
程序:
#include"stdio.h" #include"string.h" #include"queue" #include"stack" #include"iostream" #include"string" #include"map" #include"stdlib.h" #define inf 99999999 #define M 1009 using namespace std; struct st { int u,v,w,next,vis,id; }edge[M*6],Stack[M*6]; int t,top,head[M],out[M],in[M],dis[M],s[M],cnt,use[M]; struct node { int a,b,c; }p[M*6]; int cmp(const void *a,const void *b) { return (*(struct node*)b).c-(*(struct node*)a).c; } void init() { t=0; memset(head,-1,sizeof(head)); memset(edge,0,sizeof(edge)); } void add(int u,int v,int w) { edge[t].u=u; edge[t].v=v; edge[t].w=w; edge[t].next=head[u]; head[u]=t++; } /*void dfs(int u)//递归写法 { for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!edge[i].vis) { edge[i].vis=edge[i^1].vis=1; dfs(v); s[cnt++]=edge[i].w; } } }*/ void dfs(int u)//非递归写法 { int i ; top=0; Stack[top].u=u; Stack[top].id=head[u]; top++; while(top>0) { st x=Stack[top-1]; for(i=head[x.u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!edge[i].vis) { edge[i].vis=edge[i^1].vis=1; Stack[top].u=v; Stack[top].id=i; top++; break; } } if(i==-1) { //printf("%d ",x.u); if(top-1>0) s[cnt++]=edge[x.id].w; top--; } } } int main() { int A,B,C,i,j; while(scanf("%d%d",&A,&B),A||B) { int m=0; int start=inf; memset(use,0,sizeof(use)); init(); scanf("%d",&C); p[m].a=A; p[m].b=B; p[m++].c=C; start=min(start,A); start=min(start,B); use[A]=use[B]=1; while(scanf("%d%d",&A,&B),A||B) { scanf("%d",&C); p[m].a=A; p[m].b=B; p[m++].c=C; start=min(start,A); start=min(start,B); use[A]=use[B]=1; } qsort(p,m,sizeof(p[0]),cmp); for(i=0;i<m;i++) { add(p[i].a,p[i].b,p[i].c); add(p[i].b,p[i].a,p[i].c); } int num=0,degree; for(i=1;i<=44;i++) { if(use[i]) { degree=0; for(j=head[i];j!=-1;j=edge[j].next) { degree++; } if(degree&1) num++; } } if(num==0) { cnt=0; dfs(start); for(i=cnt-1;i>=0;i--) { if(i==cnt-1) printf("%d",s[i]); else printf(" %d",s[i]); } printf("\n"); } else printf("Round trip does not exist.\n"); } }