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;
#define int long long
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define rep(i, x, n) for (int i = x; i < n; i++)
#define sortv(v) sort(v.begin(), v.end())
#define sortrv(v) sort(v.rbegin(), v.rend())
#define MOD 1000000000
#define pb push_back

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

void solve() {
    int n; cin >> n;
    vector<pair<int, int>> edges(n - 1);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        edges[i] = make_pair(u, v);
    }
    
    vector<vector<int>> adj(n);
    for (auto& edge : edges) {
        adj[edge.first].push_back(edge.second);
        adj[edge.second].push_back(edge.first);
    }
    
    vector<int> deg(n, 0);
    rep(i,0,n) {
        deg[i] = adj[i].size();
    }
    
    vector<int> node;
    rep(i,0,n) {
        node.push_back(i);
    }
    
    sort(node.begin(), node.end(), [&](int a, int b) { return deg[a] > deg[b]; });
    auto cc = [&](int v1, int v2) {
        vector<bool> rem(n, false);
        rem[v1] = rem[v2] = true;
        int curr = 0;
        vector<bool> vis(n, false);
        function<void(int)> dfs1 = [&](int v) {
            vis[v] = true;
            for (int u : adj[v]) {
                if (!vis[u] && !rem[u]) {
                    dfs1(u);
                }
            }
        };
        
        rep(i,0,n) {
            if (!vis[i] && !rem[i]) {
                dfs1(i);
                curr++;
            }
        }
        
        return curr;
    };
    int res = 0;
    int K = min(10*1LL, n);
    rep(i,0,K) {
        rep(j,i+1,K) {
            res = max(res, cc(node[i], node[j]));
        }
    }
    
    cout << res << endl;
}
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // Uncomment the next line for file input/output
    // setIO("shell");
    int tc = 1;
    cin >> tc;
    while(tc--) {
        solve();
    }
    return 0;
}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define rep(i, x, n) for (int i = x; i < n; i++)
#define sortv(v) sort(v.begin(), v.end())
#define sortrv(v) sort(v.rbegin(), v.rend())
#define MOD 1000000000
#define pb push_back

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}
void solve() {
    int n; cin >> n; 
    vector<vector<int>> adj(n); 
    vector<int> deg(n); 
    set<pair<int,int>> edges; 
    for(int i = 0; i < n-1; i++) {
        int u , v; cin >> u >> v; 
        u--; v--; 
        deg[u]++; deg[v]++; 
        edges.insert({u,v}); 
    }
    map<int,int> freq; 
    for(auto x : deg) freq[x]++; 
    vi cc = deg; 
    vector<pair<int,int>> mm; 
    for(int i = 0; i < n; i++) {
        mm.pb({deg[i],i}); 
    }
    sortv(mm); 
    sortv(deg); 
    function<bool(int, int)> isadj = [&](int u, int v) {
        if(edges.count({u,v}) || edges.count({v,u})) return true; 
        else return false; 
    };
    if(freq[deg.back()] >= 3) {
        cout << (deg.back() * 2) - 1 << endl; 
        //cout << "c1" << endl;
    }
    else {
        if(freq[deg.back()] == 2) {
            int v1 = -1; 
            int v2 = -1; 
            for(int i = 0; i < n; i++) {
                if(cc[i] == deg.back()) {
                    v1 = i; 
                }
            }
            for(int i = 0; i < n; i++) {
                if(i != v1 && cc[i] == deg.back()) {
                    v2 = i; 
                }
            }
            if(isadj(v1,v2)) {
                cout << (2*deg.back()) - 2 << endl; 
                //cout << v1 << ' ' << v2 << endl;
                //cout << "c2a" << endl;
            }
            else {
                cout << (2*deg.back()) - 1 << endl; 
                cout << v1 << ' ' << v2 << endl;
              //  cout << "c2b" << endl;
            }
        }
        else {
            for(int i = 0; i < n; i++) {
                if(mm[i].first == deg[deg.size()-2]) {
                    if(!isadj(mm[i].second, mm[n-1].second)) {
                        cout << 1 + (deg.back() - 1) + (deg[deg.size()-2] - 1) << endl;
                        return;
                    }
                }
            }
            cout << 1 + (deg.back() - 1) + (deg[deg.size()-2] - 2) << endl;
            //cout << "c3" << endl;
        }
    }
    /*
    removing a node with deg k 
    increasing connected components by k - 1 
    if we remove two non adjacent nodes 
    we get k - 1 + k - 1 + 1 == 2k - 1 connected comp
    if we remove two adjacent node we get (k - 1) + (k - 2) + 1 
    2k - 2 
    non adjacent > adjacent 
    greater adjacent == greater less than non adjcaent

    */
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // Uncomment the next line for file input/output
    // setIO("shell");
    int tc = 1;
    cin >> tc;
    while(tc--) {
        solve();
    }
    return 0;
}