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

Previous submission (verdict: Time limit exceeded on test 9):
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<set<int>> adj(n + 1);
        vector<int> degree(n + 1, 0);

        for (int i = 0; i < n - 1; ++i) {
            int u, v;
            cin >> u >> v;
            adj[u].insert(v);
            adj[v].insert(u);
            degree[u]++;
            degree[v]++;
        }

        int max_deg = *max_element(degree.begin() + 1, degree.end());
        vector<int> max_nodes;
        for (int i = 1; i <= n; ++i) {
            if (degree[i] == max_deg) {
                max_nodes.push_back(i);
            }
        }

        int max_components = 0;
        for (int u : max_nodes) {
            for (int j = 1; j <= n; ++j) {
                if (j == u) continue;
                bool adjacent = adj[u].count(j);
                int current = degree[u] + degree[j] - 1 - (adjacent ? 1 : 0);
                max_components = max(max_components, current);
            }
        }

        for (int i = 0; i < max_nodes.size(); ++i) {
            for (int j = i + 1; j < max_nodes.size(); ++j) {
                int u = max_nodes[i], v = max_nodes[j];
                bool adjacent = adj[u].count(v);
                int current = degree[u] + degree[v] - 1 - (adjacent ? 1 : 0);
                max_components = max(max_components, current);
            }
        }

        cout << max_components << '\n';
    }

    return 0;
}

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<vector<int>> adj(n + 1);  // 邻接表（排序后的）
        vector<int> degree(n + 1, 0);

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

        // 对每个邻接表排序，以便后续使用二分查找
        for (int i = 1; i <= n; ++i) {
            sort(adj[i].begin(), adj[i].end());
        }

        int max_deg = *max_element(degree.begin() + 1, degree.end());
        vector<int> max_nodes;
        for (int i = 1; i <= n; ++i) {
            if (degree[i] == max_deg) {
                max_nodes.push_back(i);
            }
        }

        int max_components = 0;

        // 只需处理度数最大的顶点中的一个（第一个）
        if (!max_nodes.empty()) {
            int u = max_nodes[0];
            for (int j = 1; j <= n; ++j) {
                if (j == u) continue;
                bool adjacent = binary_search(adj[u].begin(), adj[u].end(), j);
                int current = degree[u] + degree[j] - 1 - (adjacent ? 1 : 0);
                max_components = max(max_components, current);
            }

            // 检查与其他最大度数顶点的组合
            for (int v : max_nodes) {
                if (v == u) continue;
                bool adjacent = binary_search(adj[u].begin(), adj[u].end(), v);
                int current = degree[u] + degree[v] - 1 - (adjacent ? 1 : 0);
                max_components = max(max_components, current);
            }
        }

        // 考虑次大的度数顶点（若有）
        if (max_deg > 1) {
            int second_max_deg = 0;
            for (int i = 1; i <= n; ++i) {
                if (degree[i] < max_deg && degree[i] > second_max_deg) {
                    second_max_deg = degree[i];
                }
            }
            if (second_max_deg > 0) {
                for (int i = 1; i <= n; ++i) {
                    if (degree[i] == second_max_deg) {
                        bool adjacent = binary_search(adj[i].begin(), adj[i].end(), max_nodes[0]);
                        int current = max_deg + second_max_deg - 1 - (adjacent ? 1 : 0);
                        max_components = max(max_components, current);
                    }
                }
            }
        }

        cout << max_components << '\n';
    }

    return 0;
}