加强一下数据吧
#include"bits/stdc++.h"
using namespace std;
const int N = 1000010;
#define inl inline
#define reg register
//#define ll long long
int head[N],ver[N << 1],cnt,net[N << 1];
int f[N];
int deg[N];//Èë¶È
inline void addedge(int a,int b)
{
ver[ ++ cnt] = b,net[cnt] = head[a],head[a] = cnt;
}
inline int read(void)
{
int x = 0,f = 1;char ch = getchar();
while(!isdigit(ch)) f = ch == '-' ? - 1 : f,ch = getchar();
while(isdigit(ch)) x = (x << 3) + (x << 1) + ch - '0',ch = getchar();
return x * f;
}
int ansi = 0,ans = 1e-9;
inline void dfs(int u,int fa,int deep)
{
for(int i = head[u];i;i = net[i])
{
ans = max(ans,deep);
if(ver[i] == fa) continue;
dfs(ver[i],u,deep + 1);
}
}
int main(void)
{
int n = read();
for(int i = 1;i <= n - 1;i ++)
{
int x = read(),y = read();
addedge(x,y);
addedge(y,x);
deg[x] ++;
deg[y] ++;
}
int rans = 1e-9,count = 0;
for(int i = 1;i <= n;i ++)
{
if(deg[i] == 1)
{
memset(f,0,sizeof f);
dfs(i,0,0);
if(rans < ans)
{
rans = ans;
ansi = i;
}
count ++;
}
}
printf("%d",ansi);
return 0;
}