?
//
#include<iostream>
#include<algorithm>
#include<vector> //
using namespace std;
// 1<=n<=100000
const int MAXN=1e5+6;
vector<int> e[MAXN];
int d_all[MAXN];
int cnt_all[MAXN];
void dfs( int son,int father,int d,int cnt )
{
int i;
d_all[son]=d; // 莫 +1
cnt_all[son]=cnt; //
for( i=0;i<e[son].size();i++ )
{
if( e[son][i]!=father )
{
dfs( e[son][i], son, d+1, cnt+1 );
}
}
}
int main()
{
int n,a,b,i,maxa,maxb;
scanf("%d",&n);
for( i=0;i<n-1;i++ )
{
scanf("%d%d",&a,&b);
e[a].push_back( b );
e[b].push_back( a );
}
dfs( 1,-1,0,0 );
maxa=1;
for( i=1;i<=n;i++ )
{
if( d_all[i]>d_all[maxa] ) maxa=i;
}
dfs( maxa,-1,0,0 );
maxb=1;
for( i=1;i<=n;i++ )
{
if( d_all[i]>d_all[maxb] ) maxb=i;
}
printf("%d\n",cnt_all[maxb]);
return 0;
}
//
find:
01 带上 vector 头文件
02 到底 +1 否?
|