fork download
  1. #include <bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define all(v) v.begin() , v.end()
  5. #define sz(v) int(v.size())
  6. #define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
  7. using namespace std;
  8.  
  9. typedef long long ll;
  10. typedef pair<int , int> ii;
  11. typedef pair<long long , int> lli;
  12.  
  13. const int maxN = int(2e5)+7;
  14.  
  15. int n;
  16. vector<int> g[maxN] , adj[maxN];
  17. int max_d[maxN] , p[maxN] , cnt[maxN];
  18.  
  19. void dfs(int u){
  20. max_d[u] = 0;
  21. for (int v : g[u]){
  22. if (v != p[u]){
  23. p[v] = u;
  24. adj[u].push_back(v);
  25. dfs(v);
  26. max_d[u] = max(max_d[u] , max_d[v] + 1);
  27. }
  28. }
  29. }
  30.  
  31. void solve(){
  32. cin >> n;
  33. for (int i = 1 ; i < n ; i++){
  34. int u , v;
  35. cin >> u >> v;
  36. g[u].push_back(v);
  37. g[v].push_back(u);
  38. }
  39. p[0] = -1;
  40. dfs(0);
  41. for (int i = 0 ; i < n ; i++){
  42. sort(all(adj[i]) , [](int x , int y){
  43. return max_d[x] > max_d[y];
  44. });
  45. }
  46. set<ii> st;
  47. st.insert({-max_d[0] , 0});
  48. int ans = 0;
  49. while (st.empty() == 0){
  50. int u = st.begin()->se;
  51. st.erase(st.begin());
  52. ans++;
  53. while (true){
  54. if (u != 0) cout << ans << " ";
  55. if (sz(adj[u]) == 0) break;
  56. for (int i = 1 ; i < sz(adj[u]) ; i++){
  57. int v = adj[u][i];
  58. st.insert({-max_d[v] , v});
  59. }
  60. u = adj[u][0];
  61. }
  62. }
  63. }
  64.  
  65. #define name "A"
  66.  
  67. int main(){
  68. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  69. if (fopen(name".INP" , "r")){
  70. freopen(name".INP" , "r" , stdin);
  71. freopen(name".OUT" , "w" , stdout);
  72. }
  73. int t = 1; //cin >> t;
  74. while (t--) solve();
  75. return 0;
  76. }
  77.  
  78.  
Success #stdin #stdout 0.01s 13656KB
stdin
Standard input is empty
stdout
Standard output is empty