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

Solution 1 (runtime: 218 ms):
#include<bits/stdc++.h>
#include<unordered_map>	
using namespace std;
#define ll long long int
#define vi vector<ll> 
#define all(v) v.begin() , v.end()
#define srt(v) sort(v.begin(), v.end())
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define co(c) cout<<c<<"\n"
#define rn return 
#define acc(v) accumulate(v.begin() , v.end() , 0)


template<class T, class U> istream& operator>>(istream &is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T> istream& operator>>(istream& is, vector<T>& vec) { for(auto &x : vec) cin >> x; return is; }
template<class T, class U> ostream& operator<<(ostream &os, const pair<T, U>& p) { os << "<" << p.first << "," << p.second << ">"; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& vec) { for(auto x : vec) os << x << " "; return os; }

void solve(){
  int n;
  cin >> n;

  vi adj[n+1];
  vi out(n+1 , 0);

  for(int i= 0 ; i < n -1 ; i++){
     int a, b;
     cin >> a >> b;
     
     adj[a].push_back(b);
     adj[b].push_back(a);

     out[a]++;
     out[b]++;
  }
  int ans = 1;
  int ind = max_element(out.begin(), out.end()) - out.begin();
  
  int d = out[ind];
  out[ind] = -1;
  
  ans += (d -1);
  //set<int> connected;
  int cnt = 0;
  for(auto t: adj[ind]){
      if( d == out[t]){
           cnt++;
      }
      out[t]--;  
      
  }
  if(cnt >= 2){
      co((2*d - 1));
      rn;
  } 
  
  
  

  ind = max_element(out.begin(), out.end() ) - out.begin();
  if(out[ind] == d)
  {
       co((2*d - 1)) ;
         rn;
  }
  d = out[ind];
  
  ans += (d - 1);

  co(ans);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int testcases;
    cin >> testcases;

    //testcases = 1;
    while (testcases--) {
       solve();
        //return 0;

    }
  return 0;
}



Solution 2 (runtime: 359 ms):
#include <bits/stdc++.h>
#define int long long
using namespace std;
set <int> adj[200005];
map <int,int> freq;
vector <pair<int,int>> assist;
int solve()
{
    sort(assist.begin(),assist.begin()+assist.size());
    int res=0;
    if (freq[assist.at(assist.size()-1).first]>2)
        return assist.at(assist.size()-1).first*2-1;
    if (freq[assist.at(assist.size()-1).first]==2)
    {
        if (adj[assist.at(assist.size()-1).second].count(assist.at(assist.size()-2).second))
            return assist.at(assist.size()-1).first*2-2;
        return assist.at(assist.size()-1).first*2-1;
    }
    int ss=assist.size()-2;
    while (ss>=0 && assist.at(ss).first==assist.at(assist.size()-2).first)
    {
        if (adj[assist.at(assist.size()-1).second].count(assist.at(ss).second)==0)
            return assist.at(assist.size()-1).first+assist.at(assist.size()-2).first-1;
        ss--;
    }
    return assist.at(assist.size()-1).first+assist.at(assist.size()-2).first-2;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t,n,u,v,x;
    cin >> t;
    for (int weoweo=0 ; weoweo<t ; weoweo++)
    {
        assist.clear();
        freq.clear();
        cin >> n;
        for (int i=1 ; i<n ; i++)
        {
            cin >> u >> v;
            adj[u].insert(v);
            adj[v].insert(u);
        }
        for (int i=1 ; i<=n ; i++)
        {
            x=adj[i].size();
            freq[x]++;
            assist.push_back({x,i});
        }
        cout << solve() << "\n";
        for (int i=1 ; i<=n ; i++)
            adj[i].clear();
    }
    return 0;
}