Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

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

#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define setbits(x) __builtin_popcountll(x)
template <typename T1, typename T2> auto ceil(T1 a, T2 b) {
    return (static_cast<long long>(a) + static_cast<long long>(b) - 1) / b;
}

const int mod = 1e9 + 7;
const int inf = 2e9 + 5;
const long long infl = 2e18 + 5;
double PI = 3.14159265358979323846;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define db(...) 20
#endif

void solve() {
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    vector<int> farFromLeaf(n, inf);
    for (int i = 0; i < n; i++) {
        if (g[i].size() == 1) {
            farFromLeaf[i] = 0;
        }
    }

    vector<int> visited(n, 0);
    function<void(int, int)> dfs_dist_leaf = [&](int u, int p) {
        visited[u] = true;
        for (int v : g[u]) {
            if (v == p)
                continue;
            dfs_dist_leaf(v, u);
            farFromLeaf[u] = min(farFromLeaf[u], 1 + farFromLeaf[v]);
        }
    };

    dfs_dist_leaf(0, -1);
    db(farFromLeaf);

    vector<int> order(n);
    iota(all(order), 0);
    sort(all(order), [&](int i, int j) {
        if (g[i].size() != g[j].size())
            return g[i].size() > g[j].size();
        return farFromLeaf[i] < farFromLeaf[j];
    });

    db(order);
    int toRemove = order[0];
    int firstRemove = toRemove;
    g[toRemove].clear();
    for (int i = 0; i < n; i++) {
        g[i].erase(remove(all(g[i]), toRemove), g[i].end());
    }

    order.erase(find(all(order), firstRemove));
    visited.assign(n, 0);
    farFromLeaf.assign(n, inf);
    for (int i = 0; i < n; i++) {
        if (i == firstRemove)
            continue;
        if (g[i].size() == 1) {
            farFromLeaf[i] = 0;
        }
    }

    sort(all(order), [&](int i, int j) {
        if (g[i].size() != g[j].size())
            return g[i].size() > g[j].size();
        return farFromLeaf[i] < farFromLeaf[j];
    });

    toRemove = order[0];
    int secondRemove = toRemove;
    g[toRemove].clear();
    for (int i = 0; i < n; i++) {
        g[i].erase(remove(all(g[i]), toRemove), g[i].end());
    }

    order.erase(find(all(order), secondRemove));
    db(order);

    visited.assign(n, 0);
    function<void(int)> dfs = [&](int u) {
        visited[u] = true;
        for (int v : g[u]) {
            if (!visited[v]) {
                dfs(v);
            }
        }
    };

    db(firstRemove, secondRemove);

    int connectedComponents = 0;
    for (int i = 0; i < n; i++) {
        if (i == firstRemove || i == secondRemove)
            continue;
        if (!visited[i]) {
            dfs(i);
            connectedComponents++;
        }
    }
    cout << connectedComponents << endl;
}

int32_t main() {
    fastio;
    int testCases = 1;
    cin >> testCases;
    for (int i = 1; i <= testCases; i++) {
        solve();
    }
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define setbits(x) __builtin_popcountll(x)
template <typename T1, typename T2> auto ceil(T1 a, T2 b) {
    return (static_cast<long long>(a) + static_cast<long long>(b) - 1) / b;
}

const int mod = 1e9 + 7;
const int inf = 2e9 + 5;
const long long infl = 2e18 + 5;
double PI = 3.14159265358979323846;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define db(...) 20
#endif

void solve() {
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    set<pair<int, int>> st;
    for (int i = 0; i < n; i++) {
        st.insert({g[i].size(), i});
    }

    int ans = 0;

    for (int firstNode = 0; firstNode < n; firstNode++) {
        pair<int, int> cur = {g[firstNode].size(), firstNode};
        st.erase(cur);
        for (int adj : g[firstNode]) {
            pair<int, int> child = {g[adj].size(), adj};
            st.erase(child);
            st.insert({child.first - 1, child.second});
        }
        pair<int, int> best = *st.rbegin();
        ans = max(ans, cur.first + best.first - 1);
        for (int adj : g[firstNode]) {
            pair<int, int> child = {g[adj].size(), adj};
            st.erase({child.first - 1, child.second});
            st.insert(child);
        }

        st.insert(cur);
    }

    cout << ans << endl;
}

int32_t main() {
    fastio;
    int testCases = 1;
    cin >> testCases;
    for (int i = 1; i <= testCases; i++) {
        solve();
    }
}