Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 374 ms):
#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;
}


Solution 2 (runtime: 937 ms):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//ll m = 1e9+7;


int main() {
	// your code goes here
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> a[n+1];
        map<pair<int,int>, int> e;
        for(int i=1; i<n; i++){
            int u, v;
            cin>>u>>v;
            a[u].push_back(v);
            a[v].push_back(u);
            e[{u,v}]=1;
            e[{v,u}]=1;
        }
      //  cout<<"#";
        vector<int> p;
        map<int, vector<int>> m; 
        for(int i=1;i<=n;i++){
            p.push_back(a[i].size());
         //   cout<<a[i].size()<<" "<<i<<endl;
            m[a[i].size()].push_back(i);
        }
        sort(p.begin(), p.end());
        // for(auto i:p)
        //     cout<<i<<" ";
        // cout<<endl;
        int k=p.size(), ans;
       // cout<<k<<" "<<p[k-1]<<" " <<m[p[k-1]].size()<<endl;
        if(m[p[k-1]].size()>2){
            //cout<<"@";
            ans=2*p[k-1]-1;
           // cout<<p[k-1]<<" " <<m[p[k-1]].size()<<endl;
        }
        else if(m[p[k-1]].size()==2){
            //cout<<"@";
            if(e.find({m[p[k-1]][0],m[p[k-1]][1]})==e.end()){
                ans=2*p[k-1]-1;
            }
            else
                ans=2*p[k-1]-2;
            
        }
        else{
          //  cout<<"#";
            int s = m[p[k-2]].size();
           // cout<<s<<endl;
            ans=p[k-1]+p[k-2]-2;
            for(int i=0;i<s;i++){
                if(e.find({m[p[k-2]][i], m[p[k-1]][0]})==e.end()){
                 //   cout<<m[p[k-2]][i]<<" "<<m[p[k-1]][0]<<endl;
                    ans=p[k-1]+p[k-2]-1;
                }
            }
        }
        // if(m.size()>0){
        //     if(m.size()==1){
        //         if(b==1)
        //             cout<<"2"<<endl;
        //         else
        //             cout
        //     }
        //     for(int i=1;i<=n;i++){
        //         if(m.find(i)!=m.end()){
        //             for(auto j : a[i]){
        //                 if(m.find(j))
        //             }
        //         }
        //     }
        // }
        // else{
        //     cout<<min(n-2, 3)<<endl;
        // }
        cout<<ans<<endl;
        
        
        
    }
        
}