what's wrong with my code? i use the greedy algorithm #include<stdio.h> #include<memory.h> int map[200][200],mark[200],m,n; void make(int v){ int i; for (i=0;i<n;i++) if (mark[i]==0 && map[v][i]>0) { mark[i]=v+1; map[v][i]=-map[v][i]; map[i][v]=-map[i][v]; make(i); } } void main(){ int i,j,aa,bb,la,lb,a[200],b[200],count,k; scanf("%d%d",&n,&m); memset(map,0,sizeof map); for (i=0;i<m;i++) { scanf("%d%d",&aa,&bb); map[aa-1][bb-1]=map[bb-1][aa-1]=1; } memset(mark,0,sizeof mark); i=0; do{ while (i<n && mark[i]) i++; if (i==n) break; mark[i]=32767; make(i); }while (1); count=0; for (i=0;i<n;i++) for (j=i+1;j<n;j++) if (map[i][j]>0) count++; printf("%d\n",count); for (i=0;i<n;i++) for (j=i+1;j<n;j++) if (map[i][j]>0) { la=0; aa=i; do{ a[la++]=aa; if (mark[aa]==32767) break; aa=mark[aa]-1; }while (1); lb=0; bb=j; do{ b[lb++]=bb; if (mark[bb]==32767) break; bb=mark[bb]-1; }while(1);
while (a[la-1]==b[lb-1]) {la--;lb--;} printf("%d ",la+lb+1); for (k=0;k<la+1;k++) printf("%d ",a[k]+1); for (k=lb-1;k>=0;k--) printf("%d ",b[k] +1); printf("\n"); } } Use DFS + Greedy Method > #include<stdio.h> > #include<memory.h> > int map[200][200],mark[200],m,n; > void make(int v){ > int i; > for (i=0;i<n;i++) > if (mark[i]==0 && map[v][i]>0) { > mark[i]=v+1; > map[v][i]=-map[v][i]; > map[i][v]=-map[i][v]; > make(i); > } > } > void main(){ > int i,j,aa,bb,la,lb,a[200],b[200],count,k; > scanf("%d%d",&n,&m); > memset(map,0,sizeof map); > for (i=0;i<m;i++) { > scanf("%d%d",&aa,&bb); > map[aa-1][bb-1]=map[bb-1][aa-1]=1; > } > memset(mark,0,sizeof mark); > i=0; > do{ > while (i<n && mark[i]) i++; > if (i==n) break; > mark[i]=32767; > make(i); > }while (1); > count=0; > for (i=0;i<n;i++) > for (j=i+1;j<n;j++) if (map[i][j]>0) count++; > printf("%d\n",count); > for (i=0;i<n;i++) > for (j=i+1;j<n;j++) > if (map[i][j]>0) { > la=0; > aa=i; > do{ > a[la++]=aa; > if (mark[aa]==32767) break; > aa=mark[aa]-1; > }while (1); > lb=0; > bb=j; > do{ > b[lb++]=bb; > if (mark[bb]==32767) break; > bb=mark[bb]-1; > }while(1); > > while (a[la-1]==b[lb-1]) {la--;lb--;} > printf("%d ",la+lb+1); > for (k=0;k<la+1;k++) printf("%d ",a[k]+1); > for (k=lb-1;k>=0;k--) printf("%d ",b[k] > +1); > printf("\n"); > } > } |