Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n;
    cin >> n;

    vector<vector<int>> adj(n);
    vector<int> degree(n, 0);

    // Read input edges
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        --u, --v; // Convert to 0-based index
        adj[u].push_back(v);
        adj[v].push_back(u);
        degree[u]++;
        degree[v]++;
    }

    // Step 1: Find the node with max degree
    int first = max_element(degree.begin(), degree.end()) - degree.begin();
    int first_removed_components = degree[first];

    // Step 2: Remove the first node
    for (int neighbor : adj[first]) {
        degree[neighbor]--;  // Reduce degree of connected nodes
    }
    degree[first] = 0;  // Mark as removed

    // Step 3: Find the next node with max degree
    int second = max_element(degree.begin(), degree.end()) - degree.begin();
    int second_removed_components = degree[second];

    // Maximum number of components = components after first + components after second
    cout << (first_removed_components + second_removed_components - 1) << '\n';
}

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

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}



     
    
    
   

Submission (verdict: Accepted):
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
 
    int t;
    cin >> t;
 
    while(t--){
        int n;
        cin >> n;
 
 
        vector<vector<int>> adj(n+1, vector<int>());
        vector<int> deg(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);
            deg[u]++;
            deg[v]++;
        }
 
 
        vector<int> nodes(n);
        for(int i = 0; i < n; ++i){
            nodes[i] = i + 1;
        }
 
 
        sort(nodes.begin(), nodes.end(), [&](const int a, const int b) -> bool{
            if(deg[a] != deg[b]) return deg[a] > deg[b];
            return a < b;
        });
 
 
        for(int v = 1; v <= n; ++v){
            sort(adj[v].begin(), adj[v].end());
        }
 
        long long max_val = 0;
 
 
        for(int i = 0; i < n; ++i){
            int u = nodes[i];
            for(int j = i + 1; j < n; ++j){
                int v = nodes[j];
 
                if(!binary_search(adj[u].begin(), adj[u].end(), v)){
                    long long temp = static_cast<long long>(deg[u]) + deg[v] - 1;
                    max_val = max(max_val, temp);
                    break; 
                }
            }
        }
 
        long long maxA = 0;
 
 
        for(int u = 1; u <= n; ++u){
            for(auto &v : adj[u]){
                if(u < v){ 
                    long long temp = static_cast<long long>(deg[u]) + deg[v] - 2;
                    maxA = max(maxA, temp);
                }
            }
        }
 
 
        cout << max(max_val, maxA) << "\n";
    }
 
    return 0;
}