Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

using namespace std;

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

    vector<vector<int>> adj(n);
    int u, v;

    set<pair<int, int>> st;
    vector<int> degree(n, 0);

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

        st.insert({min(u, v), max(u, v)});
        ++degree[u];
        ++degree[v];
    }

    map<int, vector<int>, greater<int>> mp;
    for(int i=0; i<n; i++)
    mp[degree[i]].push_back(i);

    if(mp.begin()->second.size() >= 3)
    {
        int d = mp.begin()->first;
        cout << (2*d - 1) << "\n";
        return;
    }

    auto it1 = mp.begin();
    auto it2 = next(it1);

    int d1 = it1->first;
    int d2 = (it2 != mp.end()) ? it2->first : 0;

    int len1 = it1->second.size();
    int len2 = (it2 != mp.end()) ? it2->second.size() : 0;

    int maxi = 0;
    for(int i=0; i<len1; i++)
    {
        int node1 = it1->second[i];

        if(i == 0 && len1 == 2)
        {
            int node2 = it1->second[1];
            if(st.find({min(node1, node2), max(node1, node2)}) != st.end())
            maxi = max(maxi, 2*d1 - 2);

            else
            {
                maxi = max(maxi, 2*d1 - 1);
                cout << maxi << "\n";
                return;
            }
        }

        for(int j=0; j<len2; j++)
        {
            int node2 = it2->second[i];
            if(st.find({min(node1, node2), max(node1, node2)}) != st.end())
            maxi = max(maxi, d1 + d2 - 2);

            else
            maxi = max(maxi, d1 + d2 - 1);
        }
    }

    cout << maxi << "\n";
}

int main()
{
    int t;
    cin >> t;

    while(t > 0)
    {
        solve();
        --t;
    }

    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>

using namespace std;

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

    vector<vector<int>> adj(n);
    int u, v;

    set<pair<int, int>> st;
    vector<int> degree(n, 0);

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

        st.insert({min(u, v), max(u, v)});
        ++degree[u];
        ++degree[v];
    }

    map<int, vector<int>, greater<int>> mp;
    for(int i=0; i<n; i++)
    mp[degree[i]].push_back(i);

    if(mp.begin()->second.size() >= 3)
    {
        int d = mp.begin()->first;
        cout << (2*d - 1) << "\n";
        return;
    }

    auto it1 = mp.begin();
    auto it2 = next(it1);

    int d1 = it1->first;
    int d2 = (it2 != mp.end()) ? it2->first : 0;

    int len1 = it1->second.size();
    int len2 = (it2 != mp.end()) ? it2->second.size() : 0;

    int maxi = 0;
    for(int i=0; i<len1; i++)
    {
        int node1 = it1->second[i];

        if(i == 0 && len1 == 2)
        {
            int node2 = it1->second[1];
            if(st.find({min(node1, node2), max(node1, node2)}) != st.end())
            maxi = max(maxi, 2*d1 - 2);

            else
            {
                maxi = max(maxi, 2*d1 - 1);
                cout << maxi << "\n";
                return;
            }
        }

        for(int j=0; j<len2; j++)
        {
            int node2 = it2->second[j];
            if(st.find({min(node1, node2), max(node1, node2)}) != st.end())
            maxi = max(maxi, d1 + d2 - 2);

            else
            maxi = max(maxi, d1 + d2 - 1);
        }
    }

    cout << maxi << "\n";
}

int main()
{
    int t;
    cin >> t;

    while(t > 0)
    {
        solve();
        --t;
    }

    return 0;
}