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();
    vi cnt(n + 1);
    for (int i = 1; i <= n - 1; i++) {
        int u, v;
        cin >> u >> v;
        g[u].insert(v);
        g[v].insert(u);
        cnt[u]++;
        cnt[v]++;
    }
    // if (n == 2) {
    //     cout << 0 << endl;
    //     return;
    // }
    vi del(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;
}

Submission (verdict: Accepted):
#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];
int a[200010];
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++) g[i].clear(), a[i] = 0;
    for (int i = 1; i <= n - 1; i++)
    {
        int u, v;
        cin >> u >> v;
        g[u].insert(v);
        g[v].insert(u);
        a[u]++;
        a[v]++;
    }
    multiset<int> st;
    for (int i = 1; i <= n; i++)
    {
        st.insert(a[i]);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        int now = 0;
        st.erase(st.find(a[i]));
        for (int v : g[i])
        {
            st.erase(st.find(a[v]));
            st.insert(a[v] - 1);
        }
        now += a[i];
        now += (*st.rbegin()) - 1;
        for (int v : g[i])
        {
            st.erase(st.find(a[v] - 1));
            st.insert(a[v]);
        }
        st.insert(a[i]);
        ans = max(ans, now);
    }
    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;
}