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;


#define ll long long int
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define F first
#define S second
#define sz(x) ((int)(x).size())
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

const int MOD = 1e9 + 7;
const ll INF = 1e18;


template<typename T>
void debug(vector<T> v) { for(auto i: v) cout << i << " "; cout << endl; }

template<typename T1, typename T2>
void debug(vector<pair<T1, T2>> v) { for(auto i: v) cout << "(" << i.F << "," << i.S << ") "; cout << endl; }


int main() {
    fast_io;

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<vector<int>>neighbours(n,vector<int>());
        vector<int>deg(n,0);
        for(int i=0;i<n-1;i++){
            int a,b;
            cin>>a>>b;
            neighbours[a-1].push_back(b-1);
            neighbours[b-1].push_back(a-1);
            deg[a-1]++;
            deg[b-1]++;
        }
        int maxi = 0;
        int conn = 1;
        vector<int>minis(n,INT_MAX);
        priority_queue< pair<int,int> ,vector<pair<int,int>> , greater<pair<int,int>> > pq;
        for(int i=0;i<n;i++){
            if(deg[i] == 1){
                minis[i] = 0;
               pq.push({0,i});
            }
        }

        while(!pq.empty()){
            pair<int,int> p  =pq.top();
            pq.pop();
            for(auto e : neighbours[p.first]){
                if(minis[e] > minis[p.first] + 1){
                    minis[e] = minis[p.first] + 1;
                    pq.push({minis[e] , p.first});
                }
            }

        }

        for(int i =1;i<n;i++){
            if(deg[maxi]  < deg[i] ){
                maxi = i;
            }
        }

        int optimal = maxi;
        for(int i = 0;i<n;i++){
            if(deg[i] == deg[maxi]){
                if(minis[i] < minis[maxi])optimal = i;
            }
        }
        maxi = optimal;
        for(auto e:neighbours[maxi]){
            deg[e]--;
        }
        conn = conn -1 + deg[maxi];
        deg[maxi] = INT_MIN;

        
        maxi = 0;
        for(int i =1;i<n;i++){
            if(deg[maxi]  < deg[i] ){
                maxi = i;
            }
        }
        conn = conn -1 + deg[maxi];

        cout<<conn<<endl;


        
    }

    return 0;
}

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


#define ll long long int
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define F first
#define S second
#define sz(x) ((int)(x).size())
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

const int MOD = 1e9 + 7;
const ll INF = 1e18;


template<typename T>
void debug(vector<T> v) { for(auto i: v) cout << i << " "; cout << endl; }

template<typename T1, typename T2>
void debug(vector<pair<T1, T2>> v) { for(auto i: v) cout << "(" << i.F << "," << i.S << ") "; cout << endl; }


int main() {
    fast_io;

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<vector<int>>neighbours(n,vector<int>());
        vector<int>deg(n,0);
        for(int i=0;i<n-1;i++){
            int a,b;
            cin>>a>>b;
            neighbours[a-1].push_back(b-1);
            neighbours[b-1].push_back(a-1);
            deg[a-1]++;
            deg[b-1]++;
        }
        int maxi = 0;
        int conn = 1;
        vector<int>minis(n,INT_MAX);
        priority_queue< pair<int,int> ,vector<pair<int,int>> , greater<pair<int,int>> > pq;
        for(int i=0;i<n;i++){
            if(deg[i] == 1){
                minis[i] = 0;
               pq.push({0,i});
            }
        }

        while(!pq.empty()){
            pair<int,int> p  =pq.top();
            pq.pop();
            int node = p.second;
            int dist  =p.first;
            for(auto e : neighbours[node]){
                if(minis[e] > minis[node] + 1){
                    minis[e] = minis[node] + 1;
                    pq.push({minis[e] , e});
                }
            }

        }

        for(int i =1;i<n;i++){
            if(deg[maxi]  < deg[i] ){
                maxi = i;
            }
        }

        int optimal = maxi;
        for(int i = 0;i<n;i++){
            if(deg[i] == deg[maxi]){
                if(minis[i] < minis[maxi])optimal = i;
            }
        }
        // for(int i=0;i<n;i++){
        //     cout<<minis[i]<<" ";
        // }
        // cout<<endl;
        maxi = optimal;
        for(auto e:neighbours[maxi]){
            deg[e]--;
        }
        conn = conn -1 + deg[maxi];
        // cout<<"hello"<<maxi<<" " <<conn<<endl;
        deg[maxi] = INT_MIN;

        
        maxi = 0;
        for(int i =1;i<n;i++){
            if(deg[maxi]  < deg[i] ){
                maxi = i;
            }
        }
        conn = conn -1 + deg[maxi];
        // cout<<"hello"<<maxi<<" " <<conn<<endl;

        cout<<conn<<endl;


        
    }

    return 0;
}