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

Solution 1 (runtime: 218 ms):
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
 
    int t;
    cin >> t;
 
    while(t--){
        int n;
        cin >> n;
 
 
        vector<vector<int>> adj(n+1, vector<int>());
        vector<int> deg(n+1, 0);
 
 
        for(int i = 0; i < n-1; ++i){
            int u, v;
            cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
            deg[u]++;
            deg[v]++;
        }
 
 
        vector<int> nodes(n);
        for(int i = 0; i < n; ++i){
            nodes[i] = i + 1;
        }
 
 
        sort(nodes.begin(), nodes.end(), [&](const int a, const int b) -> bool{
            if(deg[a] != deg[b]) return deg[a] > deg[b];
            return a < b;
        });
 
 
        for(int v = 1; v <= n; ++v){
            sort(adj[v].begin(), adj[v].end());
        }
 
        long long max_val = 0;
 
 
        for(int i = 0; i < n; ++i){
            int u = nodes[i];
            for(int j = i + 1; j < n; ++j){
                int v = nodes[j];
 
                if(!binary_search(adj[u].begin(), adj[u].end(), v)){
                    long long temp = static_cast<long long>(deg[u]) + deg[v] - 1;
                    max_val = max(max_val, temp);
                    break; 
                }
            }
        }
 
        long long maxA = 0;
 
 
        for(int u = 1; u <= n; ++u){
            for(auto &v : adj[u]){
                if(u < v){ 
                    long long temp = static_cast<long long>(deg[u]) + deg[v] - 2;
                    maxA = max(maxA, temp);
                }
            }
        }
 
 
        cout << max(max_val, maxA) << "\n";
    }
 
    return 0;
}



     
    
    
   

Solution 2 (runtime: 702 ms):
#include<bits/stdc++.h>
#define int long long 
#define endl '\n'
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
using namespace std;

vector<vector<int>> edges;

signed main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        edges.clear();

        vector<set<int>> adj(n);
        int a, b;
        for(int i=0; i<n-1; i++){
            cin>>a>>b;
            adj[a-1].insert(b-1);
            adj[b-1].insert(a-1);
            edges.push_back({a-1, b-1});
        }

        vector<int> degree(n, 0);
        // dfs(0, -1, adj, degree);
        for(int i=0; i<n; i++){
            degree[i] = adj[i].size();
        }

        int ans = 0;
        for(auto &it: edges){
            ans = max(ans, degree[it[0]] + degree[it[1]] - 2);
        }

        vector<pair<int,int>> v;

        for(int i=0; i<n; i++){
            v.push_back({degree[i], i});
        }

        sort(begin(v), end(v), [&](pair<int,int>&p1, pair<int,int>&p2){
            return p1.first > p2.first;
        });
        
        for(int i=0; i<n; i++){

            for(int j=0; j<n; j++){

                int node = v[j].second;
                int deg = v[j].first;

                if(node != i && adj[i].find(node) == adj[i].end()){
                    // cout<<"i: "<<i<<"   ";
                    // cout<<deg<<endl;
                    // cout<<"node: "<<node<<"   "<<"deg: "<<deg - 1 + degree[i]<<endl;
                    ans = max(ans, deg - 1 + degree[i]);
                    break;
                }
            }
        }

        cout<<ans<<endl;

    }
    return 0;
}