Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
#ifndef InfernoStinger
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
#define all(x) (x).begin() , (x).end()
#define ll long long
#define int long long  
#define mod 1000000007
#define mod1 998244353
#define inf 1e18
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(long double t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(bool t) {cerr << t;}
void _print(unsigned long long t) {cerr << t;}
template <class T> bool ckmin(T&a, const T&b) { bool B = a > b; a = min(a,b); return B; }
template <class T> bool ckmax(T&a, const T&b) { bool B = a < b; a = max(a,b); return B; }
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 << "]";}

int solve()
{   
    int n;
    cin>>n;
    vector<set<int>> g(n+1);
    vector<int> deg(n+1, 0), obs(n+1, false);
    for(int i=0;i<n-1;i++){
        int v,u;
        cin>>v>>u;
        g[v].insert(u);
        g[u].insert(v);
        deg[u]++;deg[v]++;
    }
    auto red = [&]() -> void {
        int val=0;
        for(int i=1;i<=n;i++){
            if(obs[i]) continue;
            if(deg[i]>=deg[val]){
                val=i;
            }
        }
        obs[val]=true;
        vector<int> torem;
        for(auto ver:g[val]){
            torem.pb(ver);
        }
        for(auto rem:torem){
            g[rem].erase(val);
            g[val].erase(rem);
            deg[rem]--;deg[val]--;
        }
    };
    red();
    // debug(obs);
    red();
    // debug(obs);
    queue<int> q;
    vector<bool> vis(n+1, false);
    int ans=0;
    for(int i=1;i<=n;i++){
        if(vis[i] || obs[i]) continue;
        q.push(i);
        ans++;
        while(q.size()){
            int node=q.front();
            q.pop();
            vis[node]=true;
            for(auto ver:g[node]){
                if(!vis[ver] && !obs[ver]) q.push(ver);
            }
        }
    }
    cout<<ans<<"\n";
    return 0;
}
 
int32_t main() 
{   
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
    #endif
    int t;
    cin>>t;
    for(int tc=0;tc<t;tc++)
    {   
        solve();   
    }
    return 0;
}

Submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
#ifndef InfernoStinger
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
#define all(x) (x).begin() , (x).end()
#define ll long long
#define int long long  
#define mod 1000000007
#define mod1 998244353
#define inf 1e18
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(long double t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(bool t) {cerr << t;}
void _print(unsigned long long t) {cerr << t;}
template <class T> bool ckmin(T&a, const T&b) { bool B = a > b; a = min(a,b); return B; }
template <class T> bool ckmax(T&a, const T&b) { bool B = a < b; a = max(a,b); return B; }
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 << "]";}

int solve()
{   
    int n;
    cin>>n;
    vector<set<int>> g(n+1);
    vector<int> deg(n+1, 0);
    set<int> cur;
    for(int i=1;i<=n;i++){
        cur.insert(i);
    }
    for(int i=0;i<n-1;i++){
        int v,u;
        cin>>v>>u;
        g[v].insert(u);
        g[u].insert(v);
        deg[u]++;deg[v]++;
    }
    auto red = [&]() -> void {
        int val=0;
        for(auto i:cur){
            if(deg[i]>=deg[val]){
                val=i;
            }
        }
        vector<int> torem;
        for(auto ver:g[val]){
            torem.pb(ver);
        }
        for(auto rem:torem){
            g[rem].erase(val);
            g[val].erase(rem);
            deg[rem]--;deg[val]--;
        }
        cur.erase(val);
    };
    red();
    red();
    queue<int> q;
    vector<bool> vis(n+1, false);
    int ans=0;
    for(auto i:cur){
        if(vis[i]) continue;
        q.push(i);
        ans++;
        while(q.size()){
            int node=q.front();
            q.pop();
            vis[node]=true;
            for(auto ver:g[node]){
                if(!vis[ver]) q.push(ver);
            }
        }
    }
    cout<<ans<<"\n";
    return 0;
}
 
int32_t main() 
{   
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
    #endif
    int t;
    cin>>t;
    for(int tc=0;tc<t;tc++)
    {   
        solve();   
    }
    return 0;
}