Cod sursa(job #3139336)

Utilizator chiriacandrei25Chiriac Andrei chiriacandrei25 Data 27 iunie 2023 14:00:46
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
#include <vector>
#include <climits>

using namespace std;

const int Nmax = 100005;

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

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

int main() {
    int n;
    ifstream fin("darb.in");
    ofstream fout("darb.out");
    fin >> n;
    for(int 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 << "\n";
    return 0;
}