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

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define int long long
using namespace std;

bool comp(pair<int, int> a, pair<int, int> b) {
    return a.first > b.first;
}

void solve() {
    int n;
    cin >> n;
    vector<set<int>> adj(n);
    vector<pair<int, int>> deg(n, {0, 0});
    for(int i = 0; i < n-1; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        adj[u].insert(v);
        adj[v].insert(u);
        deg[u].first++;
        deg[u].second = u;
        deg[v].first++;
        deg[v].second = v;
    }

    sort(deg.begin(), deg.end(), comp);

    if(n == 2) {
        cout << "0\n";
        return;
    }

    if(deg[1].first > deg[2].first) {
        if(adj[deg[0].second].find(deg[1].second) != adj[deg[0].second].end()) {
            cout << deg[0].first + deg[0].first - 2 << endl;
            return;
        }
        cout << deg[0].first + deg[0].first - 1 << endl;
        return;
    }

    int ans = 0;
    if(adj[deg[0].second].find(deg[1].second) != adj[deg[0].second].end()) {
        ans = deg[0].first + deg[1].first - 2;
    } else {
        ans = deg[0].first + deg[1].first - 1;
    }

    int i = 2;
    while(i < n && deg[i].first == deg[1].first) {
        if(adj[deg[0].second].find(deg[i].second) != adj[deg[0].second].end()) {
            ans = max(ans, deg[0].first + deg[i].first - 2);
        } else {
            ans = max(ans, deg[0].first + deg[i].first - 1);
        }
        i++;
    }

    cout << ans << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
}

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define int long long
using namespace std;

bool comp(pair<int, int> a, pair<int, int> b) {
    return a.first > b.first;
}

void solve() {
    int n;
    cin >> n;
    vector<set<int>> adj(n);
    vector<pair<int, int>> deg(n, {0, 0});
    for(int i = 0; i < n-1; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        adj[u].insert(v);
        adj[v].insert(u);
        deg[u].first++;
        deg[u].second = u;
        deg[v].first++;
        deg[v].second = v;
    }

    sort(deg.begin(), deg.end(), comp);

    if(n == 2) {
        cout << "0\n";
        return;
    }

    if(deg[1].first > deg[2].first) {
        if(adj[deg[0].second].find(deg[1].second) != adj[deg[0].second].end()) {
            cout << deg[0].first + deg[1].first - 2 << endl;
            return;
        }
        cout << deg[0].first + deg[1].first - 1 << endl;
        return;
    }

    int ans = 0;
    if(adj[deg[0].second].find(deg[1].second) != adj[deg[0].second].end()) {
        ans = deg[0].first + deg[1].first - 2;
    } else {
        ans = deg[0].first + deg[1].first - 1;
    }

    int i = 2;
    while(i < n && deg[i].first == deg[1].first) {
        if(adj[deg[0].second].find(deg[i].second) != adj[deg[0].second].end()) {
            ans = max(ans, deg[0].first + deg[i].first - 2);
        } else {
            ans = max(ans, deg[0].first + deg[i].first - 1);
        }
        i++;
    }

    cout << ans << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
}