Cod sursa(job #1890327)

Utilizator savigunFeleaga Dragos-George savigun Data 23 februarie 2017 10:59:47
Problema Zvon Scor 0
Compilator cpp Status done
Runda becreative20 Marime 1.46 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

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

int n, subs[100005], sol;
vector<int> graph[100005];

void citire();
void get_subs(int x);
void zvon(int x, int step);


int main() {
    int t;
    in >> t;
    for (int test = 1; test <= t; ++test) {
        citire();
        get_subs(1);

        for (int p = 1; p <= n; ++p) {
            for (int i = 0; i < graph[p].size(); ++i) {
                for (int j = i + 1; j < graph[p].size(); ++j) {
                    if (subs[graph[p][i]] < subs[graph[p][j]]) swap(graph[p][i], graph[p][j]);
                }
            }
        }

        zvon(1, 0);
        out << sol << '\n';
    }

    in.close();
    out.close();
    return 0;
}

void zvon(int x, int step) {
    sol = max(sol, step);
    for (int i = 0; i < graph[x].size(); ++i) {
        int y = graph[x][i];
        step++;
        zvon(y, step);
    }
}

void get_subs(int x) {
    subs[x] = 1;

    for (int i = 0; i < graph[x].size(); ++i) {
        int y = graph[x][i];
        if (subs[y] == 0) get_subs(y);
        subs[x] += subs[y];
    }
}

void citire() {
    in >> n;
    for (int i = 1; i <= 100001; ++i) {
        graph[i].clear();
        subs[i] = 0;
    }

    for (int i = 1; i < n; ++i) {
        int x, y;
        in >> x >> y;
        graph[x].push_back(y);
    }

    sol = 0;
}