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

Previous submission (verdict: Wrong answer on test 4):
#include<bits/stdc++.h>
#define int long long int
#define endl "\n"
#define pb push_back
#define vi vector<int>
#define vv vector<vi>
#define pr pair<int, int>
#define vp vector<pair<int, int>> 
#define all(v) v.begin(), v.end()
#define fr(i, x, n) for(int i=x;i<n;i++)
#define Fr(i, x, n) for(int i=x;i>=n;i--)
#define F first
#define S second
#define speedpenchu cin.tie(0); cout.tie(0);ios_base::sync_with_stdio(0);
using namespace std;
const int MOD = 1000000007;
const int INF = 1E18;

int gcd(int a, int b){if(b == 0) return a;return gcd(b, a%b);}
int add(int a, int b){int m = MOD;return ((a%m)+(b%m)+m)%m;}
int sub(int a, int b){int m = MOD;return ((b%m)-(a%m)+m)%m;}
int mul(int a, int b){int m = MOD;return ((a%m)*(b%m))%m;}

class DSU{ 
    public:
    vector<int> rank, parent, size;

    DSU(int n){
        rank.resize(n+1,0);
        parent.resize(n+1);
        size.resize(n+1);
        for(int i=0;i<n;i++){
            parent[i] = i;
            size[i] = 1;
        }
    }

    int findUPar(int node){
        if(node == parent[node]){
            return node;
        }
        return parent[node] = findUPar(parent[node]);
    }

    void unionBySize(int u,int v){
        int ulp_u = findUPar(u);
        int ulp_v = findUPar(v);
        if(ulp_u == ulp_v) return;
        if(size[ulp_u] < size[ulp_v]){
            parent[ulp_u] = ulp_v;
            size[ulp_v]+=size[ulp_u];
        }
        else{
            parent[ulp_v] = ulp_u;
            size[ulp_u]+=size[ulp_v];
        }
    }
};

vi adj[200005]; 
int L[200005], R[200005];
void javabu(){ 
    int n; cin>>n; 
    map<int, int> mp;
    fr(i, 0, n) adj[i].clear();
    fr(i, 0, n-1){
        int u, v; cin>>u>>v; --u; --v;
        L[i] = u; R[i] = v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    fr(i, 0, n){
        for(auto &node: adj[i]){
            mp[node]++;
        }
    }

    set<pr> st;
    for(auto &it : mp){
        st.insert({it.second, it.first});
    }
    int node1 = (*--st.end()).S;
    st.erase(--st.end());
    for(auto &n : adj[node1]){
        st.erase({mp[n], n});
        st.insert({--mp[n], n});
    }
    int node2 = (*--st.end()).S;

    DSU dsu(n);
    fr(i, 0, n){
        if(L[i] == node1 || L[i] == node2 || R[i] == node1 || R[i] == node2 ) continue;
        dsu.unionBySize(L[i], R[i]);
    }

    set<int> res;
    fr(i, 0, n){
        if(i == node1 || i == node2) continue;
        res.insert(dsu.findUPar(i));
    }

    cout<<res.size()<<endl;
   
}


signed main() {
    speedpenchu
    int t;
    cin >> t;
    while (t--) {
        javabu();
    }
    return 0;
}

Submission (verdict: Wrong answer on test 4):
#include<bits/stdc++.h>
#define int long long int
#define endl "\n"
#define pb push_back
#define vi vector<int>
#define vv vector<vi>
#define pr pair<int, int>
#define vp vector<pair<int, int>> 
#define all(v) v.begin(), v.end()
#define fr(i, x, n) for(int i=x;i<n;i++)
#define Fr(i, x, n) for(int i=x;i>=n;i--)
#define F first
#define S second
#define speedpenchu cin.tie(0); cout.tie(0);ios_base::sync_with_stdio(0);
using namespace std;
const int MOD = 1000000007;
const int INF = 1E18;

int gcd(int a, int b){if(b == 0) return a;return gcd(b, a%b);}
int add(int a, int b){int m = MOD;return ((a%m)+(b%m)+m)%m;}
int sub(int a, int b){int m = MOD;return ((b%m)-(a%m)+m)%m;}
int mul(int a, int b){int m = MOD;return ((a%m)*(b%m))%m;}


vi adj[200005]; 
// int L[200005], R[200005];
void javabu(){ 
    int n; cin>>n; 
    map<int, int> mp;
    fr(i, 0, n) adj[i].clear();
    fr(i, 0, n-1){
        int u, v; cin>>u>>v; --u; --v;
        // L[i] = u; R[i] = v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    fr(i, 0, n){
        for(auto &node: adj[i]){
            mp[node]++;
        }
    }

    set<pr> st; int res = 0;
    for(auto &it : mp){
        st.insert({it.second, it.first});
    }
    int node1 = (*--st.end()).S;
    res += (*--st.end()).F;
    st.erase(--st.end());
    for(auto &n : adj[node1]){
        st.erase({mp[n], n});
        st.insert({--mp[n], n});
    }
    int node2 = (*--st.end()).S;
    res += (*--st.end()).F;

    // DSU dsu(n);
    // fr(i, 0, n){
    //     if(L[i] == node1 || L[i] == node2 || R[i] == node1 || R[i] == node2 ) continue;
    //     dsu.unionBySize(L[i], R[i]);
    // }

    // set<int> res;
    // fr(i, 0, n){
    //     if(i == node1 || i == node2) continue;
    //     res.insert(dsu.findUPar(i));
    // }

    cout<<res-1<<endl;
   
}


signed main() {
    speedpenchu
    int t;
    cin >> t;
    while (t--) {
        javabu();
    }
    return 0;
}