Cod sursa(job #2391855)

Utilizator Bulboaca_EugenBulboaca Alexandru Eugen Bulboaca_Eugen Data 29 martie 2019 12:16:48
Problema Diametrul unui arbore Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb

#include <bits/stdc++.h>

using namespace std;

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

        if(y != papa)

            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, 1, 0);
    int root = best;
    dfs(root, 0, 0);
    fout << depthmax;

}