Cod sursa(job #2780697)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 7 octombrie 2021 18:30:06
Problema Zvon Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <bits/stdc++.h>
#define DIM 100005

using namespace std;

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

int t, n, ans[DIM];
vector <int> edges[DIM];

bool cmp(int x, int y)
{
    return ans[x] > ans[y];
}

void dfs(int nod)
{
    int niv = 1;

    for(auto k : edges[nod])
        dfs(k);

    sort(edges[nod].begin(), edges[nod].end(), cmp);
    for(auto k : edges[nod])
    {
        ans[nod] = max(ans[nod], niv + ans[k]);
        niv++;
    }
}

int main()
{
    f >> t;

    for(; t; t--)
    {
        f >> n;
        for(int i = 1; i < n; i++)
        {
            int x, y;
            f >> x >> y;
            edges[x].push_back(y);
        }

        dfs(1);
        g << ans[1] << "\n";

        for(int i = 1; i <= n; i++)
            edges[i].clear(), ans[i] = 0;
    }



    return 0;
}