I know It's a terible code, some idea about using heap let me know the wrong please.. thank you, #include <stdio.h> #include <string.h> #include <stdlib.h> #define maxn 1500 long inp[maxn],top=0,w,c[maxn],p[maxn],htop,outp[maxn]; struct theap { long deg; long i; } h[maxn],temp; struct tnode { long w; struct tnode* next; }; typedef struct tnode* node; node head[maxn],last[maxn]; int sort_function( const void *a, const void *b) { if ( (((struct theap*)a)->deg)!=(((struct theap*)b)->deg) ) return (((struct theap*)a)->deg)-(((struct theap*)b)- >deg); else return (((struct theap*)a)->i)-(((struct theap*)b)->i); } int sort_function2( const void *a, const void *b) { return (*(long*)a)-(*(long*)b); } long min(long a,long b) { if (a<b) return a; return b; } void delheap() { long l; h[1]=h[htop]; h[htop].deg=999999; p[h[1].i]=1; htop--; for (l=1; l*2<=htop; ) { if (min(h[l*2].deg,h[l*2+1].deg)>h [l].deg) break; if (h[l*2].deg<=h[l*2+1].deg) { temp=h[l]; h[l]=h[l*2]; h[l*2]=temp; p[h[l].i]=l; p[h[l*2].i]=l*2; l*=2; }else { temp=h[l]; h[l]=h[l*2+1]; h[l*2+1]=temp; p[h[l].i]=l; p[h[l*2+1].i]=l*2+1; l=l*2+1; } } return; } void add(long w1,long w2) { node newnode=(node)malloc(sizeof(struct tnode)); newnode->w=w2; newnode->next=NULL; if (head[w1]==NULL) { head[w1]=newnode; last[w1] =newnode; } else { last[w1]->next=newnode; last[w1]=newnode; } return; } int main() { long l,l2,k; node nownode; FILE* fname=stdin; FILE* fout=stdout; memset(c,0,sizeof(c)); for (l=0; l<maxn; l++) h[l].deg=999999; while (fscanf(fname,"%ld",&w)!=EOF) { inp[++top]=w; c [w]++; } for (l=1; l<=top+1; l++) { h[l].i=l; h[l].deg=c[l]; head[l] =NULL; } h[0].deg=-1; qsort((void *)h, top+1, sizeof(h[0]), sort_function); htop=top+1; for (l=1; l<=top+1; l++) p[h[l].i]=l; for (l=1; l<=top; l++) { add(inp[l],h[1].i); add(h[1].i,inp[l]); delheap(); h[p[inp[l]]].deg--; for (l2=p[inp[l]]; l2>1; l2/=2) { if (h[l2].deg>h [l2/2].deg) break; if (h[l2].deg==h [l2/2].deg && h[l2/2].i<h[l2].i) break; else {
temp=h[l2]; h[l2] =h[l2/2]; h [l2/2]=temp; p[h [l2].i]=l2; p[h [l2/2].i]=l2/2; } } } for (l=1; l<=top+1; l++) { fprintf(fout,"%ld:",l); l2=0; for (nownode=head[l]; nownode! =NULL; nownode=nownode->next) outp[++l2]=nownode- >w; outp[0]=-1; qsort((void *)outp, l2+1, sizeof(outp[0]), sort_function2); for (k=1; k<=l2; k++) fprintf(fout," % ld",outp[k]); fprintf(fout,"\n"); } return fclose(fname)+fclose(fout); } // 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; } I think my code is right. It's pretty straight forward. Can any one who solved this look at it? --------------------------code------------------------------------ int main(void) { int n,code[SIZE]; int *m[SIZE],c[SIZE]; int f[SIZE]; int i,j,k; n=h=0; while(scanf("%d",&code[n])==1) code[n++]--; for(i=0;i<=n;i++){ f[i]=c[i]=0; m[i]=(int *)malloc((1+f[i])*sizeof(int)); } for(k=0;k<n;k++) f[code[k]]++; for(i=0;i<=n;i++) if(f[i]==0) HeapInsert(i); for(k=0;k<n;k++){ i=code[k]; j=ExtractMin(); m[i][c[i]++]=j; m[j][c[j]++]=i; f[i]--; if(f[i]==0) HeapInsert(i); } for(i=0;i<=n;i++){ printf("%d:",i+1); qsort(m[i],c[i],sizeof(int),comp); for(j=0;j<c[i];j++) printf(" %d",m[i][j]+1); printf("\n"); } return 0; } I can't get ACCEPTED about a half of the year. I think my algorithm is absolutly right, because I can prove it. And, some questions to administarion: 1. Can a line break be in the end of output ? 2. Maybe, the C++ operator "cin >>" doesn't work right in your environment ? 3. Are you sure that your test data is correct ? Thanks. Simple bigraph match will pass it. But I've got the same problem when using cin... It will run forever and finally get a TLE. Sorry, the first line is wrong. I've solved the problem. I think that's easy. I CAN'T GET ACCEPTED A HALF OF THE YEAR. I DON'T KNOW, WHAT I DO WRONG. HERE IS MY SYMPLE C++ PROGRAMM: #include <iostream.h> const Nmax = 8000; // 7500; int N; int a[Nmax]; int i1[Nmax]; int i2[Nmax]; void Solve(); void main() { int i,j; N = 1; do { if (cin.eof()) break; cin >> a[N]; a[N++] --; } while (1); N--; Solve(); int b[Nmax]; int k; for (int ii=0; ii<N; ii++) { cout << ii+1 << ":"; k = 0; for (j=0; j<N-1; j++) { if (i1[j]==ii) b[k++] = i2[j]+1; else if (i2[j]==ii) b[k++] = i1[j]+1; } for (i=0; i<k-1; i++) for (j=i+1; j<k; j++) if (b[i]>b[j]) { int t = b[i]; b[i] = b[j]; b[j] = t; } for (i=0; i<k; i++) cout << " " << b[i]; cout << endl; } } void Solve() { int i,j; int c[Nmax]; for (i=0; i<N; i++) c[i] = 0; c[a[N-1]] = 1; int k = 0; for (i=N-1; i>=0; i--) { if (i>0 && !c[a[i-1]]) { c[a[i-1]] = 1; i1[k] = a[i-1]; i2[k++] = a[i]; } else { for (j=N-1; j>=0; j--) if (!c[j]) { c[j] = 1; i1[k] = j; i2[k++] = a[i]; break; } } } } THANK YOU ! how can i read the pruffer code (when i don'n know it's length) Is there anybody, who knows how to know the end of data block (end of input)? Mail answer to kluchnikovs@mail.ru |
|