Cod sursa(job #2304965)

Utilizator CronosClausCarare Claudiu CronosClaus Data 18 decembrie 2018 21:33:01
Problema Diametrul unui arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <bits/stdc++.h>

using namespace std;

const int mxn = 100 * 1000 + 10;

vector< int > g[ mxn ];
bitset< mxn > viz;

int n;

int mxv, mxd;

void dfs(int nod, int d){
    viz[ nod ] = 1;
    if(d > mxd){
        mxv = nod;
        mxd = d;
    }
    for(auto it: g[ nod ])
        if(viz[ it ] == 0)
            dfs(it, d + 1);
}

int main()
{
    ifstream cin("darb.in");
    ofstream cout("darb.out");
    cin>> n;
    for(int i = 1, x, y; i < n; i++){
        cin>> x >> y;
        g[ x ].push_back( y );
        g[ y ].push_back( x );
    }
    dfs( 1, 1 );
    viz = bitset< mxn >();
    dfs( mxv, 1 );
    cout<< mxd;
    return 0;
}