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 

int findMax(vector<int>& deg){
    int ans = 0;
    for (int i = 1; i < deg.size(); i++){
        if (deg[i] > deg[ans]){
            ans = i;
        }
    }
    return ans;
}

void dfs(int cur, vector<vector<int>>& adj, vector<bool>& visited){
    visited[cur] = true;

    for (int next:adj[cur]){
        if (visited[next]) continue;
        dfs(next, adj, visited);
    }
}
 
void solve(){
    int n;
    cin >> n;
    vector<int> deg(n);
    vector<vector<int>> edge;

    for (int i = 0; i < n-1; i++){
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        edge.push_back({u, v});
        deg[u]++;
        deg[v]++;
    }

    int out = findMax(deg);
    deg[out] = -1;

    // cerr << out << endl;

    
    for (int i = 0; i < n-1; i++){
        int u = edge[i][0], v = edge[i][1];

        if (u == out) deg[v]--;
        if (v == out) deg[u]--;
    }

    int out2 = findMax(deg);

    vector<vector<int>> adj(n);

    for (int i = 0; i < n-1; i++){
        int u = edge[i][0], v = edge[i][1];

        if (u == out || u == out2 || v == out || v == out2) continue;
        adj[u].push_back(v);
        adj[v].push_back(u);

    }

    vector<bool> visited(n, false);
    int ans = 0;
    for (int i = 0; i < n; i++){
        if (visited[i] || i == out || i == out2) continue;
        // cerr << i << endl;
        ans++;
        dfs(i, adj, visited);
    }

    cout << ans;
}
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1; 
    cin >> t; 
    while (t--){ 
        solve();
        cout << '\n';
    } 
}

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

int n;

int findMax(vector<int>& connect, vector<vector<int>>& edge, vector<int>& deg){
    int maxi = 0;
    for (int i = 0; i < n-1; i++){
        int u = edge[i][0], v = edge[i][1];
    
        if (deg[u] == 1) connect[v]++;
        if (deg[v] == 1) connect[u]++;
        if (connect[u] > connect[maxi]) maxi = u;
        if (connect[v] > connect[maxi]) maxi = v;
    }
    return maxi;
}

void dfs(int cur, vector<vector<int>>& adj, vector<bool>& visited){
    visited[cur] = true;
 
    for (int next:adj[cur]){
        if (visited[next]) continue;
        dfs(next, adj, visited);
    }
}

 
void solve(){
    cin >> n;
    vector<int> deg(n);
    vector<vector<int>> edge;

    for (int i = 0; i < n-1; i++){
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        edge.push_back({u, v});
        deg[u]++;
        deg[v]++;
    }

    vector<int> connect(n, 0);
    int maxi = findMax(connect, edge, deg);
    // cerr << maxi << endl;

    deg[maxi] = -1;
    

    for (int i = 0; i < n-1; i++){
        int u = edge[i][0], v = edge[i][1];

        if (u == maxi) deg[v]--;
        if (v == maxi) deg[u]--;
    }
    connect.assign(n, 0);
    connect[maxi] = -1;
    maxi = findMax(connect, edge, deg);
    // cerr << maxi << endl;

    deg[maxi] = -1;


    vector<vector<int>> adj(n);
 
    for (int i = 0; i < n-1; i++){
        int u = edge[i][0], v = edge[i][1];
 
        if (deg[u] == -1 || deg[v] == -1) continue;
        adj[u].push_back(v);
        adj[v].push_back(u);
 
    }
 
    vector<bool> visited(n, false);
    int ans = 0;
    for (int i = 0; i < n; i++){
        if (visited[i] || deg[i] == -1) continue;
        ans++;
        dfs(i, adj, visited);
    }
 
    cout << ans;

}
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1; 
    cin >> t; 
    while (t--){ 
        solve();
        cout << '\n';
    } 
}