Cod sursa(job #2369625)

Utilizator PopeangaMihneaPopeanga Mihnea- Stefan PopeangaMihnea Data 6 martie 2019 08:28:26
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, h_max, last;
vector<int>G[100001];
bool viz[100001];

void DFS(int nod, int h)
{
    viz[nod]=1;
    if(h>h_max)
    {
        h_max=h;
        last=nod;
    }
    for(auto it:G[nod])
    {
        if(!viz[it]) DFS(it, h+1);
    }
}

int main()
{
    fin>>n;
    for(int i=1; i<=n; ++i)
    {
        int x, y;
        fin>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    DFS(1, 1);
    memset(viz, 0, sizeof(viz));
    DFS(last, 1);
    fout<<h_max<<"\n";
    return 0;
}