Cod sursa(job #1799939)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 7 noiembrie 2016 01:21:18
Problema Diametrul unui arbore Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#include <map>
#include <vector>

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

std::map<int, 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, pos, len = 0; in >> n;
    for( int i = 1; i <= n; i ++ ) {
        int x, y; in >> x >> y;
        g[x].push_back( y );
        g[y].push_back( x ); }
    dfs( 1, 0, 1, pos, len );
    dfs( pos, 0, 1, pos, len );
    out << len << std::endl;
    return 0; }