Cod sursa(job #1890393)

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

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

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

void citire();
void zvon(int x);


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

        out << subs[1] << '\n';
    }

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

void zvon(int x) {
    int maxi = 0;
    int freq = 0;

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

        if (subs[y] > maxi) {
            maxi = subs[y];
            freq = 1;
        } else if (subs[y] == maxi) {
            freq++;
        }
    }

    subs[x] = maxi + 1 + (freq - 1);
}


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);
    }
}