Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <vector>
#include <set>
#include <climits>
#include <algorithm>

using namespace std;
using ll = long long;

ll INF = LLONG_MAX;

void solve() {
    ll n;
    cin >> n;
    vector<set<ll>> g(n);
    for (ll i = 0; i < n - 1; i++) {
        ll v, u;
        cin >> v >> u;
        v--;
        u--;
        g[v].insert(u);
        g[u].insert(v);
    }
    vector<ll> M1;
    ll M = 0;
    for (ll i = 0; i < n; i++) {
        if (g[i].size() == M) {
            M1.push_back(i);
        }
        if (g[i].size() > M) {
            M = g[i].size();
            M1 = {i};
        }
    }
    if (M1.size() > 1) {
        if (g[M1[0]].size() == 1) {
            if (n == 2) {
                cout << "0\n";
            }
            else {
                cout << "1\n";
            }
            return;
        }
        if (M1.size() == 2 && g[M1[0]].find(M1[1]) != g[M1[0]].end()) {
            cout << M + M - 1 << "\n";
        }
        else {
            cout << M * 2 << "\n";
        }
        return;
    }
    vector<ll> M2;
    ll MM = 0;
    for (ll i = 0; i < n; i++) {
        if (g[i].size() == MM) {
            M2.push_back(i);
        }
        if (g[i].size() > MM && g[i].size() < M) {
            MM = g[i].size();
            M2 = {i};
        }
    }
    for (auto i : M2) {
        if (g[i].find(M1[0]) == g[i].end()) {
            cout << M + MM - 1 << "\n";
            return;
        }
    }
    cout << M + MM - 2 << "\n";
}

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


Submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <vector>
#include <set>
#include <climits>
#include <algorithm>

using namespace std;
using ll = long long;

ll INF = LLONG_MAX;

void solve() {
    ll n;
    cin >> n;
    vector<set<ll>> g(n);
    for (ll i = 0; i < n - 1; i++) {
        ll v, u;
        cin >> v >> u;
        v--;
        u--;
        g[v].insert(u);
        g[u].insert(v);
    }
    vector<ll> M1;
    ll M = 0;
    for (ll i = 0; i < n; i++) {
        if (g[i].size() == M) {
            M1.push_back(i);
        }
        if (g[i].size() > M) {
            M = g[i].size();
            M1 = {i};
        }
    }
    if (M1.size() > 1) {
        if (g[M1[0]].size() == 1) {
            if (n == 2) {
                cout << "0\n";
            }
            else {
                cout << "1\n";
            }
            return;
        }
        if (M1.size() == 2 && g[M1[0]].find(M1[1]) != g[M1[0]].end()) {
            cout << M + M - 2 << "\n";
        }
        else {
            cout << M * 2 << "\n";
        }
        return;
    }
    vector<ll> M2;
    ll MM = 0;
    for (ll i = 0; i < n; i++) {
        if (g[i].size() == MM) {
            M2.push_back(i);
        }
        if (g[i].size() > MM && g[i].size() < M) {
            MM = g[i].size();
            M2 = {i};
        }
    }
    for (auto i : M2) {
        if (g[i].find(M1[0]) == g[i].end()) {
            cout << M + MM - 1 << "\n";
            return;
        }
    }
    cout << M + MM - 2 << "\n";
}

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