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

Solution 1 (runtime: 358 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());

    // 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;
    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: 687 ms):
#include <bits/stdc++.h>
using namespace std;
#define ll long long 

void jawaab (int i) {
    int n;
    cin>>n;
    int u,v;
    vector<set<int>>graph(n+1);
    for(int i=0; i<n-1; i++) {
        cin>>u>>v;
        u--; v--;
        graph[u].insert(v);
        graph[v].insert(u);
    }
    set<pair<int,int>>s;
    for(int i=0; i<n; i++) {
        s.insert({graph[i].size(),i});
    }
    if(n <= 2) {
        cout<<0<<endl;
        return;
    }
    // for(auto i: s) cout<<i.first<<" "<<i.second<<endl;
    pair<int,int> x = *s.rbegin();
    int p = x.first;
    int q = x.second;
    // cout<<p<<" : "<<q<<endl;
    s.erase({p,q});
    int cnt = 0;
    for(int i=0; i<n; i++) {
        if(graph[i].size() == x.first) cnt++;
    }
    if(cnt >= 3) {
        cout<<2*p-1<<endl;
        return;
    }
    int ans = 0;
    for(auto node: graph[x.second]) {
        int sz = graph[node].size();
        ans = max(ans, x.first + sz - 2);
        s.erase({graph[node].size(),node});
    }
    // for(auto i: s) cout<<i.first<<" "<<i.second<<endl;
    // s.erase({graph[q].size(),q});
    if(!s.empty()) {
        pair<int,int> y = *(s.rbegin());
        ans = max(ans, x.first + y.first - 1);
    }
    cout<<ans<<endl;
}
int main() {
	int t;
	cin>>t;
	for(int i=1; i<=t; i++) {
	    jawaab(i);   
	}
}