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

Solution 1 (runtime: 249 ms):
#include<bits/stdc++.h>
#define st first
#define nd second
#define all(x) x.begin(), x.end()
#define BOOST cin.tie(NULL); ios_base::sync_with_stdio(false);
 
// #define int ll
typedef long long ll;

using namespace std;
template <typename T> struct tag:reference_wrapper <T>{ using reference_wrapper <T>::reference_wrapper; };
template <typename T1, typename T2> static inline tag <ostream> operator<<(tag <ostream> os, pair<T1, T2> const& p){ return os.get()<<"{"<<p.first<<", "<<p.second<<"}", os;}
template <typename Other> static inline tag <ostream> operator<<(tag <ostream> os, Other const& o){ os.get()<<o; return os; }
template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, vector <T> const& v){ os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; }
template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, set <T> const& s){ vector <T> v; for (auto i: s) v.push_back(i); os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; }


void solve(){
    int n; cin >> n;
    // vector<pair<int,int>> deg(n + 1);
    vector<int> deg(n + 1);
    
    // for(int i = 1; i <= n; i++) deg[i].nd = i;
    vector<vector<int>> g(n + 1, vector<int>());
    vector<pair<int,int>> e;
    int a,b;
    for(int i = 1; i < n; i++){
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
        e.push_back({a,b});
        deg[a]++;
        deg[b]++;
    }
    int ans = 1;
    int vtd = 0;
    vector<int> vbest;
    for(int i = 1; i <= n; i++){
        if(deg[i] > deg[vtd]){
            vtd = i;
            vbest.clear();
        }
        if(deg[i] == deg[vtd]){
            vbest.push_back(i);
        }
    }
    vtd = vbest[0];
    if(vbest.size() <= 2){
        ans += deg[vtd] - 1;
        for(auto u : g[vtd]){
            deg[u]--;
        }
        deg[vtd] = 0;
        for(int i = 1; i <= n; i++){
             if(deg[i] > deg[vtd]){
                vtd = i;
            }
        }
        ans += deg[vtd] - 1;
    } else {
        ans += deg[vtd] - 1;
        ans += deg[vtd] - 1;
    }
    cout << ans << '\n';

}

 
int32_t main(){
    BOOST;
 
    int q = 1; cin >> q;
    while(q--){
        solve();
    }
    return 0;
}


Solution 2 (runtime: 358 ms):
#include <iostream>
#include <cmath>
#include <vector>
#include <unordered_set>
using namespace std;
using ll=long long;
unordered_set<int> visited;
void dfs(int cur,vector<vector<int>>& adj){
    visited.emplace(cur);
    for(auto& x:adj[cur]){
        if(visited.find(x)==visited.end()) dfs(x,adj);
    }
}
int main(){
    ios::sync_with_stdio(false);
	cin.tie(nullptr);
    int t;
    cin >> t;
    while(t--){
        int n,visits=-1;
        visited.clear();
        cin >> n;
        vector<int> a(n+1,0);
        vector<vector<int>> adj(n+1);
        ll mx=0,cur=0;
        
        vector<int> pos;
        for(int i=1;i<n;i++){
            int u,v;
            cin >> u >> v;
            a[u]++;
            a[v]++;
            if(a[u]>cur){
                cur=a[u],mx=u;
                pos.clear();
                pos.push_back(u);
            }else if(a[u]==cur){
                pos.push_back(u);
            }
            if(a[v]>cur){
                cur=a[v],mx=v;
                pos.clear();
                pos.push_back(v);
            }else if(a[v]==cur){
                pos.push_back(v);
            }
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        for(int i=1;i<n+1;i++){
            if(visited.find(i)==visited.end()){
                dfs(i,adj);
                visits++;
            }
        }
        ll ans=a[mx]+visits;
        vector<int> singles(pos.size(),0);
        unordered_set<int> poses;
        for(auto&x:pos)poses.emplace(x);
        for(int i=0;i<pos.size();i++){
            for(auto& ver:adj[pos[i]]){
                if(poses.find(ver)==poses.end()) singles[i]++;
            }
        }
        int most=-1;
        for(int i=0;i<pos.size();i++){
            if(singles[i]>most) mx=pos[i],most=singles[i];
        }
        a[mx]=0;
        for(auto& vert:adj[mx]){
            a[vert]--;
        }
        mx=0,cur=0;
        for(int i=0;i<n+1;i++){
            if(a[i]>cur){
                cur=a[i],mx=i;
            }
        }
        ans+=cur-1;
        cout << ans << "\n";
    }
}