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

Previous 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];
}
*/

Submission (verdict: Accepted):
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;


int main() {
    int t; cin >> t;
    while(t--){
        ll n;
        cin >> n;
        set<pair<ll,ll>> edges;
        vector<ll> deg(n+1);
        for(int i = 0; i < n-1; i++) {
            ll a,b;
            cin >> a >> b;
            edges.insert({a,b});
            edges.insert({b,a});
            deg[a]++;
            deg[b]++;
        }
        ll ans = 0;
        vector<pair<ll,ll>> b;
        for(int i = 0; i < n; i++) {
            b.push_back({deg[i+1], i+1});
        }
        sort(b.begin(), b.end(), greater<pair<ll,ll>>());
        for(int i = 0; i < n; i++) {
            for(ll j = i+1; j < n; j++) {
                if(edges.find({b[i].second, b[j].second}) == edges.end()) {
                    ans = max(ans, b[j].first + b[i].first - 1);
                    break;
                }
            }
        }

        for(auto x : edges) {
            ans = max(ans, deg[x.first] + deg[x.second] - 2);
        }
        cout << ans << 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];
}
*/