Cod sursa(job #1799940)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 7 noiembrie 2016 01:22:20
Problema Diametrul unui arbore Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>

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

std::vector< std::vector<int> > g;

void dfs( int x, int f, int l, int &pos, int &len ) {
    if( len < l ) { len = l; pos = x; }
    for( auto y : g[x] ) {
        if( y != f ) { dfs( y, x, l + 1, pos, len ); } }
    return; }

int main( int argc, const char *argv[] ) {
    std::ios::sync_with_stdio( false );
    int n; in >> n; g.resize( n + 1 );
    for( int i = 1; i <= n; i ++ ) {
        int x, y; in >> x >> y;
        g[x].push_back( y );
        g[y].push_back( x ); }
    int pos, len = 0;
    dfs( 1, 0, 1, pos, len );
    dfs( pos, 0, 1, pos, len );
    out << len << std::endl;
    return 0; }