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

Previous submission (verdict: Time 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;
long long get_num(long long max_num,long long max_num_index,vector<pair<long long,long long>>&path,long long n)
{
    long long ans = max_num;
    long long m = 0;
    vector<long long>nums(n + 1, 0);
    for (long long i = 0; i < n - 1; i++)
    {
        long long u = path[i].first;
        long long v = path[i].second;
        if (u == max_num_index || v == max_num_index)
        {
            continue;
        }
        else
        {
            nums[u]++;
            nums[v]++;
            m = max(m, max(nums[u], nums[v]));
        }
    }
    return ans + m - 1;
}
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);
        for (long long i = 0; i < n - 1; i++)
        {
            long long u, v;
            cin >> u >> v;
            path[i] = { u,v };
            num[u]++;
            num[v]++;
        }
        long long max_num = 0;
        long long max_num_index = 0;
        for (long long i = 1; i <= n; i++)
        {
            if (max_num < num[i])
            {
                max_num = num[i];
                max_num_index = i;
            }
        }
        long long ans = 0;
        for (long long i = 1; i <= n; i++)
        {
            if (num[i] == max_num)
            {
                ans = max(ans , get_num(max_num, i, path, n));
            }
        }
        cout << ans << endl;
        //long long ans = max_num;
        //long long m = 0;
        //vector<long long>nums(n + 1, 0);
        //for (long long i = 0; i < n - 1; i++)
        //{
        //    long long u = path[i].first;
        //    long long v = path[i].second;
        //    if (u == max_num_index || v == max_num_index)
        //    {
        //        continue;
        //    }
        //    else
        //    {
        //        nums[u]++;
        //        nums[v]++;
        //        m = max(m, max(nums[u], nums[v]));
        //    }
        //}
        //cout << ans + m - 1 << endl;
    }
    return 0;
}

Submission (verdict: Memory limit exceeded on test 5):
#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;
        vector<vector<long long>>mp(n + 1, vector<long long>(n + 1, 0));
        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;
}