Cod sursa(job #3251575)

Utilizator ArthurrrfubinacaARthur Paun Arthurrrfubinaca Data 26 octombrie 2024 11:16:20
Problema Diametrul unui arbore Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <cstring>
#define Nmax 100000
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
int n,m,x,y,lmax=-1,vx;
vector<int> g[Nmax + 5];
int viz[Nmax + 5];
void dfs(int nod,int l)
{
    viz[nod]=1;
    if(l>lmax)
        {
            lmax=l;
            vx=nod;
        }
    for(auto i : g[nod])
    {
        if(viz[i]==0)
        dfs(i,l+1);
    }
}
int main()
{
    fin>>n;
    for(int i=1;i<n;i++)
    {
        fin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1,0);
    lmax=-1;
    //cout<<"nigga";
    memset(viz,0,Nmax + 5);
    dfs(vx,0);
    fout<<lmax+1;
    return 0;
}