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 ll long long
#define endl "\n" 
#define ull unsigned long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define lowbit(x) (x & -(x))
#define INF 0x3f3f3f3f
#define ls(p) p << 1
#define rs(p) p << 1 | 1
const double eps = 1e-6;
const double PI = acos(double(-1));
const ull base = 131;
const double C = 0.5772156649;
const int mod = 1000007;
const int N = 2e5 + 7;
unordered_map<int, int> mp;
vector<int> G[N];
int a[N],b[N];
void solve()
{
    int n;
    cin >> n;
    mp.clear();
    for (int i = 1; i <= n;i++){
        G[i].clear();
    }
    int res = 0;
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
        mp[u]++;
        mp[v]++;
        res = max(res, max(mp[u], mp[v]));
    }
    int k = 0;
    for (int i = 1; i <= n; i++)
    {
        a[i] = mp[i];
        if(mp[i]==res){
            b[++k] = i;
        }
    }
    sort(a + 1, a + n + 1);
    int maxn=0;
    for (int i = 1; i <= k; i++)
    {
        priority_queue<int> q;
        for(auto w:G[b[i]]){
            q.push(mp[w]);
        }
        int kk = n - 1;
        while(!q.empty()&&a[kk]==q.top()){
            kk--;
            q.pop();
        }
        maxn = max(maxn, res + a[kk] - 1);
    }
    cout << maxn << endl;
}
int main()
{
    IOS;
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n" 
#define ull unsigned long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define lowbit(x) (x & -(x))
#define INF 0x3f3f3f3f
#define ls(p) p << 1
#define rs(p) p << 1 | 1
const double eps = 1e-6;
const double PI = acos(double(-1));
const ull base = 131;
const double C = 0.5772156649;
const int mod = 1000007;
const int N = 2e5 + 7;
unordered_map<int, int> mp;
vector<int> G[N];
int a[N],b[N];
void solve()
{
    int n;
    cin >> n;
    mp.clear();
    for (int i = 1; i <= n;i++){
        G[i].clear();
    }
    int res = 0;
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
        mp[u]++;
        mp[v]++;
        res = max(res, max(mp[u], mp[v]));
    }
    int k = 0;
    for (int i = 1; i <= n; i++)
    {
        a[i] = mp[i];
        if(mp[i]==res){
            b[++k] = i;
        }
    }
    sort(a + 1, a + n + 1);
    int maxn=0;
    for (int i = 1; i <= k; i++)
    {
        priority_queue<int> q;
        for(auto w:G[b[i]]){
            q.push(mp[w]);
        }
        int kk = n - 1;
        while(!q.empty()&&a[kk]==q.top()){
            kk--;
            q.pop();
        }
        maxn = max(maxn, res + max(a[kk],a[n-1]-1) - 1);
    }
    cout << maxn << endl;
}
int main()
{
    IOS;
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}