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

Solution 1 (runtime: 358 ms):
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string.h>
#define ll long long
#define pii pair<int,int>
using namespace std;

const int LM=200100;
int n;
vector<int>G[LM];
set<int> S[LM];
vector<int>v[LM];

int C(int x, int y){
    return S[x].find(y)!=S[x].end();
}

void solve(){
    scanf("%d",&n);
    for(int i=1; i<=n; i++)G[i].clear(),v[i].clear(),S[i].clear();
    for(int i=1; i<n; i++){
        int x,y;
        scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
        S[x].insert(y);
        S[y].insert(x);
    }
    int si[LM], Ms=0;
    
    for(int i=1; i<=n; i++){
        si[i]=G[i].size();
        Ms=max(Ms,si[i]);
        v[si[i]].push_back(i);
    }
    
    int MsSi=v[Ms].size();
    if(MsSi>=3)
        printf("%d\n",Ms*2-1);
    else if(MsSi==2){
        printf("%d\n",Ms*2-C(v[Ms][0],v[Ms][1])-1);
    }
    else{
        int ws=Ms-1;
        while(v[ws].empty())ws--;
        for(int i:v[ws]){
            if(C(i,v[Ms][0])==0){
                printf("%d\n",Ms+ws-1);
                return;
            }
        }
        printf("%d\n",Ms+ws-2);
    }
}

int main(){
    int tc;
    scanf("%d", &tc);
    for(;tc>0;tc--)solve();
}


Solution 2 (runtime: 343 ms):
// b569d7e0-614a-4614-b265-4276fa7e8fc3
#include <bits/stdc++.h>
using namespace std;
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int t; 
    cin >> t;
    while(t--){
        int n; 
        cin >> n;
        vector<vector<int>> graph(n+1);
        vector<int> deg(n+1, 0);
        // Read tree
        for (int i = 1; i <= n - 1; i++){
            int u, v;
            cin >> u >> v;
            graph[u].push_back(v);
            graph[v].push_back(u);
            deg[u]++;
            deg[v]++;
        }
 
        // Build a global list of vertices sorted by descending degrees.
        vector<pair<int,int>> vertices;
        vertices.reserve(n);
        for (int i = 1; i <= n; i++){
            vertices.push_back({deg[i], i});
        }
        sort(vertices.begin(), vertices.end(), [](const pair<int,int>& a, const pair<int,int>& b){
            return a.first > b.first;
        });
 
        // Candidate from adjacent pairs: For every edge, consider u-v and compute deg(u)+deg(v)-2.  
        int bestAdj = -1;
        for (int u = 1; u <= n; u++){
            for (int v : graph[u]){
                if(v > u){ // process each edge once
                    bestAdj = max(bestAdj, deg[u] + deg[v] - 2);
                }
            }
        }
 
        // Candidate from non-adjacent pair
        // For each vertex, try to find a vertex in the global sorted list that is (i) not itself and (ii) not adjacent.
        int bestNonAdjSum = -1;
        // Build a neighbor set for each vertex for quick check
        vector<unordered_set<int>> neighborSet(n+1);
        for (int u = 1; u <= n; u++){
            for (int v : graph[u]){
                neighborSet[u].insert(v);
            }
        }
 
        // For each vertex u, iterate over the global sorted list; once we find a vertex v
        // that is not u and not in neighborSet[u], update bestNonAdjSum with deg(u)+deg(v)
        for (int u = 1; u <= n; u++){
            for(auto &pr : vertices){
                int candidateDeg = pr.first;
                int v = pr.second;
                if(v == u) continue;
                if(neighborSet[u].find(v) != neighborSet[u].end()){
                    continue;
                }
                int sumDeg = deg[u] + candidateDeg;
                bestNonAdjSum = max(bestNonAdjSum, sumDeg);
                break; // Found the best candidate for u; break out.
            }
        }
 
        int candidateNonAdj = -1;
        if(bestNonAdjSum != -1)
            candidateNonAdj = bestNonAdjSum - 1;
 
        int ans = max(bestAdj, candidateNonAdj);
        if(ans < 0) ans = 0;  // In case the graph becomes empty
        cout << ans << "\n";
    }
    return 0;
}