Cod sursa(job #587793)

Utilizator MKLOLDragos Ristache MKLOL Data 5 mai 2011 21:33:48
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include<algorithm>
#include<vector>
#include<stdio.h>

#define pb push_back

using namespace std;

vector<int> g[101010];
int best[101010],T;
int x,y,N;
bool cmp(int a,int b)
{
    return best[a]>best[b];
}
void df(int x)
{
//printf("%d",x);
int ok=1;
    for(int i=0;i<g[x].size();++i)
    {
        ok=0;
        df(g[x][i]);
    }
    if(ok)
    {
        best[x]=0;
    }
    else
    {
    sort(g[x].begin(),g[x].end(),cmp);
    int timp=1;
    for(int i=0;i<g[x].size();++i)
    {
        if(best[g[x][i]]+timp>best[x])
            best[x]=best[g[x][i]]+timp;
            ++timp;

    }
    }


}

int main()
{
freopen("zvon.in","r",stdin);
freopen("zvon.out","w",stdout);
        scanf("%d",&T);


        while(T--)
        {
            scanf("%d",&N);
            for(int i=1;i<N;++i)
            {
                scanf("%d%d",&x,&y);
                g[x].pb(y);
            }
            df(1);
            printf("%d\n",best[1]);
            for(int i=1;i<=N;++i)
            {
                best[i]=0;
                g[i].clear();
            }
        }

return 0;
}