Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define op()                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define srt(a) sort(a.begin(), a.end())
#define rsrt(a) sort(a.begin(), a.end(), greater<>())
#define fo(n) for (int i = 0; i < n; i++)
#define fo1(n) for (int i = 1; i <= n; i++)
#define all(a) a.begin(), a.end()
#define pb push_back
#define ll long long
#define Inf (ll)1e17 + 7
#define pi 3.1415926536
#define gcd __gcd
#define vi vector<int>
#define vl vector<ll>
#define nl '\n'
#define min_priority priority_queue<ll, vector<ll>, greater<ll>>
#define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update>
#define show(a)           \
    for (auto e : a)      \
        cout << e << " "; \
    cout << nl;
#define rY               \
    cout << "YES" << nl; \
    return;
#define rN              \
    cout << "NO" << nl; \
    return;
#define rr return
const ll mod = 1e9 + 7;
int lg = 20;
vector<vector<ll>> anc, g; // anc[u][i] = (2^i)th ancestor of node u
vector<ll> dep, sz;
void dfs(ll u, ll par)
{
    if (par != -1)
    {
        anc[u][0] = par;
        dep[u] = dep[par] + 1;
        for (int i = 1; i < lg; i++)
        {
            int v = anc[u][i - 1];
            if (v == -1)
                break;
            anc[u][i] = anc[v][i - 1]; // 16th ancestor of node u = 8th ancestor of node(8th ancestor of node u)
        }
    }
    for (auto v : g[u])
    {
        if (v != par)
        {
            dfs(v, u);
            sz[u]++;
        }
    }
}
ll kth(ll u, ll k)
{
    for (int i = 0; i < lg; i++)
    {
        if ((1LL << i) & k)
            u = anc[u][i];
        if (u == -1)
            return -1;
    }
    return u;
}
ll lca(ll u, ll v)
{
    if (dep[u] > dep[v])
        u = kth(u, dep[u] - dep[v]);
    else if (dep[v] > dep[u])
        v = kth(v, dep[v] - dep[u]);
    if (u == v)
        return u;
    for (int i = lg - 1; i >= 0; i--)
    {
        if (anc[u][i] != anc[v][i])
        {
            u = anc[u][i];
            v = anc[v][i];
        }
    }
    return anc[u][0];
}
ll distance(ll u, ll v)
{
    ll l = lca(u, v);
    return dep[u] + dep[v] - 2 * dep[l];
}
ll go(ll u, ll v, ll k) // kth node from u to v , 0th node is u
{
    ll l = lca(u, v);
    ll dis = dep[u] + dep[v] - 2 * dep[l];
    if (k > dis)
        return -1; // not possible
    if (dep[l] + k <= dep[u])
        return kth(u, k);
    k -= dep[u] - dep[l];
    return kth(v, dep[v] - dep[l] - k);
}
ll lca_for_given_root(ll r, ll u, ll v) // Lca for given root
{
    ll a = lca(u, v);
    ll b = lca(r, u);
    ll c = lca(r, v);
    if (a == b)
        return c;
    if (b == c)
        return a;
    return b;
}
void Solve()
{
    ll n, m, x, y, z, i, j, k, l, r, q, W;
    cin >> n;
    anc = vector<vector<ll>>(n + 1, vector<ll>(lg, -1));
    g = vector<vector<ll>>(n + 1);
    dep = sz = vector<ll>(n + 1);
    for (i = 2; i <= n; i++)
    {
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    if (n == 2)
    {
        cout << 0 << nl;
        rr;
    }
    dfs(1, -1);
    // show(sz);
    vector<pair<ll, ll>> v;
    for (i = 1; i <= n; i++)
    {
        v.push_back({sz[i], i});
    }
    ll ans = 1;
    rsrt(v);
    for (i = 0; i < v.size() && i < 100; i++)
    {
        for (j = i + 1; j < v.size() && j < 100; j++)
        {
            x = v[i].second;
            y = v[j].second;
            r = lca(x, y);
            l = v[j].first + v[i].first - (r == x || y == r);
            ans = max(ans, 1 + l);
        }
    }
    cout << min(n - 2, ans) << nl;
}
int main()
{
    op();
    int test = 1, c = 1;
    cin >> test;
    while (test--)
    {
        // cin.ignore();
        // cout << "Case " << c++ << ": ";
        Solve();
    }
    return 0;
}