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<pair<int, int>> degree(n);
    // vector<vector<int>> adj(n);

    for(int i=0; i<n; i++)
    degree[i] = {0, i};

    for(int i=0; i<n-1; i++)
    {
        cin >> u >> v;
        --u;
        --v;
        st.insert({min(u, v), max(u, v)});
        ++degree[u].first;
        ++degree[v].first;
        // adj[u].push_back(v);
        // adj[v].push_back(u);
    }

    if(n == 2)
    {
        cout << "0\n";
        return;
    }

    // the more degree a node has, the more connected components it will have
    // after removal of it
    sort(degree.begin(), degree.end(), greater<pair<int, int>>());

    // consider 3 adjacent nodes n1, n2, n3 with max degree
    // Let max degree be 2
    // In this case, there has to be an edge {n1, n2} or {n2, n3} or {n1, n3}
    // which is not part of the graph.
    // If all 3 were part, then it would have cycle and won't be a tree

    // so, if > 2 nodes with max degree, answer = maxdegree + maxdegree - 1
    // basically, we remove those 2 nodes which don't have common edge
    // if there are 2 max degree nodes, we can brute force with all other possibilities

    int maxDegreeCount = 1, secMaxDegreeCount = 0;
    int maxVal = degree[0].first, secMaxVal = -1;

    bool first = true, second = true;
    for(int i=1; i<n; i++)
    {
        if(first && degree[i].first == degree[i - 1].first)
        ++maxDegreeCount;

        else if(first)
        {
            first = false;
            secMaxDegreeCount = 1;
            secMaxVal = degree[i].first;
            continue;
        }

        if(!first && second && degree[i].first == degree[i - 1].first)
        ++secMaxDegreeCount;

        else
        break;
    }

    if(maxDegreeCount >= 3)
    {
        cout << (2 * maxVal - 1) << "\n";
        return; 
    }

    int i = 0;
    int maxi = 0;
    while(i < maxDegreeCount)
    {
        auto [d1, node1] = degree[i];
        int j = i + 1;

        int limit = maxDegreeCount + secMaxDegreeCount;
        while(j < limit)
        {
            auto [d2, node2] = degree[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);
            ++j;
        }

        ++i;
    }

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

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

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

    return 0;
}

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;
}