Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#pragma GCC optimize("O3,unroll-loops")

using namespace std;

#define ll long long
#define endl '\n'
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr(i, j, n) for (int i = j; i < n; i++)
#define fastIO ios::sync_with_stdio(false), cin.tie(NULL);
#define ll1 (ll)1
#define ff first
#define ss second
#define YES cout << "YES\n"
#define NO cout << "NO\n"

#ifndef ONLINE_JUDGE
#include "template.cpp"
#else
#define debug(...)
#define debugArr(...)
#endif

typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set;
const int MOD = 1e9 + 7;

/*
unordered_map<ll, ll> mp1;
mp.reserve(1024);
mp.max_load_factor(0.25);
*/

template <typename T>
T _gcd(T __m, T __n)
{
    while (__n != 0)
    {
        T __t = __m % __n;
        __m = __n;
        __n = __t;
    }
    return __m;
}
template <typename T>
T Set(T N, T pos) { return N = N | (1 << pos); }
template <typename T>
T reset(T N, T pos) { return N = N & ~(1 << pos); }
template <typename T>
bool check(T N, T pos) { return (bool)(N & (1 << pos)); }

int32_t main()
{
    fastIO;
    int t = 1;
    cin >> t;
    while (t--)
    {
        ll n, u, v;
        cin >> n;
        vector<ll> degree(n + 1);
        vector<set<ll>> adj(n + 1);
        for (int i = 1; i < n; i++)
        {
            cin >> u >> v;
            adj[u].insert(v), adj[v].insert(u);
            degree[u]++, degree[v]++;
        }
        vector<pair<ll, ll>> ndeg;
        for (int i = 1; i <= n; i++)
        {
            ndeg.push_back({degree[i], i});
        }
        sort(rall(ndeg));
        ll secondMax = ndeg[1].ff, ans = ndeg[0].ff, root = ndeg[0].ss, i = 1;
        set<ll> val;
        while (i < ndeg.size())
        {
            if (adj[root].find(ndeg[i].ss) != adj[root].end())
            {
                val.insert(ans + ndeg[i].ff - 2);
            }
            else
            {
                val.insert(ans + ndeg[i].ff - 1);
            }
            i++;
        }
        assert(!val.empty());
        cout << *val.rbegin() << endl;
    }
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#pragma GCC optimize("O3,unroll-loops")

using namespace std;

#define ll long long
#define endl '\n'
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr(i, j, n) for (int i = j; i < n; i++)
#define fastIO ios::sync_with_stdio(false), cin.tie(NULL);
#define ll1 (ll)1
#define ff first
#define ss second
#define YES cout << "YES\n"
#define NO cout << "NO\n"

#ifndef ONLINE_JUDGE
#include "template.cpp"
#else
#define debug(...)
#define debugArr(...)
#endif

typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set;
const int MOD = 1e9 + 7;

/*
unordered_map<ll, ll> mp1;
mp.reserve(1024);
mp.max_load_factor(0.25);
*/

template <typename T>
T _gcd(T __m, T __n)
{
    while (__n != 0)
    {
        T __t = __m % __n;
        __m = __n;
        __n = __t;
    }
    return __m;
}
template <typename T>
T Set(T N, T pos) { return N = N | (1 << pos); }
template <typename T>
T reset(T N, T pos) { return N = N & ~(1 << pos); }
template <typename T>
bool check(T N, T pos) { return (bool)(N & (1 << pos)); }

int32_t main()
{
    fastIO;
    int t = 1;
    cin >> t;
    while (t--)
    {
        ll n, u, v;
        cin >> n;
        set<pair<ll, ll>> st;
        vector<ll> degree(n + 1);
        vector<vector<ll>> adj(n + 1);
        for (int i = 1; i < n; i++)
        {
            cin >> u >> v;
            adj[u].push_back(v), adj[v].push_back(u);
            degree[u]++, degree[v]++;
        }
        for (int i = 1; i <= n; i++)
        {
            st.insert({degree[i], i});
        }
        set<ll> ans;
        for (int i = 1; i <= n; i++)
        {
            vector<pair<ll, ll>> nst;
            nst.push_back({degree[i], i});
            st.extract({degree[i], i});
            for (auto it : adj[i])
            {
                ans.insert(degree[i] + degree[it] - 2);
                nst.push_back({degree[it], it});
                st.extract({degree[it], it});
            }
            if (!st.empty())
            {
                auto it = *st.rbegin();
                ans.insert(degree[i] + it.ff - 1);
            }
            for (auto it : nst)
            {
                st.insert({it.ff, it.ss});
            }
        }
        cout << *ans.rbegin() << endl;
    }
    return 0;
}