Cod sursa(job #2558171)

Utilizator Bulboaca_EugenBulboaca Alexandru Eugen Bulboaca_Eugen Data 26 februarie 2020 13:07:08
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
const int MAXN = 1 * 1e5 + 5;
vector <int> g[MAXN];
int depthmax, indice;
bool seen[MAXN];
void dfs(int node, int depth){
    if(seen[node]) return ;
    seen[node] = true;
    if(depth > depthmax) {
        depthmax = depth;
        indice = node;
    }
    for(auto x : g[node]) {
        if(!seen[x] ) dfs(x, depth + 1);
    }
}
int main()
{
    int n; fin >> n;
    for(int i = 1; i < n; ++i){
        int x, y; fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1, 1);
    for(int i = 1; i <= n; ++i) seen[i] = false;
    dfs(indice, 1);
    fout << depthmax;
    return 0;
}