Cod sursa(job #1884417)

Utilizator preda.andreiPreda Andrei preda.andrei Data 18 februarie 2017 18:52:38
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <algorithm>
#include <fstream>
#include <vector>

using namespace std;

struct Node
{
    int time;
    vector<int> sons;
};

const int kMaxN = 100000;
Node t[kMaxN];

void Dfs(int node)
{
    if (t[node].sons.size() == 0) {
        t[node].time = 0;
        return;
    }

    vector<int> son_times(t[node].sons.size());
    int sons_visited = 0;

    for (int son : t[node].sons) {
        Dfs(son);
        son_times[sons_visited++] = t[son].time;
    }
    sort(son_times.begin(), son_times.end(), greater<int>());

    t[node].time = 0;
    for (unsigned i = 0; i < t[node].sons.size(); ++i) {
        t[node].time = max(t[node].time, (int)(son_times[i] + i + 1));
    }
}

int main()
{
    ifstream fin("zvon.in");
    ofstream fout("zvon.out");

    int tests;
    fin >> tests;
    while (tests--) {
        int n;
        fin >> n;

        for (int i = 1; i < n; ++i) {
            int x, y;
            fin >> x >> y;
            t[x - 1].sons.push_back(y - 1);
        }

        Dfs(0);
        fout << t[0].time << "\n";

        for (int i = 0; i < n; ++i) {
            t[i].sons.clear();
        }
    }

    return 0;
}