Cod sursa(job #1425000)

Utilizator RathebaSerbanescu Andrei Victor Ratheba Data 26 aprilie 2015 11:21:10
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <fstream>
#include <vector>
#include <algorithm>

#define MAX 100004

using namespace std;

ifstream f("zvon.in");
ofstream g("zvon.out");


vector<int> arb[MAX];
int dist[MAX], n;

bool comp(int a, int b)
{
    return dist[a] > dist[b];
}

void dfs(int sef)
{
    int i;
    for(i=0; i<arb[sef].size(); i++)
        dfs(arb[sef][i]);
    sort(arb[sef].begin(), arb[sef].end(), comp);
    for(i=0; i<arb[sef].size(); i++)
        dist[sef] = max(dist[sef], dist[arb[sef][i]] + i + 1);
}

int main()
{
    int i, t, a, b;
    f>>t;
    while(t--)
    {
        f>>n;
        for(i=1; i<=n; i++)
        {
            arb[i].clear();
            dist[i] = 0;
        }
        for(i=1; i<n; i++)
        {
            f>>a>>b;
            arb[a].push_back(b);
        }
        dfs(1);
        g<<dist[1]<<'\n';
    }
}