Cod sursa(job #3143939)

Utilizator emazareMazare Emanuel emazare Data 3 august 2023 12:58:01
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int Nmax = 100005;
vector<int> L[Nmax];
int max_level = 0, leaf = 1;

void dfs(int node, int father, int level){
    if(max_level<level){
        max_level = level;
        leaf = node;
    }
    for(int son : L[node]){
        if(son!=father)
            dfs(son, node, level+1);
    }
}

int main()
{
    int n,i;
    fin >> n;
    for(i=1;i<n;i++){
        int x,y;
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    dfs(1,0,0);
    dfs(leaf,0,1);
    fout << max_level;
    return 0;
}