Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 281 ms):
//
// Created by Jiwon Sheen on 2025-02-02
//
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

inline void fastIo() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

using pii = pair<int, int>;

int main() {
    int t;
    int n;
    int ans;
    vector<vector<int>> v;
    vector<int> de, de2;
    vector<bool> chk;
    priority_queue<pii> pq;

    fastIo();

    cin >> t;

    while (t--) {
        cin >> n;

        ans = 1;
        v.assign(n + 1, vector<int>());
        de.assign(n + 1, 0);
        de2.assign(n + 1, 0);
        chk.assign(n + 1, false);

        for (int i = 0; i < n - 1; i++) {
            int a, b;
            cin >> a >> b;
            de[a]++, de[b]++;
            de2[a]++, de2[b]++;
            v[a].push_back(b);
            v[b].push_back(a);
        }


        for (int i = 1; i <= n; i++)
            pq.emplace(de[i], i);

        for (int k = 0; k < 2; k++) {
            while (pq.top().first != de[pq.top().second])
                pq.pop();
            auto [cd, idx] = pq.top();
            pq.pop();
            chk[idx] = true;

            ans += cd - 1;
            for (const auto &nxt: v[idx]) {
                if (!chk[nxt])
                    pq.emplace(--de[nxt], nxt);
            }
        }

        while (!pq.empty())
            pq.pop();

        if (n >= 3) {
            int ans2 = 1;

            for (int i = 1; i <= n; i++)
                pq.emplace(de2[i], i);

            chk.assign(n + 1, false);
            chk[pq.top().second] = true;
            pq.pop();

            for (int k = 0; k < 2; k++) {
                while (pq.top().first != de2[pq.top().second])
                    pq.pop();
                auto [cd, idx] = pq.top();
                pq.pop();
                chk[idx] = true;

                ans2 += cd - 1;
                for (const auto &nxt: v[idx]) {
                    if (!chk[nxt])
                        pq.emplace(--de2[nxt], nxt);
                }
            }
            ans = max(ans, ans2);

            while (!pq.empty())
                pq.pop();
        }

        cout << ans << '\n';
    }

    return 0;
}


Solution 2 (runtime: 625 ms):
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

int main(){
    ll t;
    cin>>t;
    while(t--){
        ll n, x, y, ans = 0, maxNeighbors = -1, eleRemoved = 1;
        cin>>n;
        vector<vector<ll>> adj(n + 1);
        vector<ll> neighbors(n + 1, 0);
        for(ll i = 0 ; i < n - 1 ; i++){
            cin>>x>>y;
            adj[x].push_back(y);
            adj[y].push_back(x);
        }
        for(ll i = 1 ; i <= n ; i++) neighbors[i] = adj[i].size();
        for(ll k = 1 ; k <= 2 ; k++){
            maxNeighbors = -1, eleRemoved = 1;
            for(ll i = 1 ; i <= n ; i++){
                if(maxNeighbors < neighbors[i]){
                    maxNeighbors = neighbors[i];
                    eleRemoved = i; 
                }
                else if(maxNeighbors == neighbors[i]){
                    ll sum1 = 0, sum2 = 0;
                    for(auto it : adj[eleRemoved]) sum1 += neighbors[it];
                    for(auto it : adj[i]) sum2 += neighbors[it];
                    if(sum2 < sum1) eleRemoved = i;
                }
            }
            ans += maxNeighbors;
            for(auto it : adj[eleRemoved]) neighbors[it]--;
            neighbors[eleRemoved] = -1;
        }
        ans -= 1;
        cout<<ans<<endl;
    }
}