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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

void solve() {
    int n;
    cin >> n;
    vector<int> cnt(n, 0);
    map<int, set<int>> mp;
    int mx = 0;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        cnt[u]++;
        cnt[v]++;
        mp[u].insert(v);
        mp[v].insert(u);
        mx = max(cnt[u], mx);
        mx = max(cnt[v], mx);
    }
    int cnt_mx = 0;
    for (int i = 0; i < n; i++) {
        if (cnt[i] == mx) {
            cnt_mx++;
        }
    }
    if (cnt_mx == 1) {
        int u = -1, mx = 0;
        for (int i = 0; i < n; i++) {
            if (cnt[i] >= mx) {
                mx = cnt[i];
                u = i;
            }
        }
        if (u == -1) {
            cout << "0\n";
            return;
        }
        int ans = 1;
        ans += mx - 1;
        for (int v : mp[u]) {
            cnt[v]--;
        }
        cnt[u] = -1;
        u = -1, mx = 0;
        for (int i = 0; i < n; i++) {
            if (cnt[i] >= mx) {
                mx = cnt[i];
                u = i;
            }
        }
        if (u == -1) {
            cout << "0\n";
            return;
        }
        ans += mx - 1;
        cout << ans << '\n';
    } else if (cnt_mx == 2) {
        int u = -1, v = -1;
        for (int i = 0; i < n; i++) {
            if (cnt[i] == mx) {
                if (u == -1) {
                    u = i;
                } else if (v == -1) {
                    v = i;
                } else {
                    break;
                }
            }
        }
        int ans = 1;
        if (mp[u].find(v) != mp[u].end()) {
            ans += mx - 1 + mx - 1 - 1;
        } else {
            ans += mx - 1 + mx - 1;
        }
        cout << ans << '\n';
    } else {
        cout << 2 * (mx - 1) << '\n';
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t; 
    cin >> t; 
    while (t--) {
        solve();
    }

    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

void solve() {
    int n;
    cin >> n;
    vector<int> cnt(n, 0);
    map<int, set<int>> mp;
    int mx = 0;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        cnt[u]++;
        cnt[v]++;
        mp[u].insert(v);
        mp[v].insert(u);
        mx = max(cnt[u], mx);
        mx = max(cnt[v], mx);
    }
    int cnt_mx = 0;
    for (int i = 0; i < n; i++) {
        if (cnt[i] == mx) {
            cnt_mx++;
        }
    }
    if (cnt_mx == 1) {
        int u = -1, mx = 0;
        for (int i = 0; i < n; i++) {
            if (cnt[i] >= mx) {
                mx = cnt[i];
                u = i;
            }
        }
        if (u == -1) {
            cout << "0\n";
            return;
        }
        int ans = 1;
        ans += mx - 1;
        for (int v : mp[u]) {
            cnt[v]--;
        }
        cnt[u] = -1;
        u = -1, mx = 0;
        for (int i = 0; i < n; i++) {
            if (cnt[i] >= mx) {
                mx = cnt[i];
                u = i;
            }
        }
        if (u == -1) {
            cout << "0\n";
            return;
        }
        ans += mx - 1;
        cout << ans << '\n';
    } else if (cnt_mx == 2) {
        int u = -1, v = -1;
        for (int i = 0; i < n; i++) {
            if (cnt[i] == mx) {
                if (u == -1) {
                    u = i;
                } else if (v == -1) {
                    v = i;
                } else {
                    break;
                }
            }
        }
        int ans = 1;
        if (mp[u].find(v) != mp[u].end()) {
            ans += mx - 1 + mx - 1 - 1;
        } else {
            ans += mx - 1 + mx - 1;
        }
        cout << ans << '\n';
    } else {
        int ans = 1;
        ans += mx - 1 + mx - 1;
        cout << ans << '\n';
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t; 
    cin >> t; 
    while (t--) {
        solve();
    }

    return 0;
}