Cod sursa(job #2978930)

Utilizator CobzaruAntonioCobzaru Paul-Antonio CobzaruAntonio Data 14 februarie 2023 17:20:55
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
#include <vector>
#define pb push_back
using namespace std;
ifstream cin ("darb.in");
ofstream cout ("darb.out");
int n;
int dmax,nodmax;
vector<int> g[100005];
void dfs (int nod,int p,int lvl)
{
    if (lvl>dmax)
    {
        dmax = lvl;
        nodmax = nod;
    }
    for (auto vf:g[nod])
    {
        if (vf == p)
            continue;
        dfs(vf,nod,lvl+1);
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int i,j;
    cin >> n;
    for (i=1;i<=n;i++)
    {
        int x,y;
        cin >> x >> y;
        g[x].pb(y);
        g[y].pb(x);
    }
    dfs(1,0,0);
    dmax = 0;
    dfs(nodmax,0,0);
    cout << dmax + 1;
    return 0;
}