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

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

int M = 1000000007;


int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    

    int t;
    cin>>t;

    while(t--){
        int n;
        cin>>n;

        vector<vector<int>> v(n);

        for(int i=0; i<n-1; ++i){
            int a, b;
            cin>>a>>b;
            v[a-1].push_back(b);
            v[b-1].push_back(a);
        }
        vector<pair<int, int>> mm(n);
        for(int i=0; i<n; ++i){
            int m=0;
            int mi=-1;
            for(int j=0; j<v[i].size(); ++j){
                m++;
                
            }
            mm[i] = {m , i};
        }

        sort(mm.begin(), mm.end());
        int m1=mm[n-1].second+1;
        int d1= mm[n-1].first;
        vector<pair<int, int>> mm1(n);
        for(int i=0; i<n; ++i){
            int m=0;
            int mi=-1;
            if(m1==i+1) {mm1[i]={-1,i};continue;}
            for(int j=0; j<v[i].size(); ++j){
                if(v[i][j]==m1) continue;
                m++;
                
            }
            mm1[i] = {m , i};
        }
        sort(mm1.begin(), mm1.end());
        int m2= mm1[n-1].second+1;
        int d2=mm1[n-1].first;
        //m2=1;

        //cout<<"m1 "<<m1<<"m2 "<<m2<<"\n";
        // int rem=0;
        // unordered_map<int, int> umap;
        // for(int i=0; i<n; ++i){
        //     int cnt=0;
            
        //     if(i+1==m1 || i+1==m2) {continue;}
        //     for(int j=0; j<v[i].size(); ++j){
        //         if(v[i][j]==m1 || v[i][j]==m2) continue;
        //         cnt++;
        //         umap[v[i][j]]++;
        //     }   

        //     if(cnt==0) rem++;
        //     else if(umap.find(i+1)==umap.end()){
        //         rem++;
        //     }

        // }
        //cout<<"rem "<<rem<<"\n";
        //cout<<"d1 "<<d1<<"\n";
        //cout<<"d2 "<<d2<<"\n";
        if(d1==0) d1=-1;
        if(d2==0) d2=-1;
        if(d1==1) d1=0;
        if(d2==1) d2=0;
        

        cout<<max(d1+d2,0)<<"\n";


    }

    

    

    return 0;

}

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

int M = 1000000007;


int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    

    int t;
    cin>>t;

    while(t--){
        int n;
        cin>>n;

        vector<vector<int>> v(n);

        for(int i=0; i<n-1; ++i){
            int a, b;
            cin>>a>>b;
            v[a-1].push_back(b);
            v[b-1].push_back(a);
        }
        vector<pair<int, int>> mm(n);
        for(int i=0; i<n; ++i){
            int m=0;
            int mi=-1;
            for(int j=0; j<v[i].size(); ++j){
                m++;
                
            }
            mm[i] = {m , i};
        }

        sort(mm.begin(), mm.end());
        int m1=mm[n-1].second+1;
        int d1= mm[n-1].first;
        vector<pair<int, int>> mm1(n);
        for(int i=0; i<n; ++i){
            int m=0;
            int mi=-1;
            if(m1==i+1) {mm1[i]={-1,i};continue;}
            for(int j=0; j<v[i].size(); ++j){
                if(v[i][j]==m1) continue;
                m++;
                
            }
            mm1[i] = {m , i};
        }
        sort(mm1.begin(), mm1.end());
        int m2= mm1[n-1].second+1;
        int d2=mm1[n-1].first;
        //m2=1;

        //cout<<"m1 "<<m1<<"m2 "<<m2<<"\n";
        // int rem=0;
        // unordered_map<int, int> umap;
        // for(int i=0; i<n; ++i){
        //     int cnt=0;
            
        //     if(i+1==m1 || i+1==m2) {continue;}
        //     for(int j=0; j<v[i].size(); ++j){
        //         if(v[i][j]==m1 || v[i][j]==m2) continue;
        //         cnt++;
        //         umap[v[i][j]]++;
        //     }   

        //     if(cnt==0) rem++;
        //     else if(umap.find(i+1)==umap.end()){
        //         rem++;
        //     }

        // }
        //cout<<"rem "<<rem<<"\n";
        //cout<<"d1 "<<d1<<"\n";
        //cout<<"d2 "<<d2<<"\n";
        // if(d1==0) d1=-1;
        // if(d2==0) d2=-1;
        // if(d1==1) d1=0;
        // if(d2==1) d2=0;
        

        cout<<max(d1-1+d2,0)<<"\n";


    }

    

    

    return 0;

}