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


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