Cod sursa(job #2501199)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 29 noiembrie 2019 11:09:46
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <fstream>
#include <vector>

using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");
vector < int > L[100009];
int n, i, sol, x, y, nodmax;

void dfs ( int nod, int niv, int tata ){
    if ( sol < niv ){
        sol = niv;
        nodmax = nod;
    }
    for ( int i = 0; i < L[nod].size(); i++ )
        if ( L[nod][i] != tata )
            dfs( L[nod][i], niv+1, nod );
}


int main()
{
    f>>n;
    for ( i=1; i < n; i++ ){
        f>>x>>y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    dfs( 1, 1, 0 );
    sol = 0;
    dfs ( nodmax, 1, 0 );
    g<<sol;
    return 0;
}