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

Code 1:
//
// 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;
}


Code 2:
//
// 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);
        }

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

    return 0;
}


Answer: 1