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

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
// using namespace __gnu_pbds;
 
#define int long long
#define mod 1000000007
#define base 7001
#define base2 757
#define F first
#define S second
// #define pi acos(-1)
#define double long double
// #define ordered_set tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update>
// #define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>

// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,sse3,sse4,avx")

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
constexpr int maxn = 1000001;
const int N = 1 << (int)(ceil(log2(maxn)));

int n;
vector<int> adj[maxn];

signed main()
{
    cin.tie(0) -> sync_with_stdio(0);
    int t; cin >> t;
    while (t--) {
        cin >> n;
        for (int i = 1; i < n; i++) {
            int u, v; cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }

        int ans = 0, idx = 0;
        for (int i = 1; i <= n; i++) {
            if (adj[i].size() >= ans) {
                ans = adj[i].size();
                idx = i;
            }
        }
        for (int v : adj[idx]) adj[v].pop_back();

        int mx = 0;
        for (int i = 1; i <= n; i++) {
            if (i == idx) continue;
            int temp = adj[i].size();
            mx = max(mx, temp);
        }

        mx--;
        // if (ans == 1) ans = 0;

        cout << ans + mx << '\n';

        for (int i = 1; i <= n; i++) adj[i].clear();
    }
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
// using namespace __gnu_pbds;
 
#define int long long
#define mod 1000000007
#define base 7001
#define base2 757
#define F first
#define S second
// #define pi acos(-1)
#define double long double
// #define ordered_set tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update>
// #define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>

// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,sse3,sse4,avx")

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
constexpr int maxn = 1000001;
const int N = 1 << (int)(ceil(log2(maxn)));

int n;
vector<int> adj[maxn];

signed main()
{
    cin.tie(0) -> sync_with_stdio(0);
    int t; cin >> t;
    while (t--) {
        cin >> n;
        for (int i = 1; i < n; i++) {
            int u, v; cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }

        set<pair<int, int>> s;
        for (int i = 1; i <= n; i++) s.insert({adj[i].size(), i});

        int ans = 0;
        for (int i = 1; i <= n; i++) {
            s.erase({adj[i].size(), i});
            for (auto v : adj[i]) {
                s.erase({adj[v].size(), v});
                s.insert({adj[v].size() - 1, v});
            }

            int temp = adj[i].size() + (*s.rbegin()).F - 1;
            // cout << temp << " ";
            ans = max(ans, temp);

            for (auto v : adj[i]) {
                s.erase({adj[v].size() - 1, v});
                s.insert({adj[v].size(), v});
            }
            s.insert({adj[i].size(), i});
        }

        cout << ans << "\n";

        for (int i = 1; i <= n; i++) adj[i].clear();
    }
}