Cod sursa(job #2391827)

Utilizator Bulboaca_EugenBulboaca Alexandru Eugen Bulboaca_Eugen Data 29 martie 2019 11:53:03
Problema Diametrul unui arbore Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5 + 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;

}