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

Previous submission (verdict: Memory limit exceeded on test 9):
#include<iostream>
#include<vector>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);

    int t;
    cin >> t;
    while (t--)
    {
        long long n;
        cin >> n;
        vector<pair<long long, long long>>path(n - 1);
        vector<long long>num(n + 1, 0);
        long long times = 0;
        map<pair<long long,long long>,long long>mp;
        for (long long i = 0; i < n - 1; i++)
        {
            long long u, v;
            cin >> u >> v;
            path[i] = { u,v };
            num[u]++;
            num[v]++;
            times = max({ times,num[u],num[v] });
            mp[{min(u,v),max(u,v)}] = 1;
        }
        long long ans = 0;
        for (long long i = 1; i <= n; i++)
        {
            if (num[i] == times)
            {
                for (long long j = 1; j <= n; j++)
                {
                    if (i == j)
                        continue;
                    if (mp[{min(i, j), max(i,j)}] == 1)
                    {
                        ans = max({ ans , num[i] + num[j] - 2 });
                    }
                    else
                    {
                        ans = max({ans , num[i] + num[j] - 1});
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

Submission (verdict: Accepted):
#include<iostream>
#include<vector>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);

    int t;
    cin >> t;
    while (t--)
    {
        long long n;
        cin >> n;
        vector<pair<long long, long long>>path(n - 1);
        vector<long long>num(n + 1, 0);
        long long times = 0;
        map<pair<long long,long long>,long long>mp;
        for (long long i = 0; i < n - 1; i++)
        {
            long long u, v;
            cin >> u >> v;
            path[i] = { u,v };
            num[u]++;
            num[v]++;
            times = max({ times,num[u],num[v] });
            mp[{min(u,v),max(u,v)}] = 1;
        }
        long long ans = 0;
        for (long long i = 1; i <= n; i++)
        {
            if (num[i] == times)
            {
                for (long long j = 1; j <= n; j++)
                {
                    if (i == j)
                        continue;
                    if (mp[{min(i, j), max(i,j)}] == 1)
                    {
                        ans = max({ ans , num[i] + num[j] - 2 });
                    }
                    else
                    {
                        ans = max({ans , num[i] + num[j] - 1});
                    }
                }
            }
            if (ans == times * 2 - 1) break;
        }
        cout << ans << endl;
    }
    return 0;
}