Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

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

using namespace std;

#define ll long long
#define nl '\n'
#define all(a) a.begin(), a.end()
#define alli(a, r) a.begin() + r, a.end()
#define allr(a) a.rbegin(), a.rend()
const ll N = 2e5 + 7, MOD = 1e9 + 7, INF = 1e9, LG = 18;

int n, m;

struct DSU {

private:
    vector<ll> parents, sizes;
public:
    ll sz, numberOfComponent;
    void init(int szz) {
        sz = szz;
        numberOfComponent = szz;
        parents.resize(sz + 1);
        sizes.resize(sz + 1);

        for (int i = 0; i <= sz; i++) {
            parents[i] = i;
            sizes[i] = 1;

        }
    }

    int find_root(int x) {
        if (parents[x] == x) {
            return x;
        }
        return parents[x] = find_root(parents[x]);
    }

    bool same_set(int i, int j) {
        int x = find_root(i);
        int y = find_root(j);
        return x == y;
    }

    bool merge(int u, int v) {
        int root_u = find_root(u);
        int root_v = find_root(v);
        if (root_u == root_v)
            return false;
        if (sizes[root_u] > sizes[root_v])
            swap(root_u, root_v);
        parents[root_u] = root_v;
        sizes[root_v] += sizes[root_u];
        numberOfComponent--;
        return true;
    }

    void setparent(int i, int j) {
        parents[i] = j;
    }


};

void sol(int tc) {

    cin>>n;
    vector<int>adj[n+1];
    set<pair<int,int>>edges;
    for(int i = 0 ;i<n-1;i++)
    {
        int u,v;
        cin>>u>>v;
       adj[u].push_back(v);
       adj[v].push_back(u);
       edges.insert({min(u,v),max(u,v)});
    }
    multiset<pair<int,int>,greater<>>ms;
    for(int i =1;i<=n;i++)
    {
        ms.insert({adj[i].size(),i});
    }
    int x = ms.begin()->second;
    int y ;
    ms.erase(ms.begin());
    while(!ms.empty())
    {
        auto it =*ms.begin();
        ms.erase(ms.begin());
        pair<int,int> check = {min(x,it.second),max(x,it.second)};
        auto it2 = edges.find(check);
        if(it2==edges.end())
        {
            y = it.second;
            break;
        }
        ms.insert({adj[it.second].size()-1,it.second});
        edges.erase(it2);
    }

    DSU dsu;
    dsu.init(n);
    for(auto&i:edges)
    {
        auto[u,v] = i;
        if (u != x && u != y && v != x  && v != y) {
            dsu.merge(u, v);
        }
    }

    cout<<dsu.numberOfComponent-2;

}

void file() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    // file();

    int tests = 1;
    cin >> tests;
    for (int tes = 1; tes <= tests; ++tes) {

        sol(tes);
        cout<<nl;

    }
    return 0;
}


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

using namespace std;

#define ll long long
#define nl '\n'
#define all(a) a.begin(), a.end()
#define alli(a, r) a.begin() + r, a.end()
#define allr(a) a.rbegin(), a.rend()
const ll N = 2e5 + 7, MOD = 1e9 + 7, INF = 1e9, LG = 18;

int n, m;

struct DSU {

private:
    vector<ll> parents, sizes;
public:
    ll sz, numberOfComponent;
    void init(int szz) {
        sz = szz;
        numberOfComponent = szz;
        parents.resize(sz + 1);
        sizes.resize(sz + 1);

        for (int i = 0; i <= sz; i++) {
            parents[i] = i;
            sizes[i] = 1;

        }
    }

    int find_root(int x) {
        if (parents[x] == x) {
            return x;
        }
        return parents[x] = find_root(parents[x]);
    }

    bool same_set(int i, int j) {
        int x = find_root(i);
        int y = find_root(j);
        return x == y;
    }

    bool merge(int u, int v) {
        int root_u = find_root(u);
        int root_v = find_root(v);
        if (root_u == root_v)
            return false;
        if (sizes[root_u] > sizes[root_v])
            swap(root_u, root_v);
        parents[root_u] = root_v;
        sizes[root_v] += sizes[root_u];
        numberOfComponent--;
        return true;
    }

    void setparent(int i, int j) {
        parents[i] = j;
    }


};

void sol(int tc) {

    cin>>n;
    vector<int>adj[n+1];
    set<pair<int,int>>edges;
    map<int,int>mp;
    for(int i = 0 ;i<n-1;i++)
    {
        int u,v;
        cin>>u>>v;
       adj[u].push_back(v);
       adj[v].push_back(u);
       edges.insert({min(u,v),max(u,v)});
       mp[u]++;
       mp[v]++;
    }

    int x , mx = -1 ;
    for(auto& i :mp) {
        if (i.second > mx) {
            x = i.first;
            mx = i.second;
        }
    }
    for(auto&i:adj[x])
    {
        edges.erase({min(i,x),max(i,x)});
        mp[i]--;
        mp[x]--;
    }

    int y , mxx = -1 ;
    for(auto& i :mp)
    {
        if(i.second>mxx)
        {
            y = i.first;
            mxx = i.second;
        }
    }
    DSU dsu;
    dsu.init(n);
    for(auto&i:edges)
    {
        auto[u,v] = i;
        if (u != x && u != y && v != x  && v != y) {
            dsu.merge(u, v);
        }
    }

    cout<<dsu.numberOfComponent-2;

}

void file() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    // file();

    int tests = 1;
    cin >> tests;
    for (int tes = 1; tes <= tests; ++tes) {

        sol(tes);
        cout<<nl;

    }
    return 0;
}