Cod sursa(job #2762425)

Utilizator Bulboaca_EugenBulboaca Alexandru Eugen Bulboaca_Eugen Data 7 iulie 2021 07:54:44
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
using namespace std;

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

const int MAXN = 1e5 + 2;
const int INF = 1e8 ;

vector <int> g[MAXN];
int depthmax, nodepth;

void lungime(int node, int depth, int papa){

    if(depth > depthmax) {
        depthmax = depth;
        nodepth = node;
    }

    for(auto y : g[node]){
        if(y != papa) lungime(y, depth + 1, node);
    }
}

int main()
{
    int n; fin >> n;
    for(int i = 1; i <= n - 1; ++i){
        int x, y; fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    lungime(1, 0, 0);
    lungime(nodepth, 0, 0);

    fout << depthmax + 1;
    return 0;
}