func_code_string: #include <bits/stdc++.h>   using namespace std;   long long a, b, c, d, e, i, j, ii, jj, zx, xc, qw[200009], we[200009], pas, pi,       k[200009];   pair<pair<long long, long long>, long long> p[600009];   bool bo[200009], rko[200009];   vector<long long> v[200009];   deque<long long> de;   void dfsst(long long q, long long w, long long rr) {     if (rr > xc) {       xc = rr;       zx = q;     }     for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) {       if ((*it) == w) continue;       dfsst((*it), q, rr + 1);     }   }   void dfsst2(long long q, long long w, long long rr) {     if (rr > xc) {       xc = rr;       zx = q;     }     for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) {       if ((*it) == w) continue;       dfsst2((*it), q, rr + 1);     }   }   void dfs(long long q, long long w, long long rr) {     qw[q] = rr - 1;     for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) {       if ((*it) == w) continue;       dfs((*it), q, rr + 1);     }   }   void dfs2(long long q, long long w, long long rr) {     if (d == 0) bo[q] = 1;     if (q == e) d = 1;     we[q] = rr - 1;     for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) {       if ((*it) == w) continue;       dfs2((*it), q, rr + 1);     }     if (d == 0) bo[q] = 0;   }   void dfs3(long long q, long long w) {     if (q == zx) return;     pi++;     p[pi].first.first = q;     p[pi].first.second = zx;     p[pi].second = q;     for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) {       if ((*it) == w) continue;       if (bo[(*it)] == 1) {         dfs3((*it), q);         return;       }     }   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> a;     for (i = 1; i < a; i++) {       cin >> c >> d;       v[c].push_back(d);       v[d].push_back(c);       k[c]++;       k[d]++;     }     zx = 0;     xc = 0;     dfsst(1, 0, 1);     e = zx;     zx = 0;     xc = 0;     dfsst2(e, 0, 1);     dfs(e, 0, 1);     d = 0;     dfs2(zx, 0, 1);     for (i = 1; i <= a; i++) {       if (bo[i] == 0 && v[i].size() == 1) {         de.push_back(i);         rko[i] = 1;       }     }     while (de.size() != 0) {       c = de.front();       de.pop_front();       pas += max(qw[c], we[c]);       pi++;       if (qw[c] >= we[c]) {         p[pi].first.first = e;         p[pi].first.second = c;         p[pi].second = c;       } else {         p[pi].first.first = zx;         p[pi].first.second = c;         p[pi].second = c;       }       d = 0;       for (vector<long long>::iterator it = v[c].begin(); it != v[c].end();            it++) {         if (rko[(*it)] == 1) continue;         d = (*it);         break;       }       k[d]--;       if (d != 0 && bo[d] == 0 && k[d] == 1) {         de.push_back(d);         rko[d] = 1;       }     }     pas += qw[zx] * (qw[zx] + 1) / 2;     dfs3(e, 0);     cout << pas << endl;     for (i = 1; i <= pi; i++)       cout << p[i].first.first <<     << p[i].first.second <<     << p[i].second            << endl;     return 0;   }   