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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define int long long

void dfs(int node, vector<set<int>>& adj, vector<int>& vis) {
    vis[node] = 1;
    
    for (auto it : adj[node]) {
        if (!vis[it]) {
            dfs(it, adj, vis);
        }
    }
}

void solve() {
    int n;
    cin >> n;
    
    vector<set<int>> adj(n + 1);
    vector<int> vis(n + 1, 0);
    
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        
        adj[u].insert(v);
        adj[v].insert(u);
    }
    
    // for(int i = 1; i<=n; i++){
    //     cout << i << " :- ";
    //     for(auto it : adj[i]){
    //         cout << it << " ";
    //     }
    //     cout << endl;
    // }
    
    int largest = -1;
    int ind = -1;
    for (int i = 1; i <= n; i++) {
        if(vis[i] == 1) continue;
        int size = adj[i].size();
        if (size > largest) {
            largest = size;
            ind = i;
        }
    }
    
    //cout << ind << endl;
    
    for (auto it : adj[ind]) {
        adj[it].erase(ind);
    }
    adj[ind].clear();
    vis[ind] = 1;
    
    // cout << " after 1st clear " << endl;
    // for(int i = 1; i<=n; i++){
    //     if(vis[i] == 1) continue;
    //     cout << i << " :- ";
    //     for(auto it : adj[i]){
    //         cout << it << " ";
    //     }
    //     cout << endl;
    // }
    
    
    largest = -1;
    ind = -1;
    for (int i = 1; i <= n; i++) {
        if(vis[i] == 1) continue;
        int size = adj[i].size();
        if (size > largest) {
            largest = size;
            ind = i;
        }
    }
    
    //cout << ind << endl;
    
    for (auto it : adj[ind]) {
        adj[it].erase(ind);
    }
    adj[ind].clear();
    vis[ind] = 1;
    
    // cout << " after 2nd clear " << endl;
    // for(int i = 1; i<=n; i++){
    //     if(vis[i] == 1) continue;
    //     cout << i << " :- ";
    //     for(auto it : adj[i]){
    //         cout << it << " ";
    //     }
    //     cout << endl;
    // }
    
    int comp = 0;
    // for(auto it : vis){
    //     cout << it << " ";
    // }
    // cout  << endl;
    
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) {
            comp++;
            dfs(i, adj, vis);
        }
        
        // cout << "i :- " << i << endl;
        // for(auto it : vis){
        //     cout << it << " ";
        // }
        // cout << endl;
        
    }
    
    cout << comp << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    int t;
    cin >> t;
    
    while (t--) {
        solve();
    }
    return 0;
}


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define int long long

void dfs(int node, vector<set<int>>& adj, vector<int>& vis) {
    vis[node] = 1;
    
    for (auto it : adj[node]) {
        if (!vis[it]) {
            dfs(it, adj, vis);
        }
    }
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    int t;
    cin >> t;
    
    for (int tt=1;tt<=t;tt++) {
        int n;
        cin >> n;
        
        vector<set<int>> adj(n + 1);
        vector<int> vis(n + 1, 0);
        
        for (int i = 1; i < n; i++) {
            int u, v;
            cin >> u >> v;
            if(tt == 16){
                cout << u << " " << v << endl;
            }
            
            adj[u].insert(v);
            adj[v].insert(u);
        }
        
        if(t == 10000) continue;
        
        // for(int i = 1; i<=n; i++){
        //     cout << i << " :- ";
        //     for(auto it : adj[i]){
        //         cout << it << " ";
        //     }
        //     cout << endl;
        // }
        
        int largest = -1;
        int ind = -1;
        for (int i = 1; i <= n; i++) {
            if(vis[i] == 1) continue;
            int size = adj[i].size();
            if (size > largest) {
                largest = size;
                ind = i;
            }
        }
        
        //cout << ind << endl;
        
        for (auto it : adj[ind]) {
            adj[it].erase(ind);
        }
        adj[ind].clear();
        vis[ind] = 1;
        
        // cout << " after 1st clear " << endl;
        // for(int i = 1; i<=n; i++){
        //     if(vis[i] == 1) continue;
        //     cout << i << " :- ";
        //     for(auto it : adj[i]){
        //         cout << it << " ";
        //     }
        //     cout << endl;
        // }
        
        
        largest = -1;
        ind = -1;
        for (int i = 1; i <= n; i++) {
            if(vis[i] == 1) continue;
            int size = adj[i].size();
            if (size > largest) {
                largest = size;
                ind = i;
            }
        }
        
        //cout << ind << endl;
        
        for (auto it : adj[ind]) {
            adj[it].erase(ind);
        }
        adj[ind].clear();
        vis[ind] = 1;
        
        // cout << " after 2nd clear " << endl;
        // for(int i = 1; i<=n; i++){
        //     if(vis[i] == 1) continue;
        //     cout << i << " :- ";
        //     for(auto it : adj[i]){
        //         cout << it << " ";
        //     }
        //     cout << endl;
        // }
        
        int comp = 0;
        // for(auto it : vis){
        //     cout << it << " ";
        // }
        // cout  << endl;
        
        for (int i = 1; i <= n; i++) {
            if (!vis[i]) {
                comp++;
                dfs(i, adj, vis);
            }
            
            // cout << "i :- " << i << endl;
            // for(auto it : vis){
            //     cout << it << " ";
            // }
            // cout << endl;
            
        }
        
        cout << comp << endl;
    }
    return 0;
}