Cod sursa(job #2442204)

Utilizator CharacterMeCharacter Me CharacterMe Data 23 iulie 2019 11:38:49
Problema Zvon Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int t, n, i, j, sol[100010];
vector<int> graph[100010];
struct comp{
    bool operator()(int x, int y){
        return x>=y;
    }
} c;
void dfs(int node);
int main()
{
    freopen("zvon.in", "r", stdin);
    freopen("zvon.out", "w", stdout);
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        for(i=1; i<=n; ++i) {graph[i].clear(); sol[i]=0;}
        for(i=1; i<n; ++i){
            int x, y;
            scanf("%d%d", &x, &y);
            graph[x].push_back(y);
        }
        dfs(1);
        printf("%d\n", sol[1]);
    }
    return 0;
}
void dfs(int node){
    int i, sz=graph[node].size();
    for(i=0; i<sz; ++i)
        {dfs(graph[node][i]); graph[node][i]=sol[graph[node][i]];}
    sort(graph[node].begin(), graph[node].end(), c);
    int val, mx=0;
    if(node==1) val=0; else val=1;
    for(i=0; i<sz; ++i) {graph[node][i]+=i; mx=max(mx, graph[node][i]);}
    val+=mx;
    sol[node]=val;
    return;
}