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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define mset multiset
#define umap unordered_map
#define PII pair<int, int>
#define fi first
#define se second
#define lc u << 1
#define rc u << 1 | 1
#define lowbit(x) (x & (-x))
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 998244353;
const int INF = 1e18;
const int N = 2e5 + 10;
//==================================================================================
void solve()
{
    int n;
    cin >> n;
    int ans = 0;
    vector<int> in(n + 1);
    vector<vector<int>> g(n + 1, vector<int>());
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        in[u]++;
        in[v]++;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    multiset<int> mstt;
    for (int i = 1; i <= n; i++)
    {
        mstt.insert(in[i]);
    }
    for (int u = 1; u <= n; u++)
    {
        int sum = 1;
        auto mst = mstt;
        mst.erase(mst.find(in[u]));
        for (auto v : g[u])
        {
            mst.erase(in[v]);
            mst.insert(in[v] - 1);
        }
        sum += in[u] - 1;
        sum += *mst.rbegin() - 1;
        ans = max(ans, sum);
    }
    cout << ans << '\n';
}
//==================================================================================
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // freopen("test.in", "r", stdin);
    // freopen("test.out", "w", stdout);
    int T_T = 1;
    cin >> T_T;
    while (T_T--)
        solve();
    return 0;
}

Submission (verdict: Time limit exceeded on test 3):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define mset multiset
#define umap unordered_map
#define PII pair<int, int>
#define fi first
#define se second
#define lc u << 1
#define rc u << 1 | 1
#define lowbit(x) (x & (-x))
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 998244353;
const int INF = 1e18;
const int N = 2e5 + 10;
//==================================================================================
void solve()
{
    int n;
    cin >> n;
    int ans = 0;
    vector<int> in(n + 1);
    vector<vector<int>> g(n + 1, vector<int>());
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        in[u]++;
        in[v]++;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    multiset<int> mstt;
    for (int i = 1; i <= n; i++)
    {
        mstt.insert(in[i]);
    }
    for (int u = 1; u <= n; u++)
    {
        int sum = 1;
        auto mst = mstt;
        mst.erase(mst.find(in[u]));
        for (auto v : g[u])
        {
            mst.erase(mst.find(in[v]));
            mst.insert(in[v] - 1);
        }
        sum += in[u] - 1;
        sum += *mst.rbegin() - 1;
        ans = max(ans, sum);
    }
    cout << ans << '\n';
}
//==================================================================================
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // freopen("test.in", "r", stdin);
    // freopen("test.out", "w", stdout);
    int T_T = 1;
    cin >> T_T;
    while (T_T--)
        solve();
    return 0;
}