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

Previous submission (verdict: Wrong answer on test 4):
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast_IO ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ll long long
#define all(v) (v).begin(),(v).end()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define fi first 
#define se second
#define pb push_back

/*------------- Hardwork beats Talent, when Talent doesn't work hard!! -------------*/
const ll infl = 1e17 + 7;
const int inf = 1e8 + 7;
const int mod = 1e9 + 7;


void Hrid_solve(){
    int n;
    cin >> n;
    vector<int> deg(n+1);
    set<pair<int,int>> st;
    for(int i=0; i<n-1; i++){
        int u,v;
        cin >> u >> v;
        deg[u]++;
        deg[v]++;
        if(u>v) swap(u,v);
        st.insert({u,v});
    }

    vector<pair<int,int>> vp;
    for(int i=1; i<=n; i++){
        vp.pb({deg[i],i});
    }
    sort(all(vp), greater<pair<int,int>>());

    int ans =-1; 
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            int u = vp[i].se, v = vp[j].se;
            if(u>v) swap(u,v);
            if(!st.count({u,v})){
                ans = max(ans, vp[i].fi+vp[j].fi-1);
                break;
            }
        }
        if(ans != -1) break;
    }
    for(auto [u,v]: st){
        ans = max(ans, deg[u]+deg[v]-2);
    }

    cout << ans << endl;

}


/*------------------ Today's decision might impact you Tomorrow !! ------------------*/

int32_t main(){
    fast_IO;
    // #ifndef ONLINE_JUDGE
    //     freopen("input.txt", "r", stdin);
    //     // freopen("output.txt", "w", stdout);
    // #endif

    int tc = 1;
    cin >> tc;
    for(int kase = 1; kase <= tc; kase++){
        // cout << "Case " << kase << ": \n";
        Hrid_solve();
    }

    return 0;
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast_IO ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ll long long
#define all(v) (v).begin(),(v).end()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define fi first 
#define se second
#define pb push_back

/*------------- Hardwork beats Talent, when Talent doesn't work hard!! -------------*/
const ll infl = 1e17 + 7;
const int inf = 1e8 + 7;
const int mod = 1e9 + 7;


void Hrid_solve(){
    int n;
    cin >> n;
    vector<int> deg(n+1);
    set<pair<int,int>> st;
    for(int i=0; i<n-1; i++){
        int u,v;
        cin >> u >> v;
        deg[u]++;
        deg[v]++;
        if(u>v) swap(u,v);
        st.insert({u,v});
    }

    vector<pair<int,int>> vp;
    for(int i=1; i<=n; i++){
        vp.pb({deg[i],i});
    }
    sort(all(vp), greater<pair<int,int>>());

    int ans =-1; 
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            int u = vp[i].se, v = vp[j].se;
            if(u>v) swap(u,v);
            if(!st.count({u,v})){
                ans = max(ans, vp[i].fi+vp[j].fi-1);
                break;
            }
        }
    }
    for(auto [u,v]: st){
        ans = max(ans, deg[u]+deg[v]-2);
    }

    cout << ans << endl;

}


/*------------------ Today's decision might impact you Tomorrow !! ------------------*/

int32_t main(){
    fast_IO;
    // #ifndef ONLINE_JUDGE
    //     freopen("input.txt", "r", stdin);
    //     // freopen("output.txt", "w", stdout);
    // #endif

    int tc = 1;
    cin >> tc;
    for(int kase = 1; kase <= tc; kase++){
        // cout << "Case " << kase << ": \n";
        Hrid_solve();
    }

    return 0;
}