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 ll long long

const ll MOD = 1e9 + 7;
const ll MAX_N = 5e5;

int main() {
    ll t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        ll temp = n - 1;
        map<ll, multiset<ll>> m;
        
        // Read input and construct adjacency list
        while (temp--) {
            ll l, r;
            cin >> l >> r;
            m[l].insert(r);
            m[r].insert(l);
        }

        ll v = -1;
        ll maxx = 0;
        ll ans = 0;

        for (auto& i : m) {
            if (i.second.size() > maxx) {
                maxx = i.second.size();
                v = i.first;
            }
        }

        if (v != -1) { 
            for (auto i : m[v]) {
                if (m.find(i) != m.end()) {
                    m[i].erase(v);
                }
            }
            m.erase(v);
            ans += maxx;
        }

        maxx = 0;
        v = -1;

     
        for (auto& i : m) {
            if (i.second.size() > maxx) {
                maxx = i.second.size();
                v = i.first;
            }
        }

        if (v != -1) { 
            for (auto i : m[v]) {
                if (m.find(i) != m.end()) {
                    m[i].erase(v);
                }
            }
            m.erase(v);
            ans += maxx;
        }

        cout << ans - 1 << endl;
    }
    return 0;
}


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define ll long long

const ll MOD = 1e9 + 7;
const ll MAX_N = 5e5;

int main() {
    ll t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        ll temp = n - 1;
        map<ll, multiset<ll>> m;
        
        while (temp--) {
            ll l, r;
            cin >> l >> r;
            m[l].insert(r);
            m[r].insert(l);
        }
        // for(auto i :m){
        //     cout<<i.first<<":";
        //     for(auto j:i.second) cout<<j<<" ";
        //     cout<<endl;
        // }
        ll v = -1;
        ll maxx = 0;
        ll ans = 0;
        ll cnt = 0;

        for (auto& i : m) {
            if (i.second.size() > maxx) {
                maxx = i.second.size();
                v = i.first;
            }
        }
        for(auto i : m){
            if(i.second.size() == maxx){
                cnt++;
            }
        }
        if(cnt>=3){
            cout<<2*maxx<<endl;
            continue;
        }
        if (v != -1) { 
            for (auto i : m[v]) {
                if (m.find(i) != m.end()) {
                    m[i].erase(v);
                }
            }
            m.erase(v);
            ans += maxx;
        }

        maxx = 0;
        v = -1;

     
        for (auto& i : m) {
            if (i.second.size() > maxx) {
                maxx = i.second.size();
                v = i.first;
            }
        }

        if (v != -1) { 
            for (auto i : m[v]) {
                if (m.find(i) != m.end()) {
                    m[i].erase(v);
                }
            }
            m.erase(v);
            ans += maxx;
        }
        // for(auto i :m){
        //     cout<<i.first<<":";
        //     for(auto j:i.second) cout<<j<<" ";
        //     cout<<endl;
        // }
        cout << ans - 1 << endl;
    }
    return 0;
}