Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define  nl "\n"
#define forn(a, c) for (int a = 0; a < c; a++) 
#define forl(a, b, c) for (int a = b; a <= c; a++) 
#define forr(a, b, c) for (int a = b; a >= c; a--)
#define cin(v) for(auto &i:v){cin>>i;}
#define cout(v) for(auto &i:v){cout<<i<<" ";}cout<<nl;
#define all(a) a.begin(),a.end()
#define fast() (ios_base::sync_with_stdio(false), cin.tie(NULL));
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

void solve(){
    int n;cin>>n;
    std::vector<pair<int,int>> v;
    map<int,int> mp;
    int mx = 0;
    int e1 = 0;
    forn(i,n-1){
        int x,y;cin>>x>>y;
        v.push_back({x,y});
        mp[x]++;
        mp[y]++;
        if( mx < mp[x]){mx = mp[x];e1 = x;}
        if(mx < mp[y]){mx = mp[y];e1 = y;}
    }
    sort(v.begin(),v.end());
    int mx2 = 0;
    int e2 = 0;
    for(auto &i:mp){
        if(mx-mx2 > mx-i.second && e1 != i.first){mx2 = i.second;e2 = i.first;}
    }
    // cout<<mx<<" "<<mx2<<nl;
    // cout<<e1<<" "<<e2<<nl;
    int c = mx2 + mx -1;
    // cout<<edge[e1]<<nl;
    bool flag = false;
    pair<int,int> p = {e1,e2};
    pair<int,int> pp = {e2,e1};
    // cout<<binary_search(v.begin(),v.end(),p)<<nl;
    if(binary_search(v.begin(),v.end(),p) || binary_search(v.begin(),v.end(),pp)) flag = true;
    if(flag)c--;
    cout<<c<<nl;
}

signed main()
{
    auto begin = std::chrono::high_resolution_clock::now();
    fast();
    int tt;cin>>tt;
    while(tt--){
        solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
}


Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define  nl "\n"
#define forn(a, c) for (int a = 0; a < c; a++) 
#define forl(a, b, c) for (int a = b; a <= c; a++) 
#define forr(a, b, c) for (int a = b; a >= c; a--)
#define cin(v) for(auto &i:v){cin>>i;}
#define cout(v) for(auto &i:v){cout<<i<<" ";}cout<<nl;
#define all(a) a.begin(),a.end()
#define fast() (ios_base::sync_with_stdio(false), cin.tie(NULL));
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

void solve(){
    int n;cin>>n;
    std::vector<pair<int,int>> v;
    map<int,int> mp;
    int mx = 0;
    int e1 = 0;
    forn(i,n-1){
        int x,y;cin>>x>>y;
        v.push_back({min(x,y),max(x,y)});
        mp[x]++;
        mp[y]++;
        if( mx < mp[x]){mx = mp[x];e1 = x;}
        if(mx < mp[y]){mx = mp[y];e1 = y;}
    }
    sort(v.begin(),v.end());
    int mx2 = 0;
    int e2 = 0;
    for(auto &i:mp){
        if(mx-mx2 > mx-i.second && e1 != i.first){mx2 = i.second;e2 = i.first;}
    }
    int cn1=0,cn2=0;
    for(auto &i:mp){
        if(i.second == mx)cn1++;
        if(i.second == mx2)cn2++;
    }

    // cout<<mx<<" "<<mx2<<nl;
    // cout<<e1<<" "<<e2<<nl;
    int c = mx2 + mx -1;
    // cout<<edge[e1]<<nl;
    bool flag = false;
    pair<int,int> p = {e1,e2};
    pair<int,int> pp = {e2,e1};
    if(mx2 == mx){
        if(cn1 > 2){cout<<c<<nl;return;}
        if(binary_search(v.begin(),v.end(),p) || binary_search(v.begin(),v.end(),pp))c--;
        cout<<c<<nl;return;
    }
    // if(binary_search(v.begin(),v.end(),p) || binary_search(v.begin(),v.end(),pp)) flag = true;
    // if(flag)c--;
    // cout<<c<<nl;
    for(auto &i:mp){
        if(i.second == mx2){
            pair<int,int> pr = {max(e1,i.first),min(e1,i.first)};
            if(binary_search(v.begin(),v.end(),pr) == false){cout<<c<<nl;return;}
        }
    }
    cout<<c-1<<nl;
}

signed main()
{
    auto begin = std::chrono::high_resolution_clock::now();
    fast();
    int tt;cin>>tt;
    while(tt--){
        solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
}