Cod sursa(job #3327335)

Utilizator apoputoaievladVlad Cristian Apoputoaie apoputoaievlad Data 3 decembrie 2025 15:31:24
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 100005;
int n, m;
vector <int> g[NMAX];
int lg[NMAX];

void Dfs (int nod)
{
    for(auto e : g[nod])
        if (lg[e] == 0)
        {
        lg[e] = lg[nod]+1;
        Dfs(e);
        }
}

int main()
{
    int i, a, b, lgmax = 0, poz = 0;
    fin >> n;
    for (i = 1; i < n; i++)
    {
        fin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    lg[1]=1;
    Dfs(1);
    for (i = 1; i <= n; i++)
        if (lg[i] > lgmax)
        {
        lgmax = lg[i];
        poz = i;
        }
    for (i = 1; i <= n; i++)
    {
        lg[i] = 0;
    }
    lg[poz]=1;
    Dfs(poz);
    lgmax = 0;
    for (i = 1; i <= n; i++)
        if (lg[i] > lgmax)
        {
            lgmax = lg[i];
        }
    fout << lgmax;
    return 0;
}