rt,一道数的直径题。跑到dfs时直接趋势
#include<bits/stdc++.h>
using namespace std;
int n,m;
vector<int>g[10001];
vector<int>go,vis;int node;
void dfs(int u){
for (int i=0;i<g[u].size();++i){
int t=g[u][i];
if (vis[t]==0){
vis[t]=1;
go[t]=go[u]+1;
if (go[node]<go[t])node=t;
dfs(t);
vis[t]=0;
}
}
return ;
}
int main(){
cin>>n;
m=n-1;
for (int i=0;i<m;++i){
int x,y;
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
vis[1]=1;
dfs(1);
go[node]=0;
vis[node]=1;
dfs(node);
cout<<go[node];
return 0;
}