Cod sursa(job #2738695)

Utilizator vlad082002Ciocoiu Vlad vlad082002 Data 6 aprilie 2021 11:17:27
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, f, d;
vector<int> g[100005];

void dfs(int x, int t, int depth) {
    if(depth > d) {
        d = depth;
        f = x;
    }
    for(auto next: g[x])
        if(next != t)
            dfs(next, x, depth+1);
}

int main() {
    fin >> n;
    for(int i = 1; i < n; i++) {
        int a, b;
        fin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1, 0, 1);
    dfs(f, 0, 1);
    fout << d << '\n';
}