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

Solution 1 (runtime: 406 ms):
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;

struct Vertex {
    int id;
    int deg;
};

bool cmpVertex(const Vertex &a, const Vertex &b) {
    return a.deg > b.deg;
}

int main(){
    // Use std::ios_base::sync_with_stdio(false) for fast IO.
    ios_base::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        // 1-indexed vertices
        vector<int> degree(n+1, 0);
        vector< vector<int> > adj(n+1);
        // For fast checking of adjacency, use a set for each vertex.
        vector< set<int> > neighbor(n+1);
        // Save edges for candidate2
        vector< pair<int,int> > edges;
        for (int i = 1; i <= n-1; i++){
            int u, v;
            cin >> u >> v;
            degree[u]++;
            degree[v]++;
            adj[u].push_back(v);
            adj[v].push_back(u);
            neighbor[u].insert(v);
            neighbor[v].insert(u);
            edges.push_back(make_pair(u, v));
        }
        
        // Candidate for adjacent pair: iterate over edges
        int cand2 = 0;
        for (size_t i = 0; i < edges.size(); i++){
            int u = edges[i].first, v = edges[i].second;
            int cur = degree[u] + degree[v] - 2;
            if(cur > cand2) cand2 = cur;
        }
        
        // Prepare sorted vertices by degree in descending order
        vector<Vertex> sortedVertices;
        sortedVertices.reserve(n);
        for (int i = 1; i <= n; i++){
            Vertex cur;
            cur.id = i;
            cur.deg = degree[i];
            sortedVertices.push_back(cur);
        }
        sort(sortedVertices.begin(), sortedVertices.end(), cmpVertex);
        
        // Candidate for non-adjacent pair:
        // For each vertex u, try to find a vertex v in sortedVertices such that
        // v != u and v is not adjacent to u.
        int cand1 = 0; // will store maximum (degree[u] + degree[v])
        for (int u = 1; u <= n; u++){
            int candidateVdeg = -1;
            // Iterate over the sorted list.
            for (size_t j = 0; j < sortedVertices.size(); j++){
                int v = sortedVertices[j].id;
                if(v == u) continue;
                // If v is not adjacent to u, then candidate found.
                if(neighbor[u].find(v) == neighbor[u].end()){
                    candidateVdeg = sortedVertices[j].deg;
                    break;
                }
            }
            if(candidateVdeg != -1){
                int sumDeg = degree[u] + candidateVdeg;
                if(sumDeg > cand1) cand1 = sumDeg;
            }
        }
        // For non-adjacent pair answer: cand1 - 1
        int ansNonAdjacent = (cand1 == 0 ? 0 : cand1 - 1);
        
        int ans = max(ansNonAdjacent, cand2);
        cout << ans << "\n";
    }
    return 0;
}


Solution 2 (runtime: 1124 ms):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void solve(){
    int n;
    cin>>n;
    vector<vector<int>> adjList(n + 1);
    for(int ri = 1; ri < n; ri++){
    	int u, v;
    	cin>>u>>v;
    	adjList[u].push_back(v);
    	adjList[v].push_back(u);
    }
    vector<int> degree(n + 1), maxDegreeNodes;
    set<pair<int, int>> degreeToNode;
    int maxDegree = 0;
    for(int ri = 1; ri <= n; ri++){
    	degree[ri] = adjList[ri].size();
    	degreeToNode.insert({degree[ri], ri});
    	if(degree[ri] > maxDegree){
    		maxDegree = degree[ri];
    	}
    }
    for(int ri = 1; ri <= n; ri++){
    	if(degree[ri] == maxDegree)
    		maxDegreeNodes.push_back(ri);
    }
    
    int maxConnectedComponentCounts = 0;
    for(int ri = 0; ri < maxDegreeNodes.size(); ri++){
    	int currNode = maxDegreeNodes[ri];
    	int tmpConnectedComponentsCount = 1 + maxDegree - 1;
    	degreeToNode.erase({degree[currNode], currNode});
    	for(int ri = 0; ri < adjList[currNode].size(); ri++){
    		int adjNode = adjList[currNode][ri];
    		degreeToNode.erase({degree[adjNode], adjNode});
    		degreeToNode.insert({degree[adjNode] - 1, adjNode});
    	}
    	tmpConnectedComponentsCount += (*(degreeToNode.rbegin())).first - 1;
    	if(tmpConnectedComponentsCount > maxConnectedComponentCounts)
    		maxConnectedComponentCounts = tmpConnectedComponentsCount;
    	degreeToNode.insert({degree[currNode], currNode});
    	for(int ri = 0; ri < adjList[currNode].size(); ri++){
    		int adjNode = adjList[currNode][ri];
    		degreeToNode.erase({degree[adjNode] - 1, adjNode});
    		degreeToNode.insert({degree[adjNode], adjNode});
    	}
    }
    cout<<maxConnectedComponentCounts<<endl;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        solve();
    }
    return 0;
}