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

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


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;

vector<int>adj[N];
vector<int>parentOfLeaves(N);
void dfs(int node ,int p)
{
    if(adj[node].size()==1)
    {
        parentOfLeaves[p]++;
        return;
    }
    for(auto&i:adj[node])
        if(p!=i)dfs(i,node);
}
void sol(int tc) {

    cin>>n;
    int node;
    for(int i =1 ;i<=n;i++)
    {
        adj[i].clear();
        parentOfLeaves[i] = 0;
    }
    for(int i = 2;i<=n;i++)
    {
        int u,v;cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        if(adj[u].size()>1)node = u;
        if(adj[v].size()>1)node = v;
    }
    if(n==2)return void(cout<<0);

    dfs(node , -1);
    multiset<pair<int,int>, greater<>>ms;
    for(int i =1;i<=n;i++)
    {
        if(parentOfLeaves[i])
            ms.insert({parentOfLeaves[i] , i});
    }

   if(ms.size()==1)
       cout<<n-2;
   else
   {
       auto it = ms.begin();
       int x = it->second;
       it++;
       int y = it->second;
       if(find(all(adj[x]),y)!=adj[x].end())
       {
           cout<<adj[x].size() -1 + adj[y].size()-1;
       }else
       {
           cout<<adj[x].size() -1 + adj[y].size();

       }

   }



}

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