Cod sursa(job #1890375)

Utilizator savigunFeleaga Dragos-George savigun Data 23 februarie 2017 11:14:29
Problema Zvon Scor 0
Compilator cpp Status done
Runda becreative20 Marime 1.78 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 part(vector<int> &v, int startp, int endp)
{
    int pivot = subs[v[endp]];
    int pi = startp;

    for(int i=pi;i<=endp-1;++i)
    {
        if(subs[v[i]]>pivot)
        {
            swap(v[i], v[pi]);
            pi++;
        }
    }
    swap(v[pi], v[endp]);
    return pi;
}

void quicksort(vector<int> &v, int startp, int endp)
{
    int pi;

    if(startp<endp)
    {
        pi=part(v, startp, endp);
        quicksort(v, startp, pi-1);
        quicksort(v, pi+1, endp);
    }
}


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

        for (int p = 1; p <= n; ++p) {
            quicksort(graph[p], 0, graph[p].size() - 1);
        }

        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 <= n; ++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;
}