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 fast_io ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define int long long
#define ll long long
#define mod 1000000007

vector<int> f(int n) {
    vector<int> v(61, 0);
    for (int i = 60; i >= 0; --i) {
        v[i] = (n & 1);
        n >>= 1;
    }
    return v;
}

signed main(){
    fast_io;
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<set<int>> adj(n+1);
        vector<int> count(n+1,0);
        for(int i=0;i<n-1;i++){
            int u,v;
            cin>>u>>v;
            adj[u].insert(v);
            adj[v].insert(u);
            count[u]++;
            count[v]++;
        }
        map<int,set<int>> mp;
        set<int> sety;
        for(int i=1;i<=n;i++){
            mp[count[i]].insert(i);
            sety.insert(count[i]);
        }
        if(n==2){
            cout<<0<<endl;
        }
        else{
        int count1=mp[*sety.rbegin()].size();
        //cout<<count1<<endl;
        if(count1==1 && sety.size()>1){
            int flag=0;
            int ans=*sety.rbegin();
            //cout<<ans<<endl;
            auto it2=sety.rbegin();
            ++it2;
            int val=*it2;
            ans+=val;
            //cout<<ans<<endl;
            int maxval=*mp[*sety.rbegin()].begin();
            //cout<<maxval<<endl;
            for(auto it:mp[val]){
                if(!adj[maxval].count(it)){
                    flag=1;
                    break;
                }
            }
            if(!flag){
                ans-=2;
            }
            else{
                ans--;
            }
            cout<<ans<<endl;
        }
        else{ 
            int ans=((*sety.rbegin())*2)-1;
            cout<<ans<<endl;
        }
        }
    }   
}







Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define int long long
#define ll long long
#define mod 1000000007

vector<int> f(int n) {
    vector<int> v(61, 0);
    for (int i = 60; i >= 0; --i) {
        v[i] = (n & 1);
        n >>= 1;
    }
    return v;
}

signed main(){
    fast_io;
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<set<int>> adj(n+1);
        vector<int> count(n+1,0);
        for(int i=0;i<n-1;i++){
            int u,v;
            cin>>u>>v;
            adj[u].insert(v);
            adj[v].insert(u);
            count[u]++;
            count[v]++;
        }
        map<int,set<int>> mp;
        set<int> sety;
        for(int i=1;i<=n;i++){
            mp[count[i]].insert(i);
            sety.insert(count[i]);
        }
        if(n==2){
            cout<<0<<endl;
        }
        else{
        int count1=mp[*sety.rbegin()].size();
        //cout<<count1<<endl;
        if(count1==1 && sety.size()>1){
            int flag=0;
            int ans=*sety.rbegin();
            //cout<<ans<<endl;
            auto it2=sety.rbegin();
            ++it2;
            int val=*it2;
            ans+=val;
            //cout<<ans<<endl;
            int maxval=*mp[*sety.rbegin()].begin();
            //cout<<maxval<<endl;
            for(auto it:mp[val]){
                if(!adj[maxval].count(it)){
                    flag=1;
                    break;
                }
            }
            if(!flag){
                ans-=2;
            }
            else{
                ans--;
            }
            cout<<ans<<endl;
        }
        else{ 
            int ans=((*sety.rbegin())*2)-2;
            cout<<ans<<endl;
        }
        }
    }   
}