Cod sursa(job #2245992)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 26 septembrie 2018 12:54:47
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;

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

vector <int> G[Nmax];
int _time[Nmax];
int n,maxx;

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

void clear_data()
{
    maxx=0;
    for(int i=1;i<=n;i++)
    {
        G[i].clear();
        _time[i]=0;
    }
}

inline int comp(const int &lsh, const int &rsh)
{
    return  _time[lsh]>_time[rsh];
}

void DFS(int node)
{
    for(const auto &it:G[node])
        DFS(it);
    sort(G[node].begin(),G[node].end(),comp);
    for(int i=0;i<(int)G[node].size();i++)
        _time[node]=max(_time[node],_time[G[node][i]]+i+1);
}

void solve()
{
    DFS(1);
    g<<_time[1]<<'\n';
}

int main()
{
    int T;
    f>>T;
    while(T--)
    {
        read();
        solve();
        clear_data();
    }

    return 0;
}