Cod sursa(job #2471463)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 10 octombrie 2019 23:27:00
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100005;

vector<int> graf[MAXN];
int ans, start;

void dfs(int nod, int prec, int cnt){
    if(ans < cnt){
        ans = cnt;
        start = nod;
    }
    for(auto x:graf[nod]){
        if(x == prec) continue;
        dfs(x, nod, cnt + 1);
    }
}

int main()
{
    ifstream fin("darb.in");
    ofstream fout("darb.out");
    int n;
    fin >> n;
    for(int i = 1; i < n; ++i){
        int x, y;
        fin >> x >> y;
        graf[x].push_back(y);
        graf[y].push_back(x);
    }
    dfs(1, 0, 1);
    dfs(start, 0, 1);
    fout << ans;
    return 0;
}