Language: C++

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <random>

using namespace std;

#define int long long

const int INF = 1e18 + 239;

void relax(vector<int> diam, vector<vector<int>> g, int &ans) {
    int n = (int)g.size();
    vector<int> used(n);
    vector<int> mxbh(n);
    vector<int> cnth(n);
    function<void(int,int)> dfs = [&](int cur, int ch) {
        used[cur] = 1;
        int cnt = 0;
        for (auto t : g[cur]) {
            if (!used[t]) {
                dfs(t, ch + 1);
                cnt++;
            }
        }
        cnth[ch]++;
        mxbh[ch] = max(mxbh[ch], cnt);
    };
    if ((int)diam.size() % 2 == 1) {
        dfs(diam[(int)diam.size() / 2], 0);
    } else {
        int p = (int)diam.size() / 2;
        used[diam[p]] = 1;
        used[diam[p - 1]] = 1;
        dfs(diam[p], 0);
        dfs(diam[p - 1], 0);
    }
    int cur = 1;
    for (int i = 0; i < n; i++) {
        cur *= max((int)1, mxbh[i]);
    }
    if ((int)diam.size() % 2 == 0) {
        cur *= 2;
    }
    ans = min(ans, cur);
}

vector<int> path(int n, vector<vector<int>> g, int a, int b) {
    vector<int> ps(n);
    vector<int> used(n);
    function<void(int, int)> dfs = [&](int cur, int p) {
        ps[cur] = p;
        used[cur] = 1;
        for (auto t : g[cur]) {
            if (!used[t]) {
                dfs(t, cur);
            }
        } 
    };
    dfs(a, -1);
    vector<int> ans;
    int cur = b;
    while (cur != -1) {
        ans.push_back(cur);
        cur = ps[cur];
    }
    return ans;
}

void try_path(vector<int> path, vector<vector<int>> g, int &ans) {
    // cerr << "trying " << endl;
    // for (auto t : path) {
    //     cerr << t + 1 << ' ';
    // }
    // cerr << endl;
    int n = (int)g.size();
    g.push_back(vector<int> ());
    relax(path, g, ans);
    if (path.size() % 2 == 1) {
        {
            path.insert(path.begin(), n);
            g[n].push_back(path[1]);
            g[path[1]].push_back(n);
            relax(path, g, ans);
            g[n].pop_back();
            g[path[1]].pop_back();
            path.erase(path.begin());
        }
        {
            path.push_back(n);
            g[path[(int)path.size() - 2]].push_back(n);
            g[n].push_back(path[(int)path.size() - 2]);
            relax(path, g, ans);
            g[n].pop_back();
            g[path[(int)path.size() - 2]].pop_back();
            path.pop_back();
        }

    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    vector<vector<int>> g(n);
    vector<int> sz(n);
    for (int i = 0; i + 1 < n; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
        sz[a]++;
        sz[b]++;
    }
    vector<int> lf;
    for (int i = 0; i < n; i++) {
        if (sz[i] == 1) {
            lf.push_back(i);
        }
    }
    vector<vector<int>> pathes;
    int d = 0;
    for (int i = 0; i < (int)lf.size(); i++) {
        for (int j = i + 1; j < (int)lf.size(); j++) {
            vector<int> p = path(n, g, lf[i], lf[j]);
            d = max(d, (int)p.size());
            pathes.push_back(p);
        }
    }
    int ans = INF;
    for (auto t : pathes) {
        if ((int)t.size() == d) {
            try_path(t, g, ans);
        }
    }
    cout << (d + 1) / 2 << ' ' << ans << endl;
}   

Compiler output:
a.cc: In function 'void relax(std::vector<long long int>, std::vector<std::vector<long long int> >, long long int&)':
a.cc:33:5: error: 'function' was not declared in this scope
   33 |     function<void(int,int)> dfs = [&](int cur, int ch) {
      |     ^~~~~~~~
a.cc:21:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
   20 | #include <random>
  +++ |+#include <functional>
   21 | 
a.cc:33:26: error: expression list treated as compound expression in functional cast [-fpermissive]
   33 |     function<void(int,int)> dfs = [&](int cur, int ch) {
      |                          ^
a.cc:33:14: error: expected primary-expression before 'void'
   33 |     function<void(int,int)> dfs = [&](int cur, int ch) {
      |              ^~~~
a.cc:46:9: error: 'dfs' was not declared in this scope; did you mean 'ffs'?
   46 |         dfs(diam[(int)diam.size() / 2], 0);
      |         ^~~
      |         ffs
a.cc:51:9: error: 'dfs' was not declared in this scope; did you mean 'ffs'?
   51 |         dfs(diam[p], 0);
      |         ^~~
      |         ffs
a.cc: In function 'std::vector<long long int> path(long long int, std::vector<std::vector<long long int> >, long long int, long long int)':
a.cc:67:5: error: 'function' was not declared in this scope
   67 |     function<void(int, int)> dfs = [&](int cur, int p) {
      |     ^~~~~~~~
a.cc:67:5: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
a.cc:67:27: error: expression list treated as compound expression in functional cast [-fpermissive]
   67 |     function<void(int, int)> dfs = [&](int cur, int p) {
      |                           ^
a.cc:67:14: error: expected primary-expression before 'void'
   67 |     function<void(int, int)> dfs = [&](int cur, int p) {
      |              ^~~~
a.cc:76:5: error: 'dfs' was not declared in this scope; did you mean 'ffs'?
   76 |     dfs(a, -1);
      |     ^~~
      |     ffs