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

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ll long long
#define lld long double
#define el '\n'
#define ws " "
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define nai cout << "-1\n"
#define pbk push_back
#define mkp make_pair
#define ii pair<int, int>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define dbg(x) cout << #x << " " << x << "\n"
typedef vector<int> vi;
typedef vector<ll> vl;

template<class T>
void print(T& a)
{
    for(auto x : a)
        cout << x << " ";
    cout << "\n";
}

void dfs(int vtx, vector<vi>&adj, vi&visited) {
    visited[vtx] = 1;

    for (int nd : adj[vtx]) {
        if (!visited[nd])
            dfs(nd, adj, visited);
    }
}

void solve(int tc)
{
    int n; cin >> n;

    vector<vi> adj(n + 1); 
    vi visited(n + 1);
    rep(i, n - 1) {
        int u, v; cin >> u >> v;
        adj[u].pbk(v);
        adj[v].pbk(u);
    }

    vi node(2);
    for (int i = 0; i < 2; i++) {
        int mx = INT_MIN;
        for (int ver = 1; ver <= n; ver++) {
            if (mx < (int)adj[ver].size() && !visited[ver]) {
                node[i] = ver; 
                mx = adj[ver].size();
            }
        }
        
        visited[node[i]] = 1;
        for (int nd : adj[node[i]]) {
            adj[nd].erase(remove(all(adj[nd]), node[i]), adj[nd].end());
        }
    }

    int ans = 0;
    for (int i = 1; i <= n; i++) {
        if (!visited[i]) {
            ans++;
            dfs(i, adj, visited);
        }
    }
    cout << ans << el;
}

int32_t main(void)
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int t = 1;
    cin >> t;
    for (int i = 1; i <= t; i++)
        solve(i);
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ll long long
#define lld long double
#define el '\n'
#define ws " "
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define nai cout << "-1\n"
#define pbk push_back
#define mkp make_pair
#define ii pair<int, int>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define dbg(x) cout << #x << " " << x << "\n"
typedef vector<int> vi;
typedef vector<ll> vl;

template<class T>
void print(T& a)
{
    for(auto x : a)
        cout << x << " ";
    cout << "\n";
}

void dfs(int vtx, vector<vi>&adj, vi&visited) {
    visited[vtx] = 1;
    for (int nd : adj[vtx]) {
        if (!visited[nd])
            dfs(nd, adj, visited);
    }
}

void solve(int tc)
{
    int n; cin >> n;

    vector<vi> adj(n + 1); 
    vi visited(n + 1);
    rep(i, n - 1) {
        int u, v; cin >> u >> v;
        adj[u].pbk(v);
        adj[v].pbk(u);
    }

    vi node(2);
    for (int i = 0; i < 2; i++) {
        int mx = INT_MIN;
        for (int ver = 1; ver <= n; ver++) {
            if (mx < (int)adj[ver].size() && !visited[ver]) {
                node[i] = ver; 
                mx = adj[ver].size();
            }
        }
        
        visited[node[i]] = 1;
        for (int nd : adj[node[i]]) {
            adj[nd].erase(remove(all(adj[nd]), node[i]), adj[nd].end());
        }
    }

    int ans = 0;
    for (int i = 1; i <= n; i++) {
        if (!visited[i]) {
            ans++;
            dfs(i, adj, visited);
        }
    }
    if (n >= 5) ans = max(ans, 3);
    cout << ans << el;
}

int32_t main(void)
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int t = 1;
    cin >> t;
    for (int i = 1; i <= t; i++)
        solve(i);
}