Add a linker script
ideas: there is a conclusion, a, b, c three points must have two points is two points on the diameter of the tree, as long as we mark another point on the route at the diameter of the tree, and then point to traverse mark unlabeled point of maximum distance Max diameter plus is the answer.
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+1;
vector<int>g[maxn];
int n,k,maxx=0,ans,first,second,father[maxn],Max=0,t,vis[maxn];
void dfs1(int u,int fa,int deep)//求树的直径
{
if(deep>maxx)
{
maxx=deep;
k=u;
}
for(int to:g[u])
{
if(to==fa) continue;
father[to]=u;
dfs1(to,u,deep+1);
}
}
void dfs2(int u,int fa,int deep)
{
if(deep>maxx)
{
maxx=deep;
k=u;
}
for(int to:g[u])
{
if(to==fa) continue;
if(vis[to]) continue;
dfs2(to,u,deep+1);
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<n;++i){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs1(1,0,0);
first=k;maxx=0;
dfs1(k,0,0);
second=k;
ans=maxx;
vis[first]=1;
for(int i=second;i!=first;i=father[i]) {
if(i!=second&&i!=first) t=i;
vis[i]=1;
}
maxx=0;
for(int i=1;i<=n;++i)
if(vis[i]) {
dfs2(i,0,0);
if(maxx>Max) Max=maxx,t=k;
maxx=0;
}
printf("%d\n",ans+Max);
printf("%d %d %d\n",second,first,t);
}