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

Code 1:
 #pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const long double pi = 3.141592653589793238462643383279;
//#define max(a,b) (a>b?a:b)
//#define min(a,b) (a<b?a:b)
#define forb(i,a,b) for(ll i=a;i<b;i++)
#define fo(i,a) for(ll i=0;i<a;i++)
#define foa(i,a) for(auto i=a.begin();i!=a.end();i++)
#define rep(i,a) for(ll i=a;i>-1;i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vector<int>>
#define vvll vector<vector<ll> >
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define pb push_back
const ll mod= 1e9+7;
 
void _print(ll t) {cerr<< t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
//void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
//void _print(ull t) {cerr << t;}
void printll(vector<ll> a){
    ll n=a.size();
    fo(i,n){
    cout<<a[i]<<" ";
        }
    cout<<'\n';
}
 
void printint(vector<int> a){
    ll n=a.size();
    fo(i,n) cout<<a[i]<<" ";
    cout<<'\n';
}
 
 ll gcd(ll a, ll b){
    //b = a*q1 + r1;
    //a = r1*q2 + r2;
    //do until r2=0
    if(a==0)return b;
    if(b==0)return a;
    if(a>b)swap(a,b);
    ll rem=b%a;
    while(rem){
        b=a;
        a=rem;
        rem=b%a;
    }
    return a;
}
 
ll lcm(ll a, ll b){
    return (a*b)/gcd(a,b);
}
ll power(ll a,ll b){
    ll res=1;
    while(b){
        if(b&1)res=res*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return res;
}
 ll factorial(ll n)
    {
        ll res = 1, i;
  
        for (i = 2; i <= n; i++)
            res = (res*1ll* i)%mod;
        return res;
    }


void solve() {  
   ll n;
   cin>>n;
   vector<vll>adj(n+1);
   vll in(n+1,0);
 fo(i,n-1){
    ll a,b;
    cin>>a>>b;
    in[a]++;
    in[b]++;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  if(n==2){
    cout<<0<<endl;
    return;
  }
  ll mx=0;
  set<pair<ll,ll>>st;
  forb(i,1,n+1)
    {
    mx=max(mx,in[i]);
    st.insert({in[i],i});
    }
 
ll  ans=0;
  forb(i,1,n+1){
    if(in[i]==mx){
       st.erase({in[i],i});
     ll tmp=0;
       tmp+=in[i]-1;
        
        for(auto x:adj[i]){
            st.erase({in[x],x});
            in[x]--;  
            st.insert({in[x],x});
        }
        tmp+=(*st.rbegin()).first;
       ans=max(ans,tmp);
 
       st.insert({in[i],i});
       for(auto x:adj[i]){
        st.erase({in[x],x});
        in[x]++;
        st.insert({in[x],x});
       }
    }
  }
  cout<<ans<<endl;
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t=1;
cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Code 2:
 #pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const long double pi = 3.141592653589793238462643383279;
//#define max(a,b) (a>b?a:b)
//#define min(a,b) (a<b?a:b)
#define forb(i,a,b) for(ll i=a;i<b;i++)
#define fo(i,a) for(ll i=0;i<a;i++)
#define foa(i,a) for(auto i=a.begin();i!=a.end();i++)
#define rep(i,a) for(ll i=a;i>-1;i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vector<int>>
#define vvll vector<vector<ll> >
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define pb push_back
const ll mod= 1e9+7;
 
void _print(ll t) {cerr<< t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
//void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
//void _print(ull t) {cerr << t;}
void printll(vector<ll> a){
    ll n=a.size();
    fo(i,n){
    cout<<a[i]<<" ";
        }
    cout<<'\n';
}
 
void printint(vector<int> a){
    ll n=a.size();
    fo(i,n) cout<<a[i]<<" ";
    cout<<'\n';
}
 
 ll gcd(ll a, ll b){
    //b = a*q1 + r1;
    //a = r1*q2 + r2;
    //do until r2=0
    if(a==0)return b;
    if(b==0)return a;
    if(a>b)swap(a,b);
    ll rem=b%a;
    while(rem){
        b=a;
        a=rem;
        rem=b%a;
    }
    return a;
}
 
ll lcm(ll a, ll b){
    return (a*b)/gcd(a,b);
}
ll power(ll a,ll b){
    ll res=1;
    while(b){
        if(b&1)res=res*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return res;
}
 ll factorial(ll n)
    {
        ll res = 1, i;
  
        for (i = 2; i <= n; i++)
            res = (res*1ll* i)%mod;
        return res;
    }


void solve() {  
   ll n;
   cin>>n;
   vector<vll>adj(n);
   vll indeg(n,0);
   fo(i,n-1){
       ll u,v;
       cin>>u>>v;
       u--;v--;
       adj[u].pb(v);
       adj[v].pb(u);
       indeg[u]++;
       indeg[v]++;
   } 
    ll ans=0;
   ll maxi=0,ind=0;
   fo(i,n){
       maxi=max(maxi,indeg[i]);
       if(maxi==indeg[i]){
           ind=i;
       }
   }
   indeg[ind]=0;
   ans+=maxi;
    for(auto it:adj[ind]){
        indeg[it]--;
    }
    sort(indeg.begin(),indeg.end());
    ans+=indeg.back();
    cout<<ans-1<<endl;
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t=1;
cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Answer: 1