Advertisement
prog3r

bruh

May 5th, 2025
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #ifdef LO
  5. #pragma GCC optimize("trapv")
  6. #endif
  7. constexpr int MOD = (119<<23)+1;
  8. //constexpr int MOD = 1e9+7;
  9. signed main() {
  10. #ifndef LO
  11.     clog << "FIO\n";
  12.     ios::sync_with_stdio(0);
  13.     cin.tie(0);
  14. #endif
  15.     int TT = 1;
  16.     cin >> TT;
  17.     for (int tt = 0; tt < TT; tt += 1) {
  18.         int n;
  19.         cin >> n;
  20.         vector<vector<int>> adj(n+1);
  21.         for (int i = 0; i < n-1; i += 1) {
  22.             int u, v;
  23.             cin >> u >> v;
  24.             adj[u].push_back(v);
  25.             adj[v].push_back(u);
  26.         }
  27.         vector<tuple<int,int,int>> ans;
  28.         vector<bool> noapple(n+1);
  29.         auto get_down = [&] (auto f, const int root) -> void {
  30.             queue<int> q;
  31.             map<int,int> dist;
  32.             dist[root] = 0;
  33.             q.push(root);
  34.             int max_dist = 0;
  35.             int max_dist_vert = root;
  36.             while (!q.empty()) {
  37.                 auto tp = q.front();
  38.                 q.pop();
  39.                 for (const auto &x : adj[tp]) if (!noapple[x]) {
  40.                     if (!dist.count(x) || dist[x] > dist[tp] + 1) {
  41.                         dist[x] = dist[tp] + 1;
  42.                         q.push(x);
  43.                         if (max_dist < dist[x] || max_dist == dist[x] && x > max_dist_vert) {
  44.                             max_dist_vert = x;
  45.                             max_dist = dist[x];
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.             int from = max_dist_vert;
  51.             dist.clear();
  52.             dist[from] = 0;
  53.             q.push(from);
  54.             max_dist = 0;
  55.             max_dist_vert = from;
  56.             while (!q.empty()) {
  57.                 auto tp = q.front();
  58.                 q.pop();
  59.                 for (const auto &x : adj[tp]) if (!noapple[x]) {
  60.                     if (!dist.count(x) || dist[x] > dist[tp] + 1) {
  61.                         dist[x] = dist[tp] + 1;
  62.                         q.push(x);
  63.                         if (max_dist < dist[x] || max_dist == dist[x] && x > max_dist_vert) {
  64.                             max_dist_vert = x;
  65.                             max_dist = dist[x];
  66.                         }
  67.                     }
  68.                 }
  69.             }
  70.             int to = max_dist_vert;
  71.             set<int> seen;
  72.             bool done = false;
  73.             vector<int> stack;
  74.             auto dfs = [&] (auto f, int u) -> void {
  75.                 stack.push_back(u);
  76.                 if (u == to) {
  77.                     done = true;
  78.                     return;
  79.                 }
  80.                 for (const auto &x : adj[u]) if (!noapple[x]) {
  81.                     if (!seen.count(x)) {
  82.                         seen.insert(x);
  83.                         f(f, x);
  84.                     }
  85.                     if (done) {
  86.                         return;
  87.                     }
  88.                 }
  89.                 stack.pop_back();
  90.             };
  91.             seen.insert(from);
  92.             dfs(dfs, from);
  93.             vector<int> nw;
  94.             for (const auto &x : stack) {
  95.                 noapple[x] = true;
  96.             }
  97.             for (const auto &x : stack) {
  98.                 for (const auto &v : adj[x]) {
  99.                     if (!noapple[v]) {
  100.                         nw.push_back(v);
  101.                     }
  102.                 }
  103.             }
  104.             ans.push_back({stack.size(), max(from, to), min(from, to)});
  105.             for (const auto &v : nw) {
  106.                 f(f, v);
  107.             }
  108.         };
  109.         get_down(get_down, 1);
  110.         sort(ans.rbegin(), ans.rend());
  111.         for (const auto &[_1, _2, _3] : ans) {
  112.             cout << _1 << " " << _2 << " " << _3 << " ";
  113.         }cout << "\n";
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement