Cod sursa(job #2592136)

Utilizator KillHorizon23Orban Robert KillHorizon23 Data 1 aprilie 2020 11:19:19
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>
using namespace std;

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

const int VMAX = 100005;

int n, x, y, nivel_maxim, poz_nivel_max;
vector < int > graf[VMAX];
bitset < VMAX > viz;

void DFS(int nod, int nivel)
{
    viz[nod] = 1;
    if (nivel > nivel_maxim)
        nivel_maxim = nivel, poz_nivel_max = nod;
    for (unsigned int i = 0; i < graf[nod].size(); ++i)
        if (!viz[graf[nod][i]]) DFS(graf[nod][i], nivel + 1);
}

int main()
{
    fin >> n;
    for (int i = 1; i < n; ++i)
    {
        fin >> x >> y;
        graf[x].push_back(y);
        graf[y].push_back(x);
    }
    DFS(1, 1);
    viz.reset();
    DFS(poz_nivel_max, 1);
    fout << nivel_maxim;
}