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

Previous 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;
}

Submission (verdict: Accepted):
#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;

        // 只需处理前两个最大度数顶点，覆盖所有可能的最优解
        vector<int> candidates;
        if (!max_nodes.empty()) {
            candidates.push_back(max_nodes[0]);
            if (max_nodes.size() >= 2) {
                candidates.push_back(max_nodes[1]);
            }
        }

        // 遍历候选顶点与其他顶点的组合
        for (int u : candidates) {
            for (int v = 1; v <= n; ++v) {
                if (u == v) 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 (candidates.size() >= 2) {
            int u = candidates[0], v = candidates[1];
            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);
        }

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

    return 0;
}