Cod sursa(job #106182)

Utilizator cos_minBondane Cosmin cos_min Data 18 noiembrie 2007 14:00:14
Problema Zvon Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 1.67 kb
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

#define in "zvon.in"
#define out "zvon.out"
#define dim 100067

int T, N, Q;
int Vec[dim];
bool Sel[dim], Selq[dim];
vector< vector<int> > L;

struct Compare {
       bool operator() (int i, int j)
       {
            if ( Vec[i] < Vec[j] ) return 0;
            return 1;
       }
};

void DF(int);
void Solve(int,int);

int main()
{
    int X, Y;
    
    freopen(in,"r",stdin);
    freopen(out,"w",stdout);
    
    scanf("%d", &T);
    for ( ; T > 0; T-- )
    {
        scanf("%d", &N);
        L.clear(); L.resize(N+1);
        
        memset(Sel,0,sizeof(Sel)); memset(Selq,0,sizeof(Selq)); memset(Vec,0,sizeof(Vec));
        
        for ( int i = 1; i < N; i++ )
        {
            scanf("%d%d", &X, &Y);
            L[X].push_back(Y);
        }
        
        DF(1);
        
        for ( int i = 1; i <= N; i++ )
            sort( L[i].begin(), L[i].end(), Compare() );
        
        Q = 0;
        Solve(1,0);
        
        printf("%d\n", Q);
    }
}

void DF(int nod)
{
     Sel[nod] = 1;
     
     if ( L[nod].size() == 0 ) Vec[nod] = 0; 
     
     for ( int i = 0; i < L[nod].size(); i++ )
     {
         int j = L[nod][i];
         if ( !Sel[j] )
         {
              DF(j);
              
              Vec[nod] = Vec[nod] + Vec[j] + 1;
         }
     }
}

void Solve(int nod, int timp)
{
     Selq[nod] = 1;
     
     if ( Q < timp ) Q = timp;
     
     for ( int i = 0; i < L[nod].size(); i++ )
     {
         int j = L[nod][i];
         
         if ( !Selq[j] ) Solve(j,timp+1+i);
     }
}