Cod sursa(job #1597354)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 11 februarie 2016 21:58:23
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <cstdio>
#define MAXN 100000
int v[MAXN],next[MAXN],ind[MAXN+1],timp[MAXN+1],F[MAXN];
inline void swap(int b,int e,int *x){
    int aux=x[b];
    x[b]=x[e];
    x[e]=aux;
}
void myqsort(int begin,int end){
    int b=begin,e=end,pivot=F[(b+e)/2];
    while(b<=e){
        while(F[b]<pivot) b++;
        while(F[e]>pivot) e--;
        if(b<=e){
            swap(b,e,F);
            b++;e--;
        }
    }
    if(begin<e) myqsort(begin,e);
    if(b<end) myqsort(b,end);
}
int DFS(int nod,int n){
    int max,i,poz,con,n1;
    if(ind[nod]==0)
       return 0;
    else{
        if(timp[nod]==0){
          poz=ind[nod];
          n1=0;
          while(poz>0){
             F[n+n1]=DFS(v[poz],n+n1+1);
              n1++;
              poz=next[poz];
          }
          myqsort(n,n+n1-1);
          max=0;
          con=1;
          for(i=n+n1-1;i>=n;i--){
            if(F[i]+con>max)
               max=F[i]+con;
            con++;
          }
          timp[nod]=max;
        }
        return timp[nod];
    }
}
int main(){
    FILE*fi,*fout;
    int t,n,i,max,x,y;
    fi=fopen("zvon.in" ,"r");
    fout=fopen("zvon.out" ,"w");
    fscanf(fi,"%d" ,&t);
    while(t>0){
         t--;
         fscanf(fi,"%d" ,&n);
         for(i=1;i<n;i++){
             fscanf(fi,"%d%d" ,&x,&y);
             next[i]=ind[x];
             ind[x]=i;
             v[i]=y;
         }
         fprintf(fout,"%d\n" ,DFS(1,0));
         for(i=1;i<=n;i++)
             timp[i]=ind[i]=0;
    }
    fclose(fi);
    fclose(fout);
    return 0;
}