Cod sursa(job #101928)

Utilizator cos_minBondane Cosmin cos_min Data 13 noiembrie 2007 22:09:28
Problema Zvon Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 3.34 kb
#include <stdio.h>
#include <fstream>
#include <vector>
#include <set>
using namespace std;

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

int T, N, M, Q;
int gasit, size=1, act_size=1, k;
int Vec[dim], list[2*dim];
bool Sel[dim], Sel2[dim];

vector< vector<int> > L, L2;

struct Functor {
       bool operator() (int i, int j)
       {
            return Vec[i] >= Vec[j];
       }
};

set<int, Functor> S;
set<int, Functor>::iterator it;

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

int main()
{
    int X, Y;
    int maxim, poz;
    freopen(in,"r",stdin);
    freopen(out,"w",stdout);
    
    scanf("%d", &T);
    for ( ; T > 0; T-- )
    {
        scanf("%d", &N);
        
        memset(Sel,0,sizeof(Sel));
        memset(Vec,0,sizeof(Vec));
        memset(Sel2,0,sizeof(Sel2));
        
        L.clear(); L2.clear();
        L.resize(N+1); L2.resize(N+1);
        
        if ( N == 1 ) 
        {
             printf("0\n");
             continue;
        }
        
        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++ )
        {
            S.clear();
            
            for ( int j = 0; j < L[i].size(); j++ ) 
            {
                S.insert(L[i][j]);
            }
            
            for ( it = S.begin(); it != S.end(); it++ )
            {
                L2[i].push_back(*it);
            }
        }
        /*
        for ( int i = 1; i <= N; i++, printf("\n") )
        {
            printf("%d : ", i );
            for ( int j = 0; j < L2[i].size(); j++ )
                printf("%d ", L2[i][j]); 
        }*/
        
        
        //memset(Sel,0,sizeof(Sel));
        Q = 0;
        
        Solve(1,0);
        
        printf("%d\n", Q);
    }
}

void Solve(int nod, int timp)
{
     Sel2[nod] = 1;
     
     if ( Q < timp ) Q = timp;
     
    // printf("%d ", nod);
     
     for ( int i = 0; i < L[nod].size(); i++ )
     {
          if ( !Sel2[L[nod][i]] ) Solve(L[nod][i],timp+i+1);
     }
}

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

/* while ( M )
        {
              for ( int i = 1; i <= act_size; i++ )
              {
                  maxim = -1;
                  poz = -1;
                  k = list[i];
                  
                  if ( G[k] == 1 ) continue;
                  
                  for ( int j = 0; j < L[k].size(); j++ )
                  {
                      if ( !Sel[L[k][j]] ) // nu il am in lista
                         if ( maxim < Vec[L[k][j]] ) maxim = Vec[L[k][j]], poz = L[k][j]; // are numar maxim de subordonati ?
                  }
                  
                  if ( poz == -1 ) M -= 1, G[k] = 1; 
                  else if ( poz == 0 ) Sel[poz] = 1;      
                  else size++, list[size] = poz, Sel[poz] = 1;
              }
              
              act_size = size;
              
              if ( M > 0 ) Q++;
        }*/