Cod sursa(job #3286632)

Utilizator crina2120Arnautu Cristina-Crina crina2120 Data 14 martie 2025 14:35:24
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, maxx, nod;
bitset <100003> viz;
vector <int> g[100003];

void DFS(int x, int cnt)
{
    viz[x] = 1;
    for (int i : g[x])
        if (viz[i] == 0) DFS(i, cnt + 1);
    if (maxx < cnt)
    {
        maxx = cnt;
        nod = x;
    }
}

int main()
{
    int i, j, p;
    fin >> n;
    for (p = 1; p < n; p++)
    {
        fin >> i >> j;
        g[i].push_back(j);
        g[j].push_back(i);
    }
    DFS(1, 0);
    for (i = 1; i <= n; i++)
        viz[i] = 0;
    DFS(nod, 1);
    fout << maxx<< "\n";
    return 0;
}
//0, 1, 3-5, 7-18, 21, 22, 24-27, 29, 30, 34-36, 42, 44, 45, 47, 48, 51, 54, 56-58