Cod sursa(job #2958954)

Utilizator tomaionutIDorando tomaionut Data 29 decembrie 2022 13:24:19
Problema Zvon Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <bits/stdc++.h>

using namespace std;

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

int n;
vector <int> a[100005];
int t[100005];

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

void Dfs(int x)
{
    for (auto w : a[x])
        Dfs(w);

    sort(a[x].begin(), a[x].end(), cmp);

    int k = 1;
    for (auto w : a[x])
    {
        t[x] = max(t[x], t[w] + k);
        k++;
    }
}

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

    Dfs(1);
    fout << t[1] << "\n";

    for (i = 1; i <= n; i++)
    {
        a[i].clear();
        t[i] = 0;
    }
}

int main()
{
    int t;
    fin >> t;
    while (t--)
        Test_Case();
    
    return 0;
}