Cod sursa(job #3296074)

Utilizator 0021592Grecu rares 0021592 Data 11 mai 2025 10:31:53
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
vector <vector <int>> mat;
int n, i, x, y, mx, wmx, wkx;
int viz[100010];
void dfs(int k, int s)
{
    viz[k] = s+1;
    if (mx < viz[k])
    {
        mx = viz[k];
        wmx = k;
    }
    for (auto ind : mat[k])
        if (!viz[ind])
            dfs(ind, s+1);
}
int main()
{
    in >> n;
    mat.resize(n+1);
    for (i = 1; i <= n; i++)
    {
        in >> x >> y;
        mat[x].push_back(y);
        mat[y].push_back(x);
    }
    dfs(1, 1);
    for (i = 1; i <= n; i++)
        viz[i] = 0;
    wkx = wmx;
    mx = wmx = 0;
    dfs(wkx, 1);
    out << mx-1;
    return 0;
}