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

Solution 1 (runtime: 296 ms):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef unsigned long long ull;
#define V vector
#define pb push_back
#define pf push_front
#define qb pop_back
#define qf pop_front
#define eb emplace_back
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
#define fi first
#define se second
#define FR(i, a, b) for (int i = a; i <= b; i++)
#define FRR(i, a, b) for (int i = a; i >= b; i--)
#define _sort(x) sort((x).begin(),(x).end());
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;
const ll infl = 0x3f3f3f3f3f3f3f3fll;


int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        V<set<int>>adj(n);
        for(int i=0;i<n-1;i++){
            int x,y;
            cin>>x>>y;
            x--;y--;
            adj[x].insert(y);
            adj[y].insert(x);
        }
        int res=0;
        int mx=0;
        int lst=-1;
        for(int i=0;i<n;i++)mx=max(mx,(int)adj[i].size());
        int ct=5;
        for(int i=0;i<n;i++){
            if(adj[i].size()==mx&&ct>0){
                ct--;
                int curr=mx;
                int mx2=-1;
                for(int j=0;j<n;j++){
                    if(j!=i){
                        int temp=adj[j].size();
                        if(adj[j].find(i)!=adj[j].end())temp--;
                        mx2=max(mx2,temp);
                    }
                }
                if(mx2==0){
                    curr--;
                }
                else{
                    curr--;
                    curr+=mx2;
                }
                res=max(res,curr);
            }
            if(ct==0)break;
        }

        cout<<res<<endl;

    }
}

Solution 2 (runtime: 233 ms):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fios(); ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

void solution();

int main()
{
    int t = 1;
    fios();
    cin >> t;
    while(t--)
        solution();
}
void solution()
{
   int n; cin >> n;
   vector<vector<int>>element(n+1);
   vector<int>cnt(n+1,0);
   for(int i = 0; i < n-1; i++)
   {
       int a,b; cin >> a >> b;
       cnt[a]++;
       cnt[b]++;
       element[a].push_back(b);
       element[b].push_back(a);
   }
   int maxi = 0,num;
   for(int i = 1; i <= n; i++)
   {
       maxi = max(maxi,cnt[i]);
   }
   vector<int>maxnode;
   for(int i = 1; i <= n; i++)
   {
       if(cnt[i] == maxi)
        maxnode.push_back(i);
       if(maxnode.size() >= 2)
        break;
   }
   int fans = 0;
   for(int i = 0; i < min(2,(int)maxnode.size()); i++)
   {
       int ans = cnt[maxnode[i]];
       cnt[maxnode[i]] = -1;
       for(int j = 0; j < element[maxnode[i]].size(); j++)
          cnt[element[maxnode[i]][j]]--;
       int smax = 0;
       for(int j = 1; j <= n; j++)
          smax = max(cnt[j],smax);
       fans = max(fans, ans + smax -1);
       cnt[maxnode[i]] = maxi;
       for(int j = 0; j < element[maxnode[i]].size(); j++)
          cnt[element[maxnode[i]][j]]++;
   }
   cout << fans << '\n';
}