Cod sursa(job #2885778)

Utilizator Rares5000Baciu Rares Rares5000 Data 6 aprilie 2022 16:11:22
Problema Diametrul unui arbore Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

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

vector<int> G[100002];
int nivel, nivelmaxim, nod, vis[100002];

void DFS(int x)
{
    vis[x] = 1;
    for(auto w : G[x])
        if(vis[w] == 0)
        {
            vis[w] = 1;
            nivel++;
            DFS(w);
            if(nivel > nivelmaxim)
            {
                nivelmaxim = nivel;
                nod = w;
            }
            nivel--;
        }
}

int main()
{
    int n, i, x, y;
    fin >> n;
    for(i = 1; i < n; i++)
    {
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    DFS(1);
    for(i = 1; i <= n; i++)
        vis[i] = 0;
    DFS(nod);
    fout << nivelmaxim + 1;
    return 0;
}