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

Code 1:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define mod 1000000007

vector<bool> is_prime;

void sieve(ll n) {
    is_prime.assign(n + 1, true);
    is_prime[0] = is_prime[1] = false;
    for (ll i = 2; i * i <= n; i++) {
        if (is_prime[i]) {
            for (ll j = i * i; j <= n; j += i)
                is_prime[j] = false;
        }
    }
}

ll lcm(ll a, ll b) {
    return (a / __gcd(a, b)) * b;
}

/*
1. Read question carefully
2. generalize the logic
3. convert it to mathematical equation
4. !!!!! don't think  (CHECK) again until you get wrong answer
5. write the code
*/

void solve() {
     ll n;
    cin>>n; // number of nodes;
    vector<pair<ll,ll>>edges(n-1); 
    map<pair<ll,ll>,ll>mp;
    for(ll i=0;i<n-1;i++){
        cin>>edges[i].first>>edges[i].second;
        if(edges[i].first>edges[i].second){
            swap(edges[i].first,edges[i].second);
        }
        mp[{edges[i].first,edges[i].second}]=1;
    }

    // prepraing adjacency list 
    vector<ll>degree(n,0);
    vector<vector<ll>>list(n,vector<ll>());
    for(ll i=0;i<n-1;i++){
        ll a=edges[i].first;
        ll b=edges[i].second;
        a--;
        b--;

        list[a].push_back(b);
        list[b].push_back(a);
        degree[a]++;
        degree[b]++;

    }


    vector<pair<ll,ll>>ans;

    // degree,node

    for(ll i=0;i<n;i++){
        ans.push_back({degree[i],i});
    }
    sort(ans.rbegin(),ans.rend());
    
    ll s1=ans[0].first; // max degree
     ll s2=ans[1].first;// low 

    ll max1=ans[0].second;

    vector<ll>help;

    for(ll i=1;i<ans.size();i++){
        if(ans[i].first==s2){
            help.push_back(ans[i].second);
        }
    }
    // for(ll i=0;i<ans.size();i++){
        
    //        cout<<ans[i].first<<" "<<ans[i].second<<endl;
        
    // }
    

    // cout<<endl;
    // cout<<endl;

    
    vector<ll>unconet;
    vector<ll>con;

    for(ll i=0;i<help.size();i++){
        if(mp.find({max1+1,help[i]+1})!=mp.end() || mp.find({help[i]+1,max1+1})!=mp.end() ){
            con.push_back(help[i]);
        }
        else{
            unconet.push_back(help[i]);
            
        }
    }
    

    // for(auto it:unconet){
    //     cout<<it<<" ";
    // }
    // cout<<"||||";

    // cout<<'u'<<endl;

    // for(auto it:con){
    //     cout<<it<<" ";
    // }
    // cout<<endl;
    // cout<<1<<endl;;
    ll maxi=0;
    if(unconet.size()==0){

        ll a=con[0];
        ll dega=list[a].size();
       
         for(auto &it:con){
            maxi=max(maxi,degree[it]+s1-2);
        }
        cout<<maxi<<endl;

    }
    else{

        for(auto &it:unconet){
            maxi=max(maxi,degree[it]+s1-1);
        }
        // cout<<a<<" ";
        // ll dega=list[a].size();
        // cout<<s1+dega-1<<endl;
    cout<<maxi<<endl;
    }


  


  
}

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

    ll test = 1;
    cin >> test;
    while (test--) {
        solve();
    }
    return 0;
}

Code 2:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define mod 1000000007

vector<bool> is_prime;

void sieve(ll n) {
    is_prime.assign(n + 1, true);
    is_prime[0] = is_prime[1] = false;
    for (ll i = 2; i * i <= n; i++) {
        if (is_prime[i]) {
            for (ll j = i * i; j <= n; j += i)
                is_prime[j] = false;
        }
    }
}

ll lcm(ll a, ll b) {
    return (a / __gcd(a, b)) * b;
}

/*
1. Read question carefully
2. generalize the logic
3. convert it to mathematical equation
4. !!!!! don't think  (CHECK) again until you get wrong answer
5. write the code
*/

void solve() {
     ll n;
    cin>>n; // number of nodes;
    vector<pair<ll,ll>>edges(n-1); 
    map<pair<ll,ll>,ll>mp;
    for(ll i=0;i<n-1;i++){
        cin>>edges[i].first>>edges[i].second;
        if(edges[i].first>edges[i].second){
            swap(edges[i].first,edges[i].second);
        }
        mp[{edges[i].first,edges[i].second}]=1;
    }

    // prepraing adjacency list 
    vector<ll>degree(n,0);
    vector<vector<ll>>list(n,vector<ll>());
    for(ll i=0;i<n-1;i++){
        ll a=edges[i].first;
        ll b=edges[i].second;
        a--;
        b--;

        list[a].push_back(b);
        list[b].push_back(a);
        degree[a]++;
        degree[b]++;

    }


    vector<pair<ll,ll>>ans;

    // degree,node

    for(ll i=0;i<n;i++){
        ans.push_back({degree[i],i});
    }
    sort(ans.rbegin(),ans.rend());
    
    ll s1=ans[0].first; // max degree
     ll s2=ans[1].first;// low 



    if(s1==s2){
    vector<ll>help;

    for(ll i=0;i<ans.size();i++){
        if(ans[i].first==s2){
            help.push_back(ans[i].second);
        }
    }
    ll maxi=-1e9;
    if(help.size()>=3){
        maxi=max(maxi,2*degree[help[0]]-1);
    }
    else{
        if(mp.find({help[0]+1,help[1]+1})!=mp.end() || mp.find({help[1]+1,help[0]+1})!=mp.end()){
            maxi=max(maxi,2*degree[help[0]]-2);
        }
        else{
            maxi=max(maxi,2*degree[help[0]]-1);

        }
    }
    cout<<maxi<<endl;

        
    }
    else{
    vector<ll>help;
    ll max1=ans[0].second;
    

    for(ll i=1;i<ans.size();i++){
        if(ans[i].first==s2){
            help.push_back(ans[i].second);
        }
    }

    ll maxi=-1;

    for(ll i=0;i<help.size();i++){
        if(mp.find({max1+1,help[i]+1})!=mp.end() || mp.find({help[i]+1,max1+1})!=mp.end() ){
           maxi=max(maxi,degree[max1]+degree[help[i]]-2);
        }
        else{
           maxi=max(maxi,degree[max1]+degree[help[i]]-1);
            
        }
    }
    cout<<maxi<<endl;

    }
  

   
   


  


  
}

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

    ll test = 1;
    cin >> test;
    while (test--) {
        solve();
    }
    return 0;
}

Answer: 2