Cod sursa(job #2567293)

Utilizator petrisorvmyVamanu Petru Gabriel petrisorvmy Data 3 martie 2020 16:28:21
Problema Zvon Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <bits/stdc++.h>
#define FILE_NAME "zvon"
#define fast ios_base :: sync_with_stdio(0); cin.tie(0);
#pragma GCC optimize("O3")
#define NMAX 100010
using namespace std;

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

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pi;
typedef pair<ld,ld> pct;

const ll inf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;


void add(ll &a , ll b)
{
    a += b;
    a %= mod;
}

void sub(ll &a, ll b)
{
    a = (a - b + mod) % mod;
}

int t, n, x, y, h[NMAX],pd[NMAX];
vector <int> G[NMAX];
bitset <NMAX> viz;

bool cmp(int a, int b)
{
    if(h[a] <= h[b])
        return 0;
    return 1;
}


void DFSh(int nod)
{
    for(auto it : G[nod])
        DFSh(it);

    sort(G[nod].begin(), G[nod].end(), cmp);
    for(int i = 0; i < G[nod].size(); ++i)
        h[nod] = max(h[nod], h[ G[nod][i] ] + i + 1);
}

int main()
{
    f >> t;
    while(t--)
    {
        f >> n;
        for(int i = 1; i <= n; ++i)
        {
            pd[i] = 0;
            h[i] = 0;
            viz[i] = 0;
            G[i].clear();
        }
        for(int i = 1; i < n; ++i)
        {
            f >> x >> y;
            G[x].push_back(y);
        }
        DFSh(1);
        g << h[1];
        g << '\n';
    }
    f.close();
    g.close();
    return 0;
}