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

Previous 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;
vector<vector<ll>> g;
vector<ll> sz, mx;
void dfs(ll u, ll par)
{

    for (auto v : g[u])
    {
        if (v != par)
        {
            dfs(v, u);
            sz[u]++;
        }
    }
}
ll ans = 0;
void dfs1(ll u, ll par)
{
    priority_queue<ll> q;
    mx[u] = sz[u];
    for (auto v : g[u])
    {
        if (v != par)
        {
            dfs1(v, u);
            q.push(mx[v]);
            mx[u] = max(mx[u], mx[v]);
        }
    }
    ll x = 0, y = 0;
    if (q.size() >= 2)
        x = q.top(), q.pop();
    if (q.size() >= 1)
        y = q.top(), q.pop();
    // cout << u << ' ' << x << " " << y << nl;
    ans = max(ans, 1 + max(x + y, x + sz[u] - 1));
}

void Solve()
{
    ll n, m, x, y, z, i, j, k, l, r, q, W;
    cin >> n;
    g = vector<vector<ll>>(n + 1);
    sz = mx = vector<ll>(n + 1);
    for (i = 2; i <= n; i++)
    {
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    ans = 0;
    dfs(1, -1);
    dfs1(1, -1);
    // cout << ans << nl;
    cout << min(ans, n - 2) << nl;
    // show(sz);-
}
int main()
{
    op();
    int test = 1, c = 1;
    cin >> test;
    while (test--)
    {
        // cin.ignore();
        // cout << "Case " << c++ << ": ";
        Solve();
    }
    return 0;
}

Submission (verdict: Accepted):
#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;

vector<vector<ll>> g;
vector<ll> sz;
void Solve()
{
    ll n, m, x, y, z, i, j, k, l, r, q, W;
    cin >> n;
    g = vector<vector<ll>>(n + 1);
    sz = vector<ll>(n + 1);
    set<pair<ll, ll>> s;
    for (i = 2; i <= n; i++)
    {
        cin >> x >> y;
        sz[x]++;
        sz[y]++;
        s.insert({x, y});
    }
    if (n == 2)
    {
        cout << 0 << nl;
        rr;
    }
    // show(sz);
    vector<pair<ll, ll>> v;
    for (i = 1; i <= n; i++)
    {
        v.push_back({sz[i], i});
    }
    ll ans = 0;
    rsrt(v);
    for (i = 0; i < v.size() && i < 5; i++)
    {
        for (j = i + 1; j < v.size(); j++)
        {
            x = v[i].second;
            y = v[j].second;
            l = v[j].first + v[i].first - 1 - (s.count({x, y}) || s.count({y, x}));
            ans = max(ans, 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;
}