Cod sursa(job #2246964)

Utilizator alex2209alexPavel Alexandru alex2209alex Data 27 septembrie 2018 18:55:19
Problema Zvon Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>
#include <algorithm>
#include <queue>
using namespace std;
ifstream f("zvon.in");
ofstream g("zvon.out");
int n,i,ma,s,t,a,b,v2[1010001],nr;
queue<int>v[100001];
int rezolva(int poz)
{
    int in=nr;
    int sf=in;
    if(v[poz].size()==0)
    {
        return 0;
    }
    nr=in+v[poz].size()+1;
    while(!v[poz].empty())
    {
        v2[sf]=rezolva(v[poz].front());
        sf++;
        v[poz].pop();
    }
    sort(v2+in,v2+sf-1);
    ma=0;
    s=0;
    for(i=sf-1;i>=in;i--)
    {
        s++;
        ma=max(ma,s+v2[i]);
    }
    return ma;
}
int main()
{
    f>>t;
    while(t--)
    {
        f>>n;
        nr=1;
        for(i=1;i<n;i++)
        {
            f>>a>>b;
            v[a].push(b);
        }
        g<<rezolva(1)<<'\n';
    }
    g<<" ";
    return 0;
}