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

Code 1:
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;

#define inf 1e9
#define mod 1000000007
#define MAX 1000000000
#define vpii vector<pair<int,int>>
#define int long long
#define pb push_back
#define read(arr) for (int i = 0; i < arr.size(); i++) cin >> arr[i]
#define print(arr) for (int i = 0; i < arr.size(); i++) cout << arr[i] << " "; cout << "\n"
#define vi vector<int>
#define vvi vector<vi>
#define all(v) v.begin(), v.end()


void solve(int t) {
   int n; cin >> n;
   int ans = 1;
   vector<set<int>> adj(n+1);
   vector<pair<int,int>> v(n+1, {0,0} );
   for(int i = 0 ; i  <  n +1; ++i) {
        v[i].second = i;
   }
   vi last(n+1,0);
   for(int i = 0; i < n-1; ++i) {
    int a, b; cin >> a >> b;
        adj[a].insert(b);
        adj[b].insert(a);
        v[a].first++;
        v[b].first++;
        last[a]++;
        last[b]++;
   }
    sort(v.begin(),v.end());

    int maxi = INT_MIN;
    for(int i= 1; i < n+1; ++i) {
        int p1 = i;
        int two = 0;
        for(int j  = n; j>=0 ;j--) {
            if(i!= v[j].second) {
            two = max(v[j].first, two);
            set<int>&s = adj[i];
            while(j>= 0 && v[j].second != i && s.find(v[j].second)!=s.end()) {
                --j;
            }
            
            if(j >= 0 && v[j].second != i) {
                int one = v[j].first;
                maxi = max(maxi , one + last[i] - 1);
            }
            maxi = max(two +last[i] -2 ,maxi);
            break;
            }
        }
    }
    cout << maxi << "\n";

}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int test = 1;
    cin >> test;
    

    for (int i = 1; i <= test; ++i) {
        solve(i);
    }
}

Code 2:
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;

#define inf 1e9
#define mod 1000000007
#define MAX 1000000000
#define vpii vector<pair<int,int>>
#define int long long
#define pb push_back
#define read(arr) for (int i = 0; i < arr.size(); i++) cin >> arr[i]
#define print(arr) for (int i = 0; i < arr.size(); i++) cout << arr[i] << " "; cout << "\n"
#define vi vector<int>
#define vvi vector<vi>
#define all(v) v.begin(), v.end()


void solve(int t) {
   int n; cin >> n;
   int ans = 1;
   vector<vector<int>> adj(n+1);
   vector<int> v(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);
        v[a]++;
        v[b]++;
   }
    int maxi = INT_MIN;
    int ind;
   for(int i = 1 ; i  < n+1; ++i) {
        if(maxi < v[i]) {
            maxi = v[i];
            ind = i;

        }
   }
    ans += maxi - 1;
    vector<vector<int>> mat(n);
    vector<int> u(n,0);
    for(int i = 1 ; i  < adj.size(); ++i) {
        if(i!=ind) {
        for(int j = 0 ; j < adj[i].size(); ++j) {
            if(adj[i][j] != ind) {
                int nind = i - (i>ind);
                int nind2 = adj[i][j] - (adj[i][j] > ind);
                mat[nind].push_back(nind2);
                u[nind]++;
            }
         }
        }
    }

    // for(int i = 1; i < mat.size(); ++i) {
    //     cout << i << "-> ";
    //     for(auto j :mat[i] ) {
    //         cout << j << " ";
    //     }
    //     cout << "\n";
    // }

    // for(int i = 1; i  < u.size(); ++i) {
    //     cout << u[i] << " ";
    // }
    // cout << "\n";
    maxi =INT_MIN;
    for(int i = 1; i  < u.size(); ++i ) {
        maxi = max(maxi, u[i]);
    }

    ans += maxi - 1;

    cout << ans << "\n";

}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int test = 1;
    cin >> test;
    

    for (int i = 1; i <= test; ++i) {
        solve(i);
    }
}

Answer: 1