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

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

using namespace std;
#define int long long
#define ull unsigned long long int
#define dl double long
#define all(x) x.begin(), x.end()
#define ss second
#define ff first
const int N = 2 * 1e5 + 10;
const int M = 1e9 + 7;
#define Priyanshu_Mishra

Priyanshu_Mishra

void dfs(int node, int par, vector < int > graph[], vector < int > & vis) {
    vis[node] = true;
    for (auto it: graph[node]) {
        if (it != par) {
            dfs(it, node, graph, vis);
        }
    }
}

void solve()
{
    int n;
    cin >> n;
    int x = -1, y = -1;
    vector < set < int > > edge(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        edge[u].insert(v);
        edge[v].insert(u);
    }
    if (n == 2) {
        cout << 0 << endl;
        return;
    }
    vector < pair < int, set < int >>> vp(n);
    for (int i = 0; i < n; i++) {
        vp[i] = {
            i,
            edge[i]
        };
    }
    sort(all(vp), [](const pair < int, set < int >> & a,
        const pair < int, set < int >> & b) {
        return a.ss.size() > b.ss.size();
    });
    x = vp[0].ff;
    for (int it: vp[0].ss) {
        vp[it].ss.erase(vp[0].ff);
    }
    vp.erase(vp.begin());


    sort(all(vp), [](const pair < int, set < int >> & a,
        const pair < int, set < int >> & b) {
        return a.ss.size() > b.ss.size();
    });
    y = vp[0].ff;
    for (auto it: vp[0].ss) {
        vp[it].ss.erase(vp[0].ff);
    }
    vp.erase(vp.begin());

    vector < int > graph[n];
    for (int i = 0; i < vp.size(); i++) {
        int u = vp[i].ff;
        for (auto v: vp[i].ss) {
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
    }
    int ans = 0;
    vector < int > vis(n, false);
    for (int i = 0; i < n; i++) {
        if (i != x && i != y) {
            if (!vis[i]) {
                ans++;
                dfs(i, -1, graph, vis);
            }
        }
    }
    cout << ans << endl;


}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int testcases = 1;
    cin >> testcases;
    while (testcases--)
    {
        solve();
        #ifdef Priyanshu_Mishra
        cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms\n";
        #endif
    }
    return 0;
}

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

using namespace std;
#define int long long
#define ull unsigned long long int
#define dl double long
#define all(x) x.begin(), x.end()
#define ss second
#define ff first
const int N = 2 * 1e5 + 10;
const int M = 1e9 + 7;
#define Priyanshu_Mishra

Priyanshu_Mishra

void dfs(int node, int par, vector < int > graph[], vector < int > & vis) {
    vis[node] = true;
    for (auto it: graph[node]) {
        if (it != par) {
            dfs(it, node, graph, vis);
        }
    }
}

void solve()
{
    int n;
    cin >> n;
    int x = -1, y = -1;
    vector < set < int > > edge(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        edge[u].insert(v);
        edge[v].insert(u);
    }
    if (n == 2) {
        cout << 0 << endl;
        return;
    }
    int one = 0, two = 0;
    for (int i = 0; i < n; i++) {
        if (edge[i].size() == 1) one++;
        if (edge[i].size() == 2) two++;
    }
    if (one == 2 and two == n - 2) {
        cout << 3 << endl;
        return;
    }
    vector < pair < int, set < int >>> vp(n);
    for (int i = 0; i < n; i++) {
        vp[i] = {
            i,
            edge[i]
        };
    }
    sort(all(vp), [](const pair < int, set < int >> & a,
        const pair < int, set < int >> & b) {
        return a.ss.size() > b.ss.size();
    });
    x = vp[0].ff;
    for (int it: vp[0].ss) {
        vp[it].ss.erase(vp[0].ff);
    }
    vp.erase(vp.begin());


    sort(all(vp), [](const pair < int, set < int >> & a,
        const pair < int, set < int >> & b) {
        return a.ss.size() > b.ss.size();
    });
    y = vp[0].ff;
    for (auto it: vp[0].ss) {
        vp[it].ss.erase(vp[0].ff);
    }
    vp.erase(vp.begin());

    vector < int > graph[n];
    for (int i = 0; i < vp.size(); i++) {
        int u = vp[i].ff;
        for (auto v: vp[i].ss) {
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
    }
    int ans = 0;
    vector < int > vis(n, false);
    for (int i = 0; i < n; i++) {
        if (i != x && i != y) {
            if (!vis[i]) {
                ans++;
                dfs(i, -1, graph, vis);
            }
        }
    }
    cout << ans << endl;


}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int testcases = 1;
    cin >> testcases;
    while (testcases--)
    {
        solve();
        #ifdef Priyanshu_Mishra
        cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms\n";
        #endif
    }
    return 0;
}