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 <functional>
#include <random>

using namespace std;

#define int long long

typedef unsigned long long ull;

const ull INF = ULLONG_MAX / 2;

ull safe_mul(ull a, ull b) {
    if (a >= (INF + b - 1) / b) return INF;
    return min(a * b, INF);
}

void relax(vector<int> diam, vector<vector<int>> g, ull &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);
    }
    ull cur = 1;
    for (int i = 0; i < n; i++) {
        cur = safe_mul(cur, max((int)1, mxbh[i]));
    }
    if ((int)diam.size() % 2 == 0) {
        cur = safe_mul(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, ull &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);
        }
    }
    ull ans = INF;
    for (auto t : pathes) {
        if ((int)t.size() == d) {
            try_path(t, g, ans);
        }
    }
    cout << (d + 1) / 2 << ' ' << ans << endl;
    cerr << INF << endl;
}   

Compiler output:
a.cc:29:17: error: 'ULLONG_MAX' was not declared in this scope
   29 | const ull INF = ULLONG_MAX / 2;
      |                 ^~~~~~~~~~
a.cc:22:1: note: 'ULLONG_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   21 | #include <random>
  +++ |+#include <climits>
   22 |