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);
        }
    }
    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);
}

Submission (verdict: Accepted):
#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 solve(int tc)
{
    int n; cin >> n;

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

    vi sdeg = deg;
    sort(all(sdeg));

    int mans = -1;
    for (int i = 1; i <= n; i++) {
        int ans = deg[i];
        vi ideg = {deg[i]};
        for (int vtx : adj[i]) 
            ideg.pbk(deg[vtx]);
        sort(rall(ideg));

        vi rem;
        for (int dg : ideg) {
            if (dg == sdeg.back()) {
                sdeg.pop_back();
                rem.pbk(dg);
            }
        }
        reverse(all(rem));

        int mx = -1;
        if (sdeg.size())
            mx = sdeg.back();
        for (int vtx : adj[i])
            mx = max(mx, deg[vtx] - 1);
        for (int dg : rem)
            sdeg.pbk(dg);

        mans = max(mans, ans + mx - 1);
    }
    cout << mans << 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);
}