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

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

int32_t main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        if(n == 1){
            cout << 0 << "\n";
            continue;
        }
        
        
        vector<vector<int>> adj(n+1);
        vector<int> degree(n+1, 0);vector<pair<int,int>>v1(n-1);
        for(int i = 0; i < n-1; i++){
            int u, v;
            cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
            degree[u]++;
            degree[v]++;
            v1[i].first=u,v1[i].second=v;
        }
        
        
        for (int i = 1; i <= n; i++){
            sort(adj[i].begin(), adj[i].end());
        }
        
        
        vector<pair<int,int>> vertices;
        vertices.reserve(n);
        for(int i = 1; i <= n; i++){
            vertices.push_back({degree[i], i});
        }
        sort(vertices.begin(), vertices.end(), [&](auto &a, auto &b){
            return a.first > b.first;
        });
        
        // We only need to check the top k candidates.
        int k = n;
        int ans = 0;
        
        auto isAdjacent = [&](int u, int v) -> bool {
           
            return binary_search(adj[u].begin(), adj[u].end(), v);
        };
        
       
        for (int i = 0; i < k; i++){
            for (int j = i + 1; j < k; j++){
                int u = vertices[i].second;
                int v = vertices[j].second;
                if (!isAdjacent(u, v)){
                    ans = max(ans, vertices[i].first + vertices[j].first);
                    
                    break;
                }
            }
        }
        int maxi=0;
        for(int  i=0;i<n-1;i++) maxi=max(maxi,degree[v1[i].first]+degree[v1[i].second]-1);
        cout << max(maxi-1,ans-1) << "\n";
    }
    return 0;
}


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

int32_t main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        if(n == 1){
            cout << 0 << "\n";
            continue;
        }
        
        
        vector<vector<int>> adj(n+1);
        vector<int> degree(n+1, 0);vector<pair<int,int>>v1(n-1);
        for(int i = 0; i < n-1; i++){
            int u, v;
            cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
            degree[u]++;
            degree[v]++;
            v1[i].first=u,v1[i].second=v;
        }
        
        
        for (int i = 1; i <= n; i++){
            sort(adj[i].begin(), adj[i].end());
        }
        
        
        vector<pair<int,int>> vertices;
        vertices.reserve(n);
        for(int i = 1; i <= n; i++){
            vertices.push_back({degree[i], i});
        }
        sort(vertices.begin(), vertices.end(), [&](auto &a, auto &b){
            return a.first > b.first;
        });
        

        int k = min(n, (int)5000);
        int ans = 0;
        
        auto isAdjacent = [&](int u, int v) -> bool {
            
            return binary_search(adj[u].begin(), adj[u].end(), v);
        };
        
       
        for (int i = 0; i < k; i++){
            for (int j = i + 1; j < k; j++){
                int u = vertices[i].second;
                int v = vertices[j].second;
                if (!isAdjacent(u, v)){
                    ans = max(ans, vertices[i].first + vertices[j].first);
                   
                    break;
                }
            }
        }
        int maxi=0;
        for(int  i=0;i<n-1;i++) maxi=max(maxi,degree[v1[i].first]+degree[v1[i].second]-1);
        cout << max(maxi-1,ans-1) << "\n";
    }
    return 0;
}
j < k; j++){
                int u = vertices[i].second;
                int v = vertices[j].second;
                if (!isAdjacent(u, v)){
                    ans = max(ans, vertices[i].first + vertices[j].first);
                    // For fixed i, the first valid j gives the best sum with vertex u.
                    break;
                }
            }
        }
        int maxi=0;
        for(int  i=0;i<n-1;i++) maxi=max(maxi,degree[v1[i].first]+degree[v1[i].second]-1);
        cout << max(maxi-1,ans-1) << "\n";
    }
    return 0;
}


Answer: 1