Cod sursa(job #1367401)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 1 martie 2015 20:42:59
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

const int maxn = 100005;

ifstream fin("darb.in");
ofstream fout("darb.out");

int n, dist[maxn], h;
vector <int> g[maxn];

inline void dfs(int node, int father) {
    dist[node] = dist[father] + 1;
    if(dist[h] < dist[node])
        h = node;
    for(auto it : g[node])
        if(it != father)
            dfs(it, node);
}

int main() {
    fin >> n;
    for(int i = 1 ; i < n ; ++ i) {
        int x, y;
        fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1, 0);
    dfs(h, 0);
    fout << dist[h] << '\n';
}