#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 2e5+10,M=N*2;
int h[N],e[M],ne[M],idx;
int n;
int d1[N],d2[N],up[N],p[N];
int maxd;
void add(int a,int b){
e[idx] = b;
ne[idx] = h[a];
h[a] = idx;
idx++;
}
void dfs_d(int u,int fa){
for(int i=h[u];i!=-1;i=ne[i]){
int j = e[i];
if(j!=fa){
dfs_d(j,u);
int distance = d1[j]+1;
if(distance > d1[u]){
d2[u] = d1[u];
d1[u] = distance;
p[u] = j;
}
else if(distance>d2[u]){
d2[u] = distance;
}
}
}
maxd = max(maxd,d1[u]+d2[u]);
}
void dfs_u(int u,int fa){
for(int i=h[u];i!=-1;i=ne[i]){
int j = e[i];
if(j!=fa){
up[j] = up[u] + 1;
if(p[u]==j) up[j] = max(up[j],d2[u]+1);
else up[j] = max(up[j],d1[u]+1);
dfs_u(j,u);
}
}
}
int main(){
scanf("%d",&n);
memset(h,-1,sizeof h);
for(int i=0;i<n-1;i++){
int a,b;
scanf("%d%d",&a,&b);
add(a,b),add(b,a);
}
dfs_d(0,-1);
dfs_u(0,-1);
for(int i=0;i<n;i++){
int d[3] = {d1[i],d2[i],up[i]};
sort(d,d+3);
if(d[1] + d[2] == maxd) printf("%d\n",i);
}
}
05-19
1556
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-02