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

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

vector<vector<int>> G;
vector<int> degs;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, q, t, a, b, u, v;
    cin >> t;
    while(t--){
        cin >> n;
        G.clear();
        G.resize(n + 2);
        degs.clear();
        degs.resize(n + 2);
        for(int i = 1 ; i < n ; i++){
            cin >> v >> u;
            G[u].push_back(v);
            G[v].push_back(u);
            degs[u]++;
            degs[v]++;
        }
        int ans = 1, maxDeg = -1, idx = 0;
        for(int i = 1 ; i <= n ; i++){
            //cerr << degs[i] << " ";
            if(degs[i] > maxDeg){
                maxDeg = degs[i];
                idx = i;
            }
        }
        ans += maxDeg - 1;
        for(int i : G[idx]){
            degs[i]--;
        }
        degs[idx] = -1;
        maxDeg = -1;
        for(int i = 1 ; i <= n ; i++){
            //cerr << degs[i] << " ";
            if(degs[i] > maxDeg){
                maxDeg = degs[i];
                idx = i;
            }
        }
        ans += maxDeg - 1;
        cout << ans << '\n';
    }
}

Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

vector<vector<int>> G;
vector<unordered_set<int>> neigh;
vector<int> degs;
vector<int> ids;

pii findBest(){
    for(int i = 0 ; i < ids.size() ; i++){
        for(int j = i + 1 ; j < ids.size() ; j++){
            if(neigh[ids[i]].count(ids[j]) == 0) return {ids[i], ids[j]};
        } 
    }
    return {-1, -1};
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, t, u, v;
    cin >> t;
    while(t--){
        cin >> n;
        G.clear();
        G.resize(n + 2);
        neigh.clear();
        neigh.resize(n + 2);
        ids.clear();
        degs.clear();
        degs.resize(n + 2);
        for(int i = 1 ; i < n ; i++){
            cin >> v >> u;
            G[u].push_back(v);
            G[v].push_back(u);
            neigh[u].insert(v);
            neigh[v].insert(u);
            degs[u]++;
            degs[v]++;
        }
        int ans = 0, maxDeg = -1, idx = 0;
        pii XD;
        for(int i = 1 ; i <= n ; i++){
            //cerr << degs[i] << " ";
            if(degs[i] > maxDeg){
                ids.clear();
                maxDeg = degs[i];
            }
            if(degs[i] == maxDeg){
                ids.push_back(i);
            }
        }
        if(ids.size() >= 3){
            XD = findBest();
            cout << degs[XD.first] + degs[XD.second] - 1 << '\n';
        }else{
            ans += maxDeg;
            for(int i : G[idx]){
                degs[i]--;
            }
            degs[idx] = -1;
            maxDeg = -1;
            for(int i = 1 ; i <= n ; i++){
                //cerr << degs[i] << " ";
                if(degs[i] > maxDeg){
                    maxDeg = degs[i];
                    idx = i;
                }
            }
            ans += maxDeg - 1;
            cout << ans << '\n';
        }
    }
}