Cod sursa(job #286098)

Utilizator razvan2006razvan brezulianu razvan2006 Data 23 martie 2009 14:32:56
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
 #include <cstdio>  
 #include <algorithm>  
   
 using namespace std;  
   
 int t,n,a,b;  
 struct nod  
 {  
     int inf;  
     nod *urm;  
 } *g[100100];  
   
 void baga(int x, int y)  
 {  
     nod *p=new nod;  
     p->inf=y;  
     p->urm=g[x];  
     g[x]=p;  
 }  
   
 int timp(int x)  
 {  
     if(!g[x])  
         return 0;  
     else  
     {  
         int l[n],nr=0;  
         for(nod *i=g[x];i;i=i->urm)  
             l[nr++]=timp(i->inf);  
         sort(l,l+nr);  
         int max=0;  
         for(int j=0;j<nr;j++)  
             if((l[j]+nr-j)>max)  
                 max=l[j]+nr-j;  
         return max;  
     }  
 }  
   
 int main()  
 {  
     freopen("zvon.in","r",stdin);  
     freopen("zvon.out","w",stdout);  
     scanf("%d",&t);  
     for(int i=0;i<t;i++)  
     {  
         scanf("%d",&n);  
         if(n==1)  
             printf("0\n");  
         else  
         {  
             for(int j=0;j<n-1;j++)  
             {  
                 scanf("%d%d",&a,&b);  
                 baga(a,b);  
             }  
             printf("%d\n",timp(1));  
         }  
         for(int j=0;j<n;j++)  
             g[j]=NULL;  
     }  
     fclose(stdout);  
     return 0;  
}