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

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


#define ll long long int
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define F first
#define S second
#define sz(x) ((int)(x).size())
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

const int MOD = 1e9 + 7;
const ll INF = 1e18;


template<typename T>
void debug(vector<T> v) { for(auto i: v) cout << i << " "; cout << endl; }

template<typename T1, typename T2>
void debug(vector<pair<T1, T2>> v) { for(auto i: v) cout << "(" << i.F << "," << i.S << ") "; cout << endl; }


int main() {
    fast_io;

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<vector<int>>neighbours(n,vector<int>());
        vector<int>deg(n,0);
        for(int i=0;i<n-1;i++){
            int a,b;
            cin>>a>>b;
            neighbours[a-1].push_back(b-1);
            neighbours[b-1].push_back(a-1);
            deg[a-1]++;
            deg[b-1]++;
        }
        int maxi = 0;
        int conn = 1;
        vector<int>minis(n,INT_MAX);
        priority_queue< pair<int,int> ,vector<pair<int,int>> , greater<pair<int,int>> > pq;
        for(int i=0;i<n;i++){
            if(deg[i] == 1){
                minis[i] = 0;
               pq.push({0,i});
            }
        }

        while(!pq.empty()){
            pair<int,int> p  =pq.top();
            pq.pop();
            int node = p.second;
            int dist  =p.first;
            for(auto e : neighbours[node]){
                if(minis[e] > minis[node] + 1){
                    minis[e] = minis[node] + 1;
                    pq.push({minis[e] , e});
                }
            }

        }

        for(int i =1;i<n;i++){
            if(deg[maxi]  < deg[i] ){
                maxi = i;
            }
        }

        int optimal = maxi;
        for(int i = 0;i<n;i++){
            if(deg[i] == deg[maxi]){
                if(minis[i] < minis[maxi])optimal = i;
            }
        }
        // for(int i=0;i<n;i++){
        //     cout<<minis[i]<<" ";
        // }
        // cout<<endl;
        maxi = optimal;
        for(auto e:neighbours[maxi]){
            deg[e]--;
        }
        conn = conn -1 + deg[maxi];
        // cout<<"hello"<<maxi<<" " <<conn<<endl;
        deg[maxi] = INT_MIN;

        
        maxi = 0;
        for(int i =1;i<n;i++){
            if(deg[maxi]  < deg[i] ){
                maxi = i;
            }
        }
        conn = conn -1 + deg[maxi];
        // cout<<"hello"<<maxi<<" " <<conn<<endl;

        cout<<conn<<endl;


        
    }

    return 0;
}

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


#define ll long long int
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define F first
#define S second
#define sz(x) ((int)(x).size())
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

const int MOD = 1e9 + 7;
const ll INF = 1e18;


template<typename T>
void debug(vector<T> v) { for(auto i: v) cout << i << " "; cout << endl; }

template<typename T1, typename T2>
void debug(vector<pair<T1, T2>> v) { for(auto i: v) cout << "(" << i.F << "," << i.S << ") "; cout << endl; }


int main() {
    fast_io;

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<vector<int>>neighbours(n,vector<int>());
        vector<int>deg(n,0);
        for(int i=0;i<n-1;i++){
            int a,b;
            cin>>a>>b;
            neighbours[a-1].push_back(b-1);
            neighbours[b-1].push_back(a-1);
            deg[a-1]++;
            deg[b-1]++;
        }
        if(n <= 2){
            cout<<0<<endl;
            continue;
        }
        int conn = 1;
        set<pair<int,int>>s;
        for(int i=0;i<n;i++){
            s.insert({deg[i],i});
        }
        for(int i=0;i<n;i++){
            int tconn = 1;
            pair<int,int>p = {deg[i] , i};
            s.erase(p);
            for(auto e : neighbours[i]){
                s.erase({deg[e],e});
                s.insert({deg[e]-1,e});
            }
            int deg_max = s.rbegin()->first;
       
            conn = max(conn , -1 + deg[i] + deg_max);
            s.insert(p);
            for(auto e : neighbours[i]){
                s.erase({deg[e]-1,e});
                s.insert({deg[e],e});
            }

        }

        cout<<conn<<endl;
        
    }

    return 0;
}

Answer: 2