Cod sursa(job #2928067)

Utilizator CobzaruAntonioCobzaru Paul-Antonio CobzaruAntonio Data 22 octombrie 2022 08:43:43
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("darb.in");
ofstream fout ("darb.out");
int n,m,k,i,j,xmax,lvlmax;
vector <int> G[100005];
bool f[100005];
void empty1()
{
    int i;
    for (i=1;i<=n;i++)
        f[i] = false;
}
void dfs (int x,int lvl)
{
    f[x] = true;
    if (lvl > lvlmax)
    {
        lvlmax = lvl;
        xmax = x;
    }
    for (auto e : G[x])
        if (f[e]== false)
            dfs(e,lvl+1);
}
int main()
{
    fin >> n;
    for (k=1;k<=n-1;k++)
    {
        fin >> i >> j;
        G[i].push_back(j);
        G[j].push_back(i);
    }
    dfs(1,1);
    empty1();
    dfs(xmax,1);
    fout << lvlmax;
    return 0;
}