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


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb emplace_back
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
#define trace(x) cerr << #x << " = " << x << '\n'

using namespace std;

template<class T> bool chmax(T& A, T&& B) { return B > A? A = B, true : false; }
template<class T> bool chmin(T& A, T&& B) { return B < A? A = B, true : false; }

const int64_t DESPACITO = 2e18;
const int INF = 2e9, MOD = 1e9+7;
const int MxN = 2e5 + 5;

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define MOD1 998244353
#define INF1 1e18
#define nline "\n"
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
ll oo=998244353;
ll mul(ll x,ll y)
{
    return ((x%oo)*(y%oo))%oo;
}
ll add(ll x,ll y)
{
    return ((x%oo)+(y%oo))%oo;
}
ll sub(ll x,ll y)
{
    return ((x%oo)-(y%oo)+oo)%oo;
}
ll fast_pow(ll x,ll y)
{
    if(y==0)return 1;
    if(y==1)return x;
    ll ret=1;
    ret=fast_pow(x,y/2);
    ret=mul(ret,ret);
    if(y%2==1)ret=mul(ret,x);
    return ret;
}
ll fact[2000200];
void factorial()
{
    fact[0]=1;
    for(int i=1; i<=1000000; i++)
    {
        fact[i]=mul(fact[i-1],i);
    }
}
ll calculate_inverse(ll x)
{
    return fast_pow(x,oo-2);
}
ll n_choose_k(ll n,ll k)
{
    if(k<0)return 0;
    ll ret=1;
    ret=fact[n];
    ret=mul(ret,calculate_inverse(fact[k]));
    ret=mul(ret,calculate_inverse(fact[n-k]));
    return ret;
}
ll gcd(ll x,ll y)
{
    if(x<y)swap(x,y);
    if(y==0)return x;
    return gcd(y,x%y);
}
struct DSU {
    vector<int> parent, size;
    int components;

    DSU(int n) {
        parent.resize(n);
        size.resize(n, 1);
        components = n;
        iota(parent.begin(), parent.end(), 0);
    }

    int find(int x) {
        if (parent[x] != x) {
            parent[x] = find(parent[x]);
        }
        return parent[x];
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (size[x] < size[y]) swap(x, y);
        parent[y] = x;
        size[x] += size[y];
        components--;
    }
};

void solve() {
    int n;
    cin>>n;
    vector<set<int>>g(n);
    vector<int>deg(n,0);
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        --u; 
        --v;
        deg[u]++;
        deg[v]++;
        g[u].insert(v);
        g[v].insert(u);
    }

    vector<int>idx(n);
    iota(all(idx),0);
    sort(all(idx),[&](int u, int v){
        return deg[u]>deg[v];
    });

    int v1=idx[0],v2=idx[1];
    DSU dsu(n);

    for(int u=0;u<n;u++){
        if(u==v1||u==v2)continue;
        for(int v:g[u]){
            if(v==v1||v==v2)continue;
            dsu.unite(u,v);
        }
    }

    cout<<dsu.components-2<<endl;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
#endif
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}