Cod sursa(job #2567272)

Utilizator petrisorvmyVamanu Petru Gabriel petrisorvmy Data 3 martie 2020 16:20:28
Problema Zvon Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.61 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;

void DFSh(int nod)
{
    for(auto it : G[nod])
    {
        DFSh(it);
        h[nod] = max(h[nod], h[it] + 1);
    }
}
bool cmp(int a, int b)
{
    if(h[a] <= h[b])
        return 0;
    return 1;
}

void call(int nod)
{
    int cat = 0;
    for(auto it : G[nod])
    {
        cat++;
        pd[it] = pd[nod] + cat;
        call(it);
    }
}
int main()
{
    f >> t;
    while(t--)
    {
        f >> n;
        for(int i = 1; i <= n; ++i)
        {
            pd[i] = 0;
            h[i] = 1;
            viz[i] = 0;
            G[i].clear();
        }
        for(int i = 1; i < n; ++i)
        {
            f >> x >> y;
            G[x].push_back(y);
        }
        DFSh(1);
        for(int i = 1; i <= n; ++i)
            sort(G[i].begin(), G[i].end(), cmp);
        call(1);
        int ans = 0;
        for(int i = 1; i <= n; ++i)
            ans = max(ans, pd[i]);
        g << ans;
        g << '\n';
    }
    f.close();
    g.close();
    return 0;
}