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

Previous submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>

using namespace std;

static inline void boost_io() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

typedef long long int64;

class Solution {
    int n;

    vector<vector<int>> graph;
    set<pair<int, int>> edges;
    

public:
    Solution(int n) : n(n) {
        graph.resize(n + 1);
    }

    void addEdge(int u, int v) {
        graph[u].push_back(v);
        graph[v].push_back(u);

        edges.insert({u, v});
        edges.insert({v, u});
    }

    int solve() {
        int ans = 0;
        priority_queue<pair<int, int>> pq;

        for (int u = 2; u <= n; ++u) {
            pq.push({graph[u].size(), u});
        }

        for (int i = 0; i < 5 && pq.size() > 0; ++i) {
            auto [_, v] = pq.top();

            for (int u = 1; u <= n; ++u) {
                if (u == v) {
                    continue;
                }

                if (edges.count({u, v})) {
                    ans = max(ans, (int)(graph[u].size() + graph[v].size() - 2));
                } else {
                    ans = max(ans, (int)(graph[u].size() + graph[v].size() - 1));
                }
            }
        }

        return min(ans, n - 2);
    }
};

int main() {
    boost_io();

    int T;
    cin >> T;

    while (T--) {
        int n;
        cin >> n;

        Solution s(n);

        for (int i = 2; i <= n; ++i) {
            int u, v;
            cin >> u >> v;
            s.addEdge(u, v);
        }

        cout << s.solve() << endl;
    }

    return 0;
}

Submission (verdict: Accepted):
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>

using namespace std;

static inline void boost_io() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

typedef long long int64;

class Solution {
    int n;

    vector<vector<int>> graph;
    set<pair<int, int>> edges;
    

public:
    Solution(int n) : n(n) {
        graph.resize(n + 1);
    }

    void addEdge(int u, int v) {
        graph[u].push_back(v);
        graph[v].push_back(u);

        edges.insert({u, v});
        edges.insert({v, u});
    }

    int solve() {
        int ans = 0;
        priority_queue<pair<int, int>> pq;

        for (int u = 2; u <= n; ++u) {
            pq.push({graph[u].size(), u});
        }

        for (int i = 0; i < 5 && pq.size() > 0; ++i) {
            auto [_, v] = pq.top();

            pq.pop();

            for (int u = 1; u <= n; ++u) {
                if (u == v) {
                    continue;
                }

                if (edges.count({u, v})) {
                    ans = max(ans, (int)(graph[u].size() + graph[v].size() - 2));
                } else {
                    ans = max(ans, (int)(graph[u].size() + graph[v].size() - 1));
                }
            }
        }

        return min(ans, n - 2);
    }
};

int main() {
    boost_io();

    int T;
    cin >> T;

    while (T--) {
        int n;
        cin >> n;

        Solution s(n);

        for (int i = 2; i <= n; ++i) {
            int u, v;
            cin >> u >> v;
            s.addEdge(u, v);
        }

        cout << s.solve() << endl;
    }

    return 0;
}