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; 
using ll = long long int;

// Function to find the index of the largest element in an array
int largest(const vector<int>& arr) {
    int max_index = 0;
    for (int i = 1; i < arr.size(); i++)
        if (arr[i] > arr[max_index])
            max_index = i;
    return max_index;
}

int main() {
    int t;
    cin >> t;
    
    while (t--) {
        int n;
        cin >> n;
        
        vector<vector<int>> adj(n);
        vector<int> degree(n, 0);

        for (int i = 0; i < n - 1; i++) {
            int a, b;
            cin >> a >> b;
            a--; b--;  // Convert to 0-based indexing
            adj[a].push_back(b);
            adj[b].push_back(a);
            degree[a]++;
            degree[b]++;
        }

        // Find the node with the highest degree (first removal)
        int p = largest(degree);
        int sum = degree[p];  // Store the count of removed edges

        // Remove p from the tree
        for (int neighbor : adj[p]) {
            degree[neighbor]--;  // Decrease neighbor's degree
        }
        degree[p] = 0;  // Mark p as removed

        // Find the new node with the highest degree (second removal)
        int q = largest(degree);
        sum += degree[q] - 1;  // Remove q but count its edges - 1

        cout << sum << '\n';
    }
    
    return 0;
}



     
    
    
   

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