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

Previous 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;
}


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;



void solve() {
    ll n;
    cin >> n;

    vector<vector<int> > g(n+1);

    loop(i,0,n-1)
    {
        int a,b;
        cin>>a>>b;
        g[a].pb(b);
        g[b].pb(a);
    }
    if(n==2)
    {
        cout<<0<<endl;
        return;
    }

    multiset<int> s;
    int ans = 0;;
    loop(i,1,n+1) s.insert(int(g[i].size()));

    loop(i,1,n+1)
    {
        int curr = g[i].size();
        if(s.find(curr)!=s.end()){
             s.erase(int(g[i].size()));
        }
        for(auto j : g[i])
        {
           if(s.find(int(g[j].size()))!=s.end()){
             s.erase(int(g[j].size()));
        }
            s.insert(int(g[j].size())-1);
        }
        curr += *s.rbegin();
        ans = max(ans,curr);

        for(auto j : g[i])
        {
           if(s.find(int(g[j].size()-1))!=s.end()){
             s.erase(int(g[j].size()-1));
           }
            s.insert(int(g[j].size()));
        }

        s.insert(int(g[i].size()));
    }
    cout<<ans-1<<nl;
}

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;
}