Cod sursa(job #152215)

Utilizator andrei_infoMirestean Andrei andrei_info Data 9 martie 2008 11:06:54
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <vector>

using namespace std;

#define MAX 100000

vector < int > G[MAX], tmp[MAX];
int t[MAX], N,T;

int cmp(const int& a,const int& b)
{
    return a>b;
}

void df( int nod )
{
    for ( vector <int> :: iterator it = G[nod].begin(); it!=G[nod].end(); it++)
    {
        df( *it );
        tmp[nod].push_back(t[*it]);
    }
    sort(tmp[nod].begin(), tmp[nod].end(), cmp);
    t[nod] = 0;
    int cnt = 1;
    for ( vector <int> :: iterator it = tmp[nod].begin(); it!=tmp[nod].end(); it++, cnt++)
    {
        if ( *it + cnt > t[nod])
            t[nod] = *it + cnt;
    };
};

int main()
{
    ifstream fin("zvon.in");
    ofstream fout("zvon.out");

    fin>>T;
    for ( ; T>0; T--)
    {
        fin>>N;
        for (int i  = 1; i<=N; i++)
             G[i].clear(), tmp[i].clear();
        for ( int i = 1; i<N; i++)
        {
            int a,b;
            fin>>a>>b;
            G[a].push_back(b);
        }
        t[1] = 0;
        df(1);
        fout<<t[1]<<"\n";
    }
    return 0;
}