Cod sursa(job #3342023)

Utilizator daviddxmqStan David Andrei daviddxmq Data 22 februarie 2026 14:54:10
Problema Diametrul unui arbore Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int maxn = 100001;
vector<int> g[maxn];
int n;
int maxd, actd;
void dfs(int nod, int parent, int dist) {
    if (dist > actd)
        actd = dist;
    for (int vecin : g[nod])
        if (vecin != parent)
            dfs (vecin, nod, dist + 1);
}
int main() {
    ifstream in("darb.in");
    ofstream out("darb.out");
    in >> n;
    int x, y;
    for (int i = 1; i <= n - 1; ++i) {
        in >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (int i = 1; i <= n; ++i) {
        dfs(i, 0, 0);
        if(actd > maxd)
            maxd = actd;
    }
    out << maxd + 1;
    return 0;
}