Cod sursa(job #1743380)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 18 august 2016 02:47:27
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

ifstream cin("zvon.in");
ofstream cout("zvon.out");

const int MAXN = 100000;

vector<int> g[1 + MAXN];
int dp[1 + MAXN];

bool Compare(const int &a, const int &b) {
    return dp[a] > dp[b];
}

void DFS(int node) {
    for (auto &son : g[node])
        DFS(son);
    sort(g[node].begin(), g[node].end(), Compare);
    int step = 0;
    for (auto &son : g[node]) {
        step++;
        dp[node] = max(dp[node], step + dp[son]);
    }
}

int main() {
    int tests;
    cin >> tests;
    for (int test = 1; test <= tests; test++) {
        int n;
        cin >> n;
        for (int i = 1; i < n; i++) {
            int x, y;
            cin >> x >> y;
            g[x].push_back(y);
        }
        DFS(1);
        if (g[1].size() == 0)
            cout << "0\n";
        else
            cout << dp[1] << "\n";
        for (int i = 1; i <= n; i++) {
            g[i].clear();
            dp[i] = 0;
        }
    }
    return 0;
}