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

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    const int K = 200; // 取前200个顶点检查
    while (t--) {
        int n;
        cin >> n;
        if (n == 2) {
            cout << 0 << '\n';
            int u, v;
            cin >> u >> v;
            continue;
        }
        vector<unordered_set<int>> adj(n + 1);
        vector<int> degrees(n + 1, 0);
        vector<pair<int, int>> edges;
        for (int i = 0; i < n - 1; ++i) {
            int u, v;
            cin >> u >> v;
            adj[u].insert(v);
            adj[v].insert(u);
            degrees[u]++;
            degrees[v]++;
            edges.emplace_back(u, v);
        }
        int max_adj = 0;
        for (auto &e : edges) {
            int u = e.first, v = e.second;
            int current = degrees[u] + degrees[v] - 2;
            if (current > max_adj) {
                max_adj = current;
            }
        }
        vector<int> nodes(n);
        iota(nodes.begin(), nodes.end(), 1);
        sort(nodes.begin(), nodes.end(), [&](int a, int b) {
            return degrees[a] > degrees[b];
        });
        int m = min(n, K);
        int max_non_adj = 0;
        for (int i = 0; i < m; ++i) {
            int u = nodes[i];
            for (int j = i + 1; j < m; ++j) {
                int v = nodes[j];
                if (!adj[u].count(v)) {
                    int current = degrees[u] + degrees[v] - 1;
                    if (current > max_non_adj) {
                        max_non_adj = current;
                    }
                }
            }
        }
        cout << max(max_adj, max_non_adj) << '\n';
    }
    return 0;
}

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