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

Solution 1 (runtime: 234 ms):
#pragma GCC Optimize ("Ofast")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define N '\n'
#define pb push_back
#define pob pop_back
#define ff first
#define ss second
#define bb begin()
#define ee end()
#define fastIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).


int main()
{
    fastIO();
    ll t = 1,it;
    cin >> t;
    for(it = 1;it <= t;it++)
    {
        ll n,m,q,i,j,k,s = 0,x = 0,y = 0,c = 0,d = 0,f = 0,a,b;
        cin >> n;
        vector<ll> v[n+5];
        ll deg[n+5] = {};
        ll cnt[n+5] = {};
        for(i = 0;i < n-1;i++)
        {
            cin >> c >> d;
            v[c].pb(d);
            v[d].pb(c);
            deg[c]++;
            deg[d]++;
        }
        for(i = 1;i <= n;i++)
        {
            for(j = 0;j < v[i].size();j++)
            {
                cnt[i] += deg[v[i][j]];
            }
        }
        ll maxi = 0,maxidx, maxvx = LLONG_MAX;
        for(i = 1;i <= n;i++)
        {
            c = deg[i];
            if(c > maxi)
            {
                maxi = c;
                maxidx = i;
                maxvx = cnt[i];
            }
            else if((c == maxi) && (cnt[i] < maxvx))
            {
                maxidx = i;
                maxvx = cnt[i];
            }
        }
        for(i = 0;i < v[maxidx].size();i++)
        {
            deg[v[maxidx][i]]--;
        }
        s += maxi;
        maxi = 0;
        for(i = 1; i <= n;i++)
        {
            if(i != maxidx)
            {
                maxi = max(maxi,deg[i]);
            }
        }
        s += maxi-1;
        cout << s << N;
    }
    return 0;
}






Solution 2 (runtime: 218 ms):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
 
#define ll long long
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
#define ii pair<int, int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define mems(x, v) memset(x, v, sizeof(x))
#define sz(v) ((int)(v).size())
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
const double PI = acos(-1.0);
const int INF = 1e9;
const ll MOD = 1e9+7;
 
ll power(ll x, ll y, ll p = MOD){
   ll res=1;
   x = x%p;
   while(y>0){
     if(y&1){
       res = (res*x)%p;
     }
     y = y>>1;
     x = (x*x)%p;
   }
   return res;
}
 
ll modInverse(ll n, ll p = MOD){
   return power(n,p-2,p);
}
 
void solve() {
   int n;
   cin>>n;
   vi adj[n+1];
   int a,b;
   rep(i,0,n-1){
      cin>>a>>b;
      adj[a].push_back(b);
      adj[b].push_back(a);
   }
   vector<pair<int,int>>v;
   rep(i,1,n+1){
      sort(adj[i].begin(),adj[i].end());
      v.push_back({adj[i].size(),i});
   }
   sort(v.rbegin(),v.rend());
   int x=1;int y=1;
   while(x<n && v[x].first==v[x-1].first)x++;
   y=x+1;
   while(y<n && v[y].first==v[y-1].first)y++;
   int maxi=0;
   rep(i,0,x-1){
      rep(j,i+1,x){
         int idx = lower_bound(adj[v[i].second].begin(),adj[v[i].second].end(),v[j].second)-adj[v[i].second].begin();
         if(idx<adj[v[i].second].size() && adj[v[i].second][idx]==v[j].second){
           maxi = max(maxi,2*v[i].first-2);
         }
         else{
             cout<<2*v[i].first-1<<endl;
             return;
         }
      }
   }
   if(x>1){
      cout<<maxi<<endl;
      return;
   }
   rep(j,x,y){
      int idx = lower_bound(adj[v[0].second].begin(),adj[v[0].second].end(),v[j].second)-adj[v[0].second].begin();
      if(idx<adj[v[0].second].size() && adj[v[0].second][idx]==v[j].second){
        maxi = max(maxi,v[0].first+v[j].first-2);
         
      }
      else{
          maxi = max(maxi,v[0].first+v[j].first-1);
         cout<<maxi<<endl;
         return;
      }
   }
   cout<<maxi<<endl;
}
 
int main() {
   ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
   int t;
   cin >> t;
   while(t--) {
      solve();
   }
   return 0;
}