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

Previous submission (verdict: Time limit exceeded on test 3):
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;


int main() {
    int t; cin >> t;
    while(t--){
        int n; cin >> n;
        int u, v;
        vector<pair<int, int>>v1;
        map<int, int>m1;
        for(int i = 0; i < n-1; i++){
            cin >> u >> v;
            v1.push_back({u, v});
            m1[u]++;
            m1[v]++;
        }
        vector<pair<int, int>> vec(m1.begin(), m1.end());

        sort(vec.begin(), vec.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
            return a.second > b.second;
        });
        int res = 0;
        int counter = 0;
        for(int i = 0; i < n-2; i++){
            for(int j = 1; j < n-1; j++){
                int max1 = vec[i].first;
                int max2 = vec[j].first;
                counter = 0;
                for(int k = 0; k < n-1; k++){
                    if(v1[k].first != max1 && v1[k].second != max1 && v1[k].first != max2 && v1[k].second != max2){
                        counter++;
                    }
                }
                res = max(res, counter + (n - 2 - (2*counter)));
            }
        }


        cout << res << endl;
    }

    return 0;
}



/*
ll gcd(int a, int b){
    if(b == 0) return a;
    return gcd(b, a%b);
}

ll lcm(int a, int b){
    return a*b / gcd(a,b);
}
*/
/*
int knapSack(int W, const vector<int>& weights, const vector<int>& values, int n) {

    vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));

    // Build the DP table
    for (int i = 1; i <= n; i++) {
        for (int w = 0; w <= W; w++) {
            if (weights[i - 1] <= w) {
                // Include the item or exclude it
                dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - weights[i - 1]] + values[i - 1]);
            } else {
                // Exclude the item
                dp[i][w] = dp[i - 1][w];
            }
        }
    }

    // The last cell of the DP table contains the maximum value
    return dp[n][W];
}
*/

Submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;


int main() {
    int t; cin >> t;
    while(t--){
        int n;
        cin >> n;
        int u, v;
        vector<pair<int, int>> edges;
        map<int, int> degree;

        for (int i = 0; i < n - 1; i++) {
            cin >> u >> v;
            edges.push_back({u, v});
            degree[u]++;
            degree[v]++;
        }

        vector<pair<int, int>> vec(degree.begin(), degree.end());
        sort(vec.begin(), vec.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
            return a.second > b.second;
        });

        
        int max1 = vec[0].first;
        int max2 = vec[1].first;
        
        bool areNeighbors = false;
        for (auto edge : edges) {
            if ((edge.first == max1 && edge.second == max2) || (edge.first == max2 && edge.second == max1)) {
                areNeighbors = true;
                break;
            }
        }
        
        int result;
        if (areNeighbors) {
            result = degree[max1] + degree[max2] - 2;
        } else {
            result = degree[max1] + degree[max2] - 1;
        }

        cout << result << endl;

    }

    return 0;
}



/*
ll gcd(int a, int b){
    if(b == 0) return a;
    return gcd(b, a%b);
}

ll lcm(int a, int b){
    return a*b / gcd(a,b);
}
*/
/*
int knapSack(int W, const vector<int>& weights, const vector<int>& values, int n) {

    vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));

    // Build the DP table
    for (int i = 1; i <= n; i++) {
        for (int w = 0; w <= W; w++) {
            if (weights[i - 1] <= w) {
                // Include the item or exclude it
                dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - weights[i - 1]] + values[i - 1]);
            } else {
                // Exclude the item
                dp[i][w] = dp[i - 1][w];
            }
        }
    }

    // The last cell of the DP table contains the maximum value
    return dp[n][W];
}
*/