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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define endl "\n"
#define all(a) a.begin(), a.end()
#define ll long long
#define ull unsigned long long
#define inf 0x7fffffff;
#define rep(i, l, r) for(int i = l; i <= r; ++i)
#define rrp(i, l, r) for(int i = l; i >= r; --i)
#define fucking_go(x) int t; cin >> t; while(t--) x()
using namespace std;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int> >;
using vvl = vector<vector<ll> >;
using vpii = vector<pair<int, int> >;
using vpll = vector<pair<ll, ll> >;
using pii = pair<int, int>;
const ll mod = 1e9 + 7;
//const ll mod = 998244353;
const ll INF = 1e18;

int n;
set<int> g[200010];
void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++) g[i].clear();
    for (int i = 1; i <= n - 1; i++) {
        int u, v;
        cin >> u >> v;
        g[u].insert(v);
        g[v].insert(u);
    }
    if (n == 2) {
        cout << 0 << endl;
        return;
    }
    vi del(n + 1);
    vi cnt(n + 1);
    for (int i = 1; i <= n; i++) {
        if (g[i].size() == 1) cnt[(*g[i].begin())]++;
    }
    int mx = -1, index = 0;
    for (int i = 1; i <= n; i++) {
        if (cnt[i] > mx) {
            mx = cnt[i];
            index = i;
        }
    }
    del[index] = 1;
    for (int x : g[index]) {
        g[x].erase(index);
        g[index].erase(x);
        if (g[x].size() == 1) {
            cnt[(*g[x].begin())]++;
        }
    }
    mx = -1, index = 0;
    for (int i = 1; i <= n; i++) {
        if (del[i] == 0) {
            if (cnt[i] > mx) {
                mx = cnt[i];
                index = i;
            }
        }
    }
    if (mx == -1) {
        for (int i = 1; i <= n; i++) {
            if (del[i] == 0) {
                del[i] = 1;
                break;
            }
        }
    }
    else {
        del[index] = 1;
        for (int x : g[index]) {
            g[x].erase(index);
            g[index].erase(x);
        }
    }
    int ans = 0;
    vi flag(n + 1);
    queue<int> q;
    for (int i = 1; i <= n; i++) {
        if (del[i] == 0) {
            q.push(i);
            ans++;
            while (!q.empty()) {
                int x = q.front();
                q.pop();
                del[x] = 1;
                for (int y : g[x]) {
                    if (del[y] == 0) {
                        q.push(y);
                    }
                }
            }
        }
    }
    cout << ans << endl;
}

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
// helper();
// init();
fucking_go(solve);
//solve();

return 0;
}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define endl "\n"
#define all(a) a.begin(), a.end()
#define ll long long
#define ull unsigned long long
#define inf 0x7fffffff;
#define rep(i, l, r) for(int i = l; i <= r; ++i)
#define rrp(i, l, r) for(int i = l; i >= r; --i)
#define fucking_go(x) int t; cin >> t; while(t--) x()
using namespace std;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int> >;
using vvl = vector<vector<ll> >;
using vpii = vector<pair<int, int> >;
using vpll = vector<pair<ll, ll> >;
using pii = pair<int, int>;
const ll mod = 1e9 + 7;
//const ll mod = 998244353;
const ll INF = 1e18;

int n;
set<int> g[200010];
void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++) g[i].clear();
    for (int i = 1; i <= n - 1; i++) {
        int u, v;
        cin >> u >> v;
        g[u].insert(v);
        g[v].insert(u);
    }
    // if (n == 2) {
    //     cout << 0 << endl;
    //     return;
    // }
    vi del(n + 1);
    vi cnt(n + 1);
    for (int i = 1; i <= n; i++) {
        if (g[i].size() == 1) cnt[(*g[i].begin())]++;
    }
    int mx = -1, index = 0;
    for (int i = 1; i <= n; i++) {
        if (cnt[i] > mx) {
            mx = cnt[i];
            index = i;
        }
    }
    del[index] = 1;
    vi tmp;
    for (int x : g[index]) {
        tmp.push_back(x);
    }
    for (int x : tmp) {
        g[x].erase(index);
        g[index].erase(x);
        if (g[x].size() == 1) {
            cnt[(*g[x].begin())]++;
        }
    }
    mx = -1, index = 0;
    for (int i = 1; i <= n; i++) {
        if (del[i] == 0) {
            if (cnt[i] > mx) {
                mx = cnt[i];
                index = i;
            }
        }
    }
    if (mx == -1) {
        for (int i = 1; i <= n; i++) {
            if (del[i] == 0) {
                del[i] = 1;
                break;
            }
        }
    }
    else {
        del[index] = 1;
        tmp.clear();
        for (int x : g[index]) {
            tmp.push_back(x);
        }
        for (int x : tmp) {
            g[index].erase(x);
            g[x].erase(index);
        }
    }
    int ans = 0;
    vi flag(n + 1);
    queue<int> q;
    for (int i = 1; i <= n; i++) {
        if (del[i] == 0) {
            q.push(i);
            ans++;
            while (!q.empty()) {
                int x = q.front();
                q.pop();
                del[x] = 1;
                for (int y : g[x]) {
                    if (del[y] == 0) {
                        q.push(y);
                    }
                }
            }
        }
    }
    cout << ans << endl;
}

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
// helper();
// init();
fucking_go(solve);
//solve();

return 0;
}