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

Solution 1 (runtime: 265 ms):
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace __gnu_pbds;
using namespace std;
//pbds
// X.find_by_order(k) return kth element. 0 indexed.
// X.order_of_key(k) returns count of elements strictly less than k.
 
#define ll                         long long
#define vi                          vector<int>
#define vvi                         vector<vector<int>> 
#define f(n) for(ll i=0;i<n;i++)
#define f1(v) for(auto r:v) cout<<r<<" "
#define nl cout<<endl
#define rep(i,a,b)                  for(int i = a; i < b; i++)
#define repr(i,a,b)                 for(int i=a;i>=b;i--)
#define F                          first
#define S                          second
#define maxv(a) *max_element(a.begin(),a.end())
#define minv(a) *min_element(a.begin(),a.end())
#define acc accumulate(a.begin(),a.end())
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define zero cout<<"0"<<endl
#define minus cout<<"-1"<<endl
#define si                          set<int>
#define sc                          set<char>
#define out return
#define mii                         map<int,int> 
#define mci                         map<char,int>
#define pb                          push_back
#define pp                          pop_back
#define msi                         map<string, int>
#define mis                         map<int, string>
#define in                          insert
// #define mm                         1000000007
#define pi                          3.14159265358979323846
#define prec(x)                     cout << fixed << setprecision(x)
#define bpc(x)                      __builtin_popcountll(x)
#define ull                         unsigned long long
#define sv(v) sort(v.begin(),v.end())
#define sv1(v) sort(v.begin(),v.end(),greater<ll>())
#define svp(v) sort(v.begin(),v.end(),greater<pair<ll,ll>>())
#define all(x)                      x.begin(), x.end()
#define read(a, n)                  rep(i, 0, n)    cin >> a[i]
#define print(a, n)                 rep(i, 0, n)    cout << a[i] << " "; cout << endl
vector<int>prime;
set<int>prime_set;
void fastio()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}




void solve(){
 ll n;
 cin>>n;
 
 ll x,y;
// map<ll,vector<ll>> m;
 vector<vector<ll>> v(n);
 f(n-1){
   cin>>x>>y;
  // m[x-1].pb(y-1);
  // m[y-1].pb(x-1);
   v[x-1].pb(y-1);
   v[y-1].pb(x-1);
 }
vector<pair<ll,ll>> v1;
ll i=0;
for(i;i<n;){
  v1.pb({v[i].size(),i});
  i++;
}
svp(v1);
// for(auto x:v1){
//   cout<<x.first<<" "<<x.second<<endl;
// }
x=v1[0].second;
ll p=0;
for(auto y:v1){
  if(y.first==v[x].size()){
    p++;
    if(p==3) break;
  }
  else break;
  
}
if(p>=3){
  cout<<2*v[x].size()-1<<endl;
  // yes;
  out;
}
// cout<<x<<" ";
ll s=0;
s+=v[x].size()-1;
// cout<<s<<" ";
v[x].clear();
for (int i = 0; i < n; i++) {
        for(auto y=v[i].begin();y!=v[i].end();y++){
          if(*y==x) {v[i].erase(y);
          break;}
        }
    }
    
v1.clear();
i=0;
for(i;i<n;i++){
  v1.pb({v[i].size(),i});
}
svp(v1);
// nl;
// for(auto x:v1){
//   cout<<x.first<<" "<<x.second<<endl;
// }
x=v1[0].second;
// cout<<x<<" ";
s+=v[x].size();
// cout<<s<<" ";
// if(v[x].size()==0) s++;
 


cout<<s<<endl;
 
 
 
 
 
 
 
 
 
 
 
 
                          
}
 
int32_t main(){
   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    ll t=1;
   
       cin >> t;
 
    while (t--) {
        solve();
 
    }
    return 0;
} 

Solution 2 (runtime: 1249 ms):
#include <bits/stdc++.h>
#define int long long int 
using namespace std;
const int mx = 2e5 + 7;

signed main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;

        // Initialize indegree array to 0
        int indegree[n+1] = {0};  // Using initialization to avoid garbage values
        vector<int> graph[n+1];

        for (int i = 0; i < n - 1; i++) {
            int u, v;
            cin >> u >> v;
            indegree[u]++;
            indegree[v]++;
            graph[u].push_back(v);
            graph[v].push_back(u);
        }

        // Handle edge case when n is 2
        if (n == 2) {
            cout << 0 << endl;
            continue;
        }

        int maxIndegree = 0;
        set<pair<int, int>> st;

        // Insert indegrees into set and track the max indegree
        for (int i = 1; i <= n; i++) {
            maxIndegree = max(maxIndegree, indegree[i]);
            st.insert({indegree[i], i});
        }

        int ans = 0;

        // Iterate through nodes with the maximum indegree
        for (int i = 1; i <= n; i++) {
            if (indegree[i] == maxIndegree) {
                // Remove the current node from the set
                st.erase({indegree[i], i});
                int tmpAns = 0;
                tmpAns += indegree[i] - 1;

                // Decrease the degree of neighbors and update the set
                for (int x : graph[i]) {
                    st.erase({indegree[x], x});
                    indegree[x]--;
                    st.insert({indegree[x], x});
                }

                // Get the max degree node from the set
                tmpAns += (*st.rbegin()).first;

                // Update the answer if necessary
                ans = max(ans, tmpAns);

                // Restore the original degree for current node
                st.insert({indegree[i], i});
                for (int x : graph[i]) {
                    st.erase({indegree[x], x});
                    indegree[x]++;
                    st.insert({indegree[x], x});
                }
            }
        }

        cout << ans << endl;
    }
}