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]++;
        }
        if (n == 2)
        {
            cout << 0 << endl;
            continue;
        }
        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 (ndeg[i].ff >= secondMax and 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++;
        }
        cout << *val.rbegin() << endl;
    }
    return 0;
}

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]++;
        }
        if (n == 2)
        {
            cout << 0 << endl;
            continue;
        }
        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() and ndeg[i].ff >= secondMax)
        {
            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++;
        }
        cout << *val.rbegin() << endl;
    }
    return 0;
}