Pagini recente » Cod sursa (job #2413226) | Cod sursa (job #2777428) | Cod sursa (job #385982) | Cod sursa (job #2840659) | Cod sursa (job #1438060)
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define Nmax 100002
#define inf 0x3f3f3f3f
FILE *f = fopen ( "darb.in", "r" );
FILE *g = fopen ( "darb.out", "w" );
vector < int > G[Nmax];
queue < int > Q;
int dist[Nmax], N;
int BFS ( int start ){
vector < int > :: iterator it;
for ( int i = 1; i <= N; ++i )
dist[i] = inf;
dist[start] = 0;
Q.push ( start );
while ( !Q.empty() ){
int nod = Q.front();
Q.pop();
for ( it = G[nod].begin(); it < G[nod].end(); ++it )
if ( dist[*it] > dist[nod] + 1 ){
dist[*it] = dist[nod] + 1;
Q.push ( *it );
}
}
int maxx = -1, nod;
for ( int i = 1; i <= N; ++i )
if ( dist[i] != inf && dist[i] > maxx ){
maxx = dist[i];
nod = i;
}
return nod;
}
int main(){
int M, x, y, Start;
fscanf ( f, "%d", &N );
for ( int i = 1; i < N; ++i ){
fscanf ( f, "%d%d", &x, &y );
G[x].push_back ( y );
G[y].push_back ( x );
}
Start = BFS ( 1 );
fprintf ( g, "%d", dist[BFS ( Start )] + 1 );
return 0;
}