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

Code 1:
#include <iostream>
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>

using namespace std;

#define endl "\n"
#define ll long long
#define dl double long
#define pb push_back
const int mod = 998244353;

void solve() {
    
    int n;
    cin >> n;

    unordered_set<int> adj[n+1];
    vector<pair<int,int>> edges;

    for(int i=1; i<n; i++) {
        int u, v;
        cin >> u >> v;
        edges.pb({u,v});

        adj[u].insert(v);
        adj[v].insert(u);
    }

    int ans = 0;

    for(auto& it: edges) {
        int eu = adj[it.first].size();
        int ev = adj[it.second].size();

        ans = max(ans, eu+ev-2);
    }

    vector<pair<int,int>> numOfAdjEdges(n+1);
    numOfAdjEdges[0] = {0,0};

    for(int i=1; i<=n; i++) {
        numOfAdjEdges[i] = {-adj[i].size(), i};
    }

    sort(numOfAdjEdges.begin(), numOfAdjEdges.end());

    
    

    for(int j=0; j<n; j++) {
        int v1 = numOfAdjEdges[j].second;
        int e1 = -numOfAdjEdges[j].first;
    
        for(int i=j+1; i<n; i++) {
            int v2 = numOfAdjEdges[i].second;
            if(adj[v1].find(v2) != adj[v1].end()) continue; 

            int e2 = -numOfAdjEdges[i].first;
            ans = max(ans, e1+e2-1);
            break;
        }
    }

    cout << ans << endl;
    
}

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

    while(testCases--) solve();
    return 0;
}

Code 2:
#include <iostream>
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>

using namespace std;

#define endl "\n"
#define ll long long
#define dl double long
#define pb push_back
const int mod = 998244353;

int getVertex(int& n, unordered_set<int> adj[]) {

}

void solve() {
    
    int n;
    cin >> n;

    unordered_set<int> adj[n+1];

    for(int i=1; i<n; i++) {
        int u, v;
        cin >> u >> v;

        adj[u].insert(v);
        adj[v].insert(u);
    }

    vector<pair<int,int>> numOfAdjEdges(n+1);
    numOfAdjEdges[0] = {0,0};

    for(int i=1; i<=n; i++) {
        numOfAdjEdges[i] = {-adj[i].size(), i};
    }

    sort(numOfAdjEdges.begin(), numOfAdjEdges.end());
    // for(int i=0; i<n; i++) {
    //     cout << -numOfAdjEdges[i].first << ", " << numOfAdjEdges[i].second << endl;
    // }

    int len = 0;
    while(len < n && numOfAdjEdges[len].first == numOfAdjEdges[0].first) len++;

    // cout << len << endl;

    if(len == 1) {
        int ans = 0;
        int e1 = -numOfAdjEdges[0].first, v1 = numOfAdjEdges[0].second;
        // cout << v1 << " : " << e1 << endl;

        for(int i=1; i<n; i++) {
            int e2 = -numOfAdjEdges[i].first;
            int v2 = numOfAdjEdges[i].second;

            // cout << v2 << " : " << e2 << endl;

            if(e2 != -numOfAdjEdges[1].first) break;

            int comps = e1+e2-1;
            if(adj[v1].find(v2) != adj[v1].end()) comps--;

            ans = max(ans, comps);
        }

        cout << ans << endl;
    }
    else {
        int ans = 0;
        int e1 = -numOfAdjEdges[0].first, v1 = numOfAdjEdges[0].second;
        // cout << v1 << " : " << e1 << endl;

        for(int i=1; i<len; i++) {
            int e2 = -numOfAdjEdges[i].first;
            int v2 = numOfAdjEdges[i].second;

            // cout << v2 << " : " << e2 << endl;

            int comps = e1+e2-1;
            if(adj[v1].find(v2) != adj[v1].end()) comps--;

            ans = max(ans, comps);
        }

        cout << ans << endl;
    }
}

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

    while(testCases--) solve();
    return 0;
}

Answer: 1