Cod sursa(job #1456380)

Utilizator cristian.caldareaCaldarea Cristian Daniel cristian.caldarea Data 30 iunie 2015 14:35:32
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <fstream>
#include <set>
#include <vector>
#include <cstring>

using namespace std;

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

const int Nmax = 100002;

int t, n;
vector<int> G[Nmax];


int Dfs(int x);


int main()
{
    fin >> t;
    while (t--)
    {

        for (int i = 1; i <= n; i++)
            G[i].clear();
        fin >> n;
        for (int i = 1, x, y; i < n; i++)
        {
            fin >> x >> y;
            G[x].push_back(y);
        }

        fout << Dfs(1) << '\n';
    }

    return 0;
}


int Dfs(int x)
{
    multiset <int> q;

    for ( auto y : G[x] )
    {
        q.insert(Dfs(y));
    }

    int z = 0, cnt = 1;
    for (set < int > :: reverse_iterator it = q.rbegin(); it != q.rend(); it++)
    {
        z = max (z, *it + cnt);
        cnt++;
    }

    return z;
}