|
|
back to boardHow to beat time limit Isn't any trick for Prufer code ??? Posted by Badd 22 Apr 2002 20:45 // my solution has got a time limit below is my code // ------------------- #include <stdio.h> #include <stdlib.h> #include <string.h> #define maxn 7503 typedef struct ttnode *tnode; struct ttnode { long w; tnode next; }; tnode list[maxn],first[maxn]; long w,n,min,m,l=0,l2,deg[maxn],dat[maxn],used [maxn],top,ptemp,ntemp; long index_prev[maxn],index_next[maxn]; int add(int w1,int w2) { tnode now=(tnode)malloc(sizeof(struct ttnode)); long temp; now->w=w2; now->next=first[w1]; first[w1]=now; while (now->next !=NULL && (now->next)->w < (now->w) ) { temp=now->w; now->w=(now->next)->w; (now->next)->w=temp; now=now->next; } return 0; } int main() { tnode now; index_next[0]=1; for (l=0; l<maxn; l++) { deg[l]=0; used[l]=0; } l=0; while ( scanf("%ld",&w) != EOF ) { l++; dat[l]=w; deg[w]++; } n=l+1; for (l=1; l<=n; l++) { index_prev[l]=l-1; index_next[l]=l+1; } for (l=1; l<=n; l++) list[l]=NULL; for (l=1; l<=n; l++) deg[l]++; for (l=1; l<=n-1; l++) { l2=index_next[0]; while (l2<n+1) { if (deg[l2]==1) { m=l2; break; } l2=index_next[l2]; } ntemp=index_next[m]; ptemp=index_prev[m]; index_prev[ ntemp ]=ptemp; index_next[ ptemp ]=ntemp; deg[m]--; deg[ dat[l] ]--; add(m,dat[l]); add(dat[l],m); } for (l=1; l<=n; l++) { printf("%ld: ",l); now=first[l]; while (now!=NULL) { printf("%ld ",now->w); now=now- >next; } printf("\n"); } return 0; } Use heaps (-) Posted by WAZZAP 23 Apr 2002 14:31 |
|
|