Cod sursa(job #3311488)

Utilizator Andrei-Dani-10Pisla Andrei Daniel Andrei-Dani-10 Data 22 septembrie 2025 18:04:36
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int nmax = 1e5;
int n, a, b, maxi, dp[nmax + 2];
int wheremx;
vector <int> muchii[nmax + 2];

void dfs(int node, int parent){
    dp[node] = dp[parent] + 1;
    maxi = max(maxi, dp[node]);

    if(maxi == dp[node])
        wheremx = node;

    for(auto nxt : muchii[node]){
        if(nxt != parent)
            dfs(nxt, node);
    }
}

int main(){

    in>>n;
    for(int i = 1; i <= n - 1; i++){
        in>>a>>b;
        muchii[a].push_back(b);
        muchii[b].push_back(a);
    }

    dfs(1, 0);
    dfs(wheremx, 0);
    out<<maxi<<"\n";

    return 0;
}