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

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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        set<pair<int, int>> edges;
        vector<int> indegree(n + 1, 0);
        for (int i = 0; i < n - 1; i++)
        {
            int first, second;
            cin >> first >> second;
            indegree[first]++;
            indegree[second]++;
            edges.insert({first, second});
            edges.insert({second, first});
        }
        vector<pair<int, int>> degree;
        for (int i = 1; i <= n; i++)
        {
            degree.push_back({indegree[i], i});
        }
        sort(degree.rbegin(), degree.rend());

        int res = 0;
        // first we will do for non adjacent
        for (int i = 0; i < n; i++)
        {
            int degree1 = degree[i].first;
            int ele1 = degree[i].second;
            for (int j = i + 1; j < n; j++)
            {
                int degree2 = degree[j].first;
                int ele2 = degree[j].second;
                if (edges.count({ele1, ele2}))
                    continue;
                else
                {
                    res = max(res, degree1 + degree2 - 1);
                    break;
                }
            }
        }
        // now we will do for adjacent elements
        for (auto a : edges)
        {
            int first = a.first;
            int second = a.second;
            int degree1 = indegree[first];
            int degree2 = indegree[second];
            res = max(res, degree1 + degree2 - 2);
        }
        cout << res << endl;
    }

    return 0;
}


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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;

        vector<unordered_set<int>> adj(n + 1);
        for (int i = 0; i < n - 1; i++)
        {
            int first, second;
            cin >> first >> second;
            adj[first].insert(second);
            adj[second].insert(first);
        }
        int maxi = 0, index = 0;

        for (int i = 1; i <= n; i++)
        {
            int key = i;
            unordered_set<int> &neighbors = adj[i];
            if (neighbors.size() > maxi)
            {
                maxi = neighbors.size();
                index = key;
            }
        }
        unordered_set<int> &byebye = adj[index];
        // cout << index << endl;
        for (auto a : byebye)
        {
            int currele = a;
            adj[currele].erase(index);
        }
        byebye.clear();
        byebye.insert(-1);

        maxi = -1, index = 0;

        for (int i = 1; i <= n; i++)
        {
            int key = i;
            unordered_set<int> &neighbors = adj[i];
            if (neighbors.count(-1))
            {
                // cout << i << endl;
                continue;
            }

            if ((int)neighbors.size() > maxi)
            {

                maxi = neighbors.size();
                index = i;
            }
        }

        unordered_set<int> &byebye2 = adj[index];
        // cout << index << endl;
        for (auto a : byebye2)
        {
            int currele = a;
            adj[currele].erase(index);
        }
        byebye2.clear();
        byebye2.insert(-1);
        int res = 0;
        unordered_set<int> selected;
        for (int i = 1; i <= n; i++)
        {
            unordered_set<int> &neighbors = adj[i];
            if (neighbors.count(-1) || selected.count(i))
                continue;
            selected.insert(i);
            for (auto a : neighbors)
            {
                selected.insert(a);
            }
            res++;
        }
        cout << res << endl;
    }
    return 0;
}


Answer: 1