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.

*******************************************************************************/
/******************************************************************************

                              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 <iostream>
#include <bits/stdc++.h> 
#include <cmath>
using namespace std; 
#include <bits/stdc++.h> 
using namespace std; 
using ll = long long int;
 
 int largest(vector<int>& arr) {
         int max = 0;

    
    for (int i = 1; i < arr.size(); i++)
        if (arr[i] > arr[max])
            max = i;

    return max;
}
 
int main()
{  int n;
   cin>>n;
   vector <int> v;
   
   int a,m,b;
    
    for(int i=0;i<n;i++ ){
        
        cin>>m;
        vector <int> vec[m];
        for(int j=0;j<m;j++){
            v.push_back(0);
            
        }
        
        for(int j=0;j<m-1;j++){
            cin>>a>>b;
            v[a-1]=v[a-1]+1;
            v[b-1]=v[b-1]+1;
            vec[a-1].push_back(b-1);
            vec[b-1].push_back(a-1);
        }
        
        int p= largest(v);
        
        int sum=v[p];
        
        for(int j=0;j<vec[p].size();j++){
            v[vec[p][j]]=v[vec[p][j]]-1;
            
        } 
        
        v.erase(v.begin() + p);
        int q=largest(v);
        
        sum=sum+v[q]-1; 
        cout<<sum<<'\n';
        v.clear();
        
        }
    
     
    
    
    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; 
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;
}