Cod sursa(job #2388848)

Utilizator Bulboaca_EugenBulboaca Alexandru Eugen Bulboaca_Eugen Data 26 martie 2019 16:28:45
Problema Diametrul unui arbore Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
std::vector <int> g[MAXN];
int best, depthmax;
void dfs(int node, int papa, int depth){
    if(depth > depthmax){
        best = node;
        depthmax = depth;
    }
    for(auto y : g[node])
        dfs(y, node, depth + 1);
}
ifstream fin("darb.in");
ofstream fout("darb.out");
int main()
{
    int n;
    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, 0);
    int root = best;
    best = 0;
    depthmax = 0;
    dfs(root, 0, 0);
    fout << depthmax;
}