Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;

#define ll long long int
const ll M = 1e9 + 7; 
#define aa ios::sync_with_stdio(false)
#define bb cin.tie(nullptr)

void solve() {
    ll n; cin >> n;
    set<pair<ll, ll>> st;
    vector<ll> deg(n);
    for(ll i = 0; i < n - 1; ++i){
        ll a, b; cin >> a >> b;
        st.insert({min(a, b) - 1, max(a, b) - 1});
        deg[a - 1]++;
        deg[b - 1]++;
    }

    map<ll, vector<ll>> mpp;
    for(ll i = 0; i < n; ++i){
        mpp[deg[i]].push_back(i);
    }

    vector<pair<ll, vector<ll>>> v;
    for(auto it : mpp) {
        v.push_back({it.first, it.second});
    }

    sort(v.begin(), v.end());

    // for(auto it : v){
    //   cout<<it.first<<" : ";
    //   for(auto val : it.second){
    //     cout<<val<<" ";
    //   }cout<<endl;
    // }cout<<endl;

    ll cnt = 0;
    ll sm = 0;
    ll ele1 = -1;
    ll ele2 = -1;
    for(ll i = v.size()-1; i >= 0; --i){
        ll sz = v[i].second.size() - 1;
        // cout<<v[i].second.size()<<"hh\n";
        while(cnt < 2) {
            sm += v[i].first;
            if(ele1 == -1) {
                ele1 = v[i].second[v[i].second.size() - 1 - sz];
                // cout<<ele1<<"h\n";
            }else if(ele2 == -1) {
                ele2 = v[i].second[v[i].second.size() - 1 - sz];
            }
            sz--;
            cnt++;
            if(sz == -1) break;
        }


        if(v[v.size() - 1].second.size() >= 3) {
            cout<<(sm - 1)<<"\n";
            return;
        }else if(cnt == 2) {
            // cout<<ele1<<" "<<ele2<<" "<<sm<<"\n";
            if(st.find({min(ele1, ele2), max(ele1, ele2)}) == st.end()) {
                cout<<(sm - 1)<<"\n";
                return;
            }
            cout<<(sm - 2)<<"\n";
            return;
        } 
       
    }




}

int main() {
    aa; bb;
    ll t; cin >> t;
    while(t--) solve();

    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

#define ll long long int
const ll M = 1e9 + 7; 
#define aa ios::sync_with_stdio(false)
#define bb cin.tie(nullptr)

void solve() {
    ll n; cin >> n;
    set<pair<ll, ll>> st;
    vector<ll> deg(n);
    for(ll i = 0; i < n - 1; ++i){
        ll a, b; cin >> a >> b;
        st.insert({min(a, b) - 1, max(a, b) - 1});
        deg[a - 1]++;
        deg[b - 1]++;
    }

    map<ll, vector<ll>> mpp;
    for(ll i = 0; i < n; ++i){
        mpp[deg[i]].push_back(i);
    }

    vector<pair<ll, vector<ll>>> v;
    for(auto it : mpp) {
        v.push_back({it.first, it.second});
    }

    sort(v.begin(), v.end());

    ll cnt = 0;
    ll sm = 0;
    ll ele1 = -1;
    ll ele2 = -1;
    ll realcnt = 0;
    set<ll> chk;
    for(ll i = v.size()-1; i >= 0; --i){
        ll sz = v[i].second.size();
        // cout<<v[i].second.size()<<"hh\n";
        while(cnt < 2) {
            sm += v[i].first;
            if(ele1 == -1) {
                ele1 = v[i].second[v[i].second.size() - sz];
                // cout<<ele1<<"h\n";
            }else if(ele2 == -1) {
                ele2 = v[i].second[v[i].second.size() - sz];
            }
            sz--;
            cnt++;
            if(sz == 0) break;
        }
        realcnt += v[i].second.size();
        if(cnt >= 2) {
            for(auto it : v[i].second) {
                chk.insert(it);
            }
        }


        if(v[v.size() - 1].second.size() >= 3) {
            cout<<(sm - 1)<<"\n";
            return;
        }else if(v[v.size() - 1].second.size() == 2) {
            if(st.find({min(ele1, ele2), max(ele1, ele2)}) == st.end()) {
                cout<<(sm - 1)<<"\n";
                return;
            }
            cout<<(sm - 2)<<"\n";
            return;
        } else if(cnt == 2) {
            for(auto it : chk) {
                if(st.find({min(ele1, it), max(ele1, it)}) == st.end()) {
                    cout<<(sm - 1)<<"\n";
                    return;
                }
            }
            cout<<(sm - 2)<<"\n";
            return;
        } 

        
       
    }




}

int main() {
    aa; bb;
    ll t; cin >> t;
    while(t--) solve();

    return 0;
}