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] = 0;
    out = findMax(deg);
    deg[out] = 0;

    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] == 0 || deg[v] == 0) 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]) continue;
        ans++;
        dfs(i, adj, visited);
    }

    cout << ans-2;
}
 
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 1):
#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] = 0;

    // 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);
    deg[out2] = 0;

    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';
    } 
}