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

Solution 1 (runtime: 905 ms):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f(i,a,b) for(ll i = a; i < b; i++)
#define F(i,a,b) for(ll i = b-1; i >= a; i--)
#define cu continue
#define br break
#define bb begin
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define ff first
#define ss second
#define rr return
#define mem(name, value) memset(name, value, sizeof(name))
typedef vector <ll> vll;
typedef vector <pair<ll,ll>> vpll;
typedef set <ll> sll;
typedef multiset <ll> msll;
typedef map <ll, ll> mll;
typedef pair <ll, ll> pll;
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define unset(x, i) (x & ~(1ll << i))
#define set(x, i) (x | ((1ll << i)))
#define check(x, i) (x & (1ll << i))
#define all(v) (v).begin(), (v).end()
#define rev(v) reverse(all(v))
#define srt(v) sort(all(v))
#define srtgrt(v) sort(all(v), greater<ll>())
#define kl    '\n'
#define no cout << "NO" << '\n'
#define yes cout << "YES" << '\n'
#define inf             1e18
#define go(i, a) for (auto &i : a)
#define sz(obj) (int(obj.size()))
#define ip(n) ll n; cin>>n;
#define ipp(n,k) ll n; cin>>n; ll k; cin>>k;
#define ip3(n,m,k) ll n; cin>>n;ll m;cin>>m; ll k; cin>>k;
#define is(s) string s; cin>>s;
#define ic(s) char s; cin>>s;
#define in(n) cin>>n;
#define inn(n,m) cin>>n>>m;
#define op(x) cout<<x<<kl;
#define ol(x) cout<<x<<" ";
#define vii(vc,n) vll vc(n+5); f(i,1,n+1){cin>>vc[i];}
#define vi(vc,n) vll vc; f(i,0,n){ll x; cin>>x;vc.pb(x);}
#define vI(vc,n) f(i,0,n){ll x; cin>>x;vc.pb(x);}
#define depressed()                    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define TRC
#ifdef TRC
#define trc(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){cout << name << " : " << arg1 << endl;}
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);}
#else
#define trc(...)
#endif
//const ll mod = 1e9+7;
const ll N = 5e5 + 5;
vll v[N];
void fun()
{
    ip(n);
    f(i,1,n+1){
        v[i].clear();
    }
    mll d;
    f(i,0,n-1){
        ipp(x,y);
        v[x].pb(y);
        v[y].pb(x);
        d[x]++;
        d[y]++;
    }
    mll m;
    f(i,1,n+1){
        m[d[i]]++;
    }
    ll ct = 0;
    f(i,1,n+1){
        m[d[i]]--;
        if(m[d[i]] == 0) m.erase(d[i]);
        ll e = d[i];
        go(j,v[i]){
            m[d[j]]--;
            if(m[d[j]] == 0) m.erase(d[j]);
                         
             ll u = d[i]+d[j]-2;
             ct = max(ct,u); 
        }
        // go(j,m){
        //     trc(j.ff,j.ss);
        // }
        auto it = m.rbegin();
        if(sz(m)){
           ll k = it->ff;
           ll k2 = it->ss;
           ll w = -1;
           //trc(e,k,k2);
            w+=k;
            w+=e;
           ct = max(ct,w);
       }

           m[d[i]]++;
         go(j,v[i]){
            m[d[j]]++;
        }
    }
    op(ct);

}
int32_t main()
{// taskkill /f /im a.exe
  depressed(); ll tc1 = 1;
  cin>>tc1;
  while(tc1--){
     fun();
  }
  return 0;
}

Solution 2 (runtime: 1046 ms):
#include<bits/stdc++.h>
using namespace std;
#define  int long long
void dfs(int node,int par,vector<set<int>> &g,vector<int> &vis)
{
    vis[node]=1;
    for(auto &it:g[node])
    {
        if(vis[it]||it==par) continue;
        dfs(it,node,g,vis);
    }
}
void solve() {
  int n;
  cin>>n;
  vector<int> deg(n+1);
  vector<set<int>> g(n+1);
  for(int i=1;i<n;i++)
  {
    int a,b; cin>>a>>b;
    g[a].insert(b);
    g[b].insert(a);
    deg[a]++;
    deg[b]++;
  }
  set<pair<int,int>> st;
  for(int i=1;i<=n;i++)
    st.insert({deg[i],i});
  auto fi=(--st.end());
  int f=(*fi).second;
  int mx=deg[f];
  int ans=0;
  for(int i=1;i<=n;i++)
  {
      //remove this vertex
    if(mx==deg[i])
    {
      for(auto &it:g[i])
      {
         st.erase({deg[it],it});
         deg[it]--;
         st.insert({deg[it],it});
      }
      st.erase({mx,i});
      int comp=deg[i];
    //need to remove only one vertex remove with highest degree
    auto it=(--st.end());
      int maxdeg=(*it).first;
       comp+=maxdeg-1;
      ans=max(ans,comp);
      //undo 
       for(auto &it:g[i])
       {
         st.erase({deg[it],it});
         deg[it]++;
         st.insert({deg[it],it});
       }
       st.insert({mx,i});
     }
  }
  cout<<ans<<endl;
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int t = 1;
    cin >> t;
    while(t--) solve();
    return 0;
}