神树
2025-02-03 13:19:59
发布于:上海
#include<bits/stdc++.h>
using namespace std;
vector<int> g[100010];
int n;//结点数量
int h[100010];
int ans;
void dfs(int u,int fa,int depth){
h[u] = depth;
ans = max(ans,h[u]);
for(int son:g[u]){
if(son==fa)continue;
dfs(son,u,depth+1);
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
int a;
int b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1,-1,1);
// ans最大高度
for(int k=1;k<=ans;k++){
cout<<k-1<<endl;
}
for(int k=ans+1;k<=n;k++){
cout<<ans-1+(k-ans)*2<<endl;
}
return 0;
}
这里空空如也
有帮助,赞一个