Cod sursa(job #1330250)

Utilizator wGEORGEWGeorge Cioti wGEORGEW Data 30 ianuarie 2015 15:32:26
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
ifstream f ("zvon.in");
ofstream g ("zvon.out");
 
const int NMAX = 100000 + 1;
 
int t, n;
int timp[NMAX];
vector <int> graf[NMAX];
 
void citeste() {
    f >> n;
    int a, b;
    for (int i = 1; i < n; i++) {
        f >> a >> b;
        graf[a].push_back(b);
    }
}
 
inline bool comp(int a, int b) {
    return timp[a] > timp[b];
}
 
void DFS(int nod) {
    int l = graf[nod].size(), fiu;
    for (int i = 0; i < l; i++) {
        fiu = graf[nod][i];
        DFS(fiu);
    }
 
    sort (graf[nod].begin(), graf[nod].end(), comp);
 
    for (int i = 0; i < l; i++) {
        fiu = graf[nod][i];
        timp[nod] = max(timp[nod], timp[graf[nod][i]] + i + 1);
    }
}
 
void initializeaza() {
    for (int i = 1; i <= n; i++) {
        graf[i].clear();
        timp[i] = 0;
    }
}
 
int main() {
    f >> t;
    while (t--) {
        initializeaza();
        citeste();
        DFS(1);
        g << timp[1] << '\n';
    }
    return 0;
}