Cod sursa(job #2571493)

Utilizator 3DwArDPauliuc Edward 3DwArD Data 5 martie 2020 00:06:27
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
#define NMAX 100005
using namespace std;
vector< int > G[NMAX];

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

vector< int > level(NMAX);

int high = -1;
void dfs(int node, int lvl){
    level[node] = lvl;
    if(high == -1 || level[node] > level[high])
        high = node;
    for(auto it: G[node]){
        if(!level[it])dfs(it, lvl+1);
    }
}
int main()
{
    int n;
    f>>n;

    for(int i=0,x,y;i<n;i++){
        f>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }


    dfs(1,1);
    for(int i=1;i<=n;i++)level[i]=0;
    int newRoot = high;
    high=-1;
    dfs(newRoot, 1);
    g<<level[high];
    return 0;
}