Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Runtime error on test 2):
using namespace std;
#include <bits/stdc++.h>

#define all(A) A.begin(),A.end()
#define fori(i, k, n) for (int i = k; i < n; i++)
#define ln "\n"


vector<vector<int> > adj;
vector<int> deg;
vector<bool> visited;
int n, U, V;

void removeVertex(int u) {
    deg[u] = 0;
    for (const auto &v: adj[u]) {
        adj[v].erase(remove(all(adj[v]), u), adj[v].end());
        deg[v]--;
    }
    adj.erase(adj.begin() + u);
}

void dfs(int u) {
    visited[u] = true;
    for (const auto &v: adj[u])
        if (!visited[v])
            dfs(v);
}

void printGraph() {
    fori(u, 0, n) {
        cout << (u + 1) << "->";
        for (const auto &v: adj[u])
            cout << (v + 1) << ", ";
        cout << ln;
    }
}


void solve() {
    cin >> n;
    adj.assign(n, vector<int>());
    deg.assign(n, 0);

    fori(i, 0, n-1) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
        deg[a]++;
        deg[b]++;
    }
    set<pair<int, int> > tree;
    fori(u, 0, n)
        tree.insert({deg[u], u});
    auto first = prev(tree.end());
    tree.erase(first);
    U = first->second;
    removeVertex(U);
    tree.clear();
    fori(u, 0, n) {
        if (u == U)
            continue;
        tree.insert({deg[u], u});
    }
    V = prev(tree.end())->second;
    removeVertex(V);
    visited.assign(n, false);
 //   cout << "U: " << U + 1 << " V: " << V + 1 << ln;
    int ans = 0;
    fori(u, 0, n) {
        if (u != U && u != V && !visited[u]) {
            //cout << "u: " << u + 1 << ln;
            dfs(u);
            ans++;
        }
    }
    cout << ans << ln;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}


Submission (verdict: Wrong answer on test 4):
using namespace std;
#include <bits/stdc++.h>

#define all(A) A.begin(),A.end()
#define fori(i, k, n) for (int i = k; i < n; i++)
#define ln "\n"


vector<vector<int> > adj;
vector<int> deg;
vector<bool> visited;
int n, U, V;

void removeVertex(int u) {
    deg[u] = 0;
    for (const auto &v: adj[u]) {
        adj[v].erase(remove(all(adj[v]), u), adj[v].end());
        deg[v]--;
    }
    adj[u].clear();
}

void dfs(int u) {
    visited[u] = true;
    for (const auto &v: adj[u])
        if (!visited[v])
            dfs(v);
}

void printGraph() {
    fori(u, 0, n) {
        cout << (u + 1) << "->";
        for (const auto &v: adj[u])
            cout << (v + 1) << ", ";
        cout << ln;
    }
}


void solve() {
    cin >> n;
    adj.assign(n, vector<int>());
    deg.assign(n, 0);
    fori(i, 0, n-1) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
        deg[a]++;
        deg[b]++;
    }
    set<pair<int, int> > tree;
    fori(u, 0, n)
        tree.insert({deg[u], u});
    auto first = prev(tree.end());
    tree.erase(first);
    U = first->second;
    removeVertex(U);
    tree.clear();
    fori(u, 0, n) {
        if (u == U)
            continue;
        tree.insert({deg[u], u});
    }
    V = prev(tree.end())->second;
    removeVertex(V);
    visited.assign(n, false);
    //   cout << "U: " << U + 1 << " V: " << V + 1 << ln;
    int ans = 0;
    fori(u, 0, n) {
        if (u != U && u != V && !visited[u]) {
            //cout << "u: " << u + 1 << ln;
            dfs(u);
            ans++;
        }
    }
    cout << ans << ln;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}