Cod sursa(job #3136986)

Utilizator TheLostRevolverCalin Andrei TheLostRevolver Data 9 iunie 2023 20:05:53
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
vector<int> v[100001];
int nivelmax, nodmax;

void dfs(int nod, int tata, int nivel) {
    if (nivel > nivelmax) {
        nodmax = nod;
        nivelmax = nivel;
    }
    for (int i: v[nod]) {
        if (i != tata) {
            dfs(i, nod, nivel + 1);
        }
    }
}

int main() {
    ifstream fin("darb.in");
    ofstream fout("darb.out");
    int n;
    fin >> n;
    for (int i = 1; i < n; i++) {
        int a, b;
        fin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(1, 0, 1);
    dfs(nodmax, 0, 1);
    fout << nivelmax;
    fin.close();
    fout.close();
    return 0;
}