Cod sursa(job #2393416)

Utilizator Bulboaca_EugenBulboaca Alexandru Eugen Bulboaca_Eugen Data 31 martie 2019 14:21:24
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 15;
vector <int> g[MAXN];
int depthmax, best;
void parcurgere(int node, int papa, int depth){
    if(depth > depthmax){
        best = node;
        depthmax = depth;
    }
    for(auto y : g[node] )
        if(y != papa)
            parcurgere(y, node, depth + 1);
}
ifstream fin("darb.in");
ofstream fout("darb.out");
int main(){
    int n;
    fin >> n ;
    for(int i = 1; i <= n - 1; ++i){
        int a, b;
        fin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    parcurgere(1, 0, 0);
    int leaf = best;
    best = 0;
    depthmax = 0;
    parcurgere(leaf, 0, 0);
    fout << depthmax + 1;
    return 0;
}