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;

#define int long long 

void dfs(unordered_map<int, vector<int>>& adj, int index, int p, vector<int>& parent, vector<int>& c_count)
{
    int count = 0;

    for(int i : adj[index])
    {
        if(i != p)
        {
            parent[i] = index;
            count++;
            dfs(adj, i, index, parent, c_count);
        }
    }

    c_count[index] = count;
}

signed main()
{
    int tt;
    cin >> tt;

    while(tt--)
    {
        int n;
        cin >> n;

        unordered_map<int, vector<int>> adj;

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

            u--;
            v--;

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

        vector<int> parent(n, -1);
        vector<int> c_count(n, 0);

        dfs(adj, 0, -1, parent, c_count);
        set<pair<int, int>> st;

        for(int i = 0; i < n; i++) st.insert({c_count[i], i});

        int ans = 0;

        int f = (*st.rbegin()).second;
        st.erase(*st.rbegin());

        ans += c_count[f];
        auto record = st.find({c_count[parent[f]], parent[f]});
        if(record != st.end()) st.erase(record);

        if(f!=0)st.insert({c_count[parent[f]] - 1, parent[f]});

        if(!st.empty())
        {
            ans += (*st.rbegin()).first;
            st.erase(prev(st.end()));
        }

        if(st.empty())
        {
            cout << 0 << endl;
            continue;
        }

        cout << ans << endl;
    }

    return 0;
}

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;

#define int long long 

void dfs(unordered_map<int, vector<int>>& adj, int index, int p, vector<int>& parent, vector<int>& c_count)
{
    int count = 0;

    for(int i:adj[index])
    {
        if(i!=p)
        {
            parent[i] = index;
            count++;
            dfs(adj, i, index, parent, c_count);
        }
    }

    c_count[index] = count;
}

signed main()
{
    int tt;
    cin>>tt;

    while(tt--)
    {
        int n;
        cin>>n;

        unordered_map<int, vector<int>> adj;

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

            u--;
            v--;

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

        vector<int> parent(n, -1);
        vector<int> c_count(n, 0);

        dfs(adj, 0, -1, parent, c_count);
        set<pair<int, int>> st;

        for(int i=0; i<n; i++)st.insert({c_count[i], i});

        int ans = 0;
        int f = (*st.rbegin()).second;
        ans+=c_count[f];
        st.erase(*st.rbegin());

        if(f!=0)
        {
            auto record = st.find({c_count[parent[f]], parent[f]});
            if(record != st.end())st.erase(record);
            c_count[parent[f]]--;
            st.insert({c_count[parent[f]], parent[f]});
        }


        int f1 = (*st.rbegin()).second;
        ans+=c_count[f1];
        st.erase(*st.rbegin());

        if(st.empty())
        {
            cout<<0<<endl;
            continue;
        }

        cout<<ans<<endl;
    }

    return 0;
}