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

Previous submission (verdict: Wrong answer on test 2):
#include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define vinput(v) for(int i=0;i<n;i++){cin>>v[i];}
#define loop(i,m,n) for(int i=m;i<n;i++)
#define vi vector<int>
#define vll vector<long long int>
#define pb push_back
#define ppb pop_back
#define MOD 1000000007
#define MOD1 998244353
#define nl endl 
#define mp make_pair
#define allr(x) (x).rbegin(),(x).rend()
#define ff first
#define sx(x) ((int)(x).size())
#define ss second
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define big1 998244353
#define big2 1000000007
#define all(v) v.begin(),v.end()
typedef long long ll;

vector<vector<ll> > arr; 
vector<set<ll> > adj;
set<pair<ll,ll> > d;
vector<ll> a;

void dfs(int node, int par) {
    for (auto i : arr[node]) {
        if (i != par) {
            auto it = d.find(mp(a[i], i));
            if (it != d.end()) {
                d.erase(it);
                d.insert(mp(a[i] - 1, i));
            }
        }
    }
}

void solve() {
    ll n;
    cin >> n;
    arr.assign(n + 1, vector<ll>());
    adj.assign(n + 1, set<ll>());
    a.assign(n + 1, 0);
    
    for (ll i = 1; i < n; i++) {
        ll u, v;
        cin >> u >> v;
        arr[u].pb(v);
        arr[v].pb(u);
        adj[u].insert(v);
        adj[v].insert(u);
        a[u]++;
        a[v]++;
    }

    if (n == 2) {
        cout << 0 << endl;
        return;
    }

    for (ll i = 1; i <= n; i++) {
        d.insert(mp(a[i], i));
    }

    ll ans = 0;
    auto it = d.rbegin();
    ans += it->ff;
    dfs(it->ss, 0);
    
    d.erase(prev(it.base()));  // Corrected erase
    it = d.rbegin();
    
    ans += it->ff;
    ans--;

    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
#endif
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


Submission (verdict: Wrong answer on test 4):
#include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define vinput(v) for(int i=0;i<n;i++){cin>>v[i];}
#define loop(i,m,n) for(int i=m;i<n;i++)
#define vi vector<int>
#define vll vector<long long int>
#define pb push_back
#define ppb pop_back
#define MOD 1000000007
#define MOD1 998244353
#define nl endl 
#define mp make_pair
#define allr(x) (x).rbegin(),(x).rend()
#define ff first
#define sx(x) ((int)(x).size())
#define ss second
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define big1 998244353
#define big2 1000000007
#define all(v) v.begin(),v.end()
typedef long long ll;

vector<vector<ll> > arr; 
vector<set<ll> > adj;
set<pair<ll,ll> > d;
vector<ll> a;

void dfs(int node, int par) {
    for (auto i : arr[node]) {
        if (i != par) {
            auto it = d.find(mp(a[i], i));
            if (it != d.end()) {
                d.erase(it);
                d.insert(mp(a[i] - 1, i));
            }
        }
    }
}

void solve() {
    ll n;
    cin >> n;
    arr.clear();
    adj.clear();
    a.clear();
    d.clear();
    arr.assign(n + 1, vector<ll>());
    adj.assign(n + 1, set<ll>());
    a.assign(n + 1, 0);
    
    for (ll i = 1; i < n; i++) {
        ll u, v;
        cin >> u >> v;
        arr[u].pb(v);
        arr[v].pb(u);
        adj[u].insert(v);
        adj[v].insert(u);
        a[u]++;
        a[v]++;
    }

    if (n == 2) {
        cout << 0 << endl;
        return;
    }

    for (ll i = 1; i <= n; i++) {
        d.insert(mp(a[i], i));
    }

    ll ans = 0;
    auto it = d.rbegin();
    ans += it->ff;
    dfs(it->ss, 0);
    
    d.erase(prev(it.base()));  // Corrected erase
    it = d.rbegin();
    
    ans += it->ff;
    ans--;

    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
#endif
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}