Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 4):
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// #define ordered_multiset tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update>
#define lcm(a,b) (a*b)/__gcd(a,b)
#define nl "\n"
#define spc " "

typedef long long ll;

const int N = 2e5+5;
const int mod = 1000000007;

// code starts here

int find_max(vector<int>&V,int length)
{
    int ans=0,index=1;
    for(int i=1;i<length;i++)
    {
        if(V[i]>ans)
        {
            ans=V[i];
            index=i;
        }
    }
    return index;
}

vector<int> my_func(vector<int>V,int length)
{
    vector<int>a;
    vector<pair<int,int> >p;
    p.push_back({0,0});
    for(int i=1;i<length;i++)
    {
        p.push_back({V[i],i});
    }
    sort(p.begin(),p.end());
    a.push_back(p[length-1].second);

    for(int i=1;i<length;i++)
    {
        if(p[i].first==p[length-2].first)
        {
           a.push_back(p[i].second);
        }
    }
    return a;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    //t=1;
    while(t--)
    {
       int n;
       cin>>n;
       vector<int>g[n+1];
       vector<int>degree(n+1,0);
       int ans=1;
       for(int i=1;i<n;i++)
       {
           int u,v;
           cin>>u>>v;
           g[u].push_back(v);
           g[v].push_back(u);
           degree[u]++;
           degree[v]++;
       }
       vector<int>V=my_func(degree,n+1);
       set<int>st;
       for(auto it: V)
       {
           st.insert(it);
       }
       int m=V.size()-1;
       int node=0;
       for(auto it: V)
       {
           int cnt=0;
           for(auto itt: g[it])
           {
               if(st.find(itt)!=st.end())
                cnt++;
           }
           if(cnt<m)
           {
               node=it;
               break;
           }
       }
       if(node==0)
        node=V[0];

       ans=ans+degree[node]-1;
       degree[node]=0;
       for(auto it : g[node])
       {
           degree[it]--;
       }
       node=find_max(degree,n+1);
       ans=ans+degree[node]-1;
       if(ans<0)
       ans=0;
       cout<<ans<<nl;
    }

}


Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// #define ordered_multiset tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update>
#define lcm(a,b) (a*b)/__gcd(a,b)
#define nl "\n"
#define spc " "

typedef long long ll;

const int N = 2e5+5;
const int mod = 1000000007;

// code starts here

int find_max(vector<int>&V,int length)
{
    int ans=0,index=1;
    for(int i=1;i<length;i++)
    {
        if(V[i]>ans)
        {
            ans=V[i];
            index=i;
        }
    }
    return index;
}

vector<int> my_func(vector<int>V,int length)
{
    vector<int>a;
    vector<pair<int,int> >p;
    p.push_back({0,0});
    for(int i=1;i<length;i++)
    {
        p.push_back({V[i],i});
    }
    sort(p.begin(),p.end());
    a.push_back(p[length-1].second);

    for(int i=1;i<length-1;i++)
    {
        if(p[i].first==p[length-2].first)
        {
           a.push_back(p[i].second);
        }
    }
    return a;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    //t=1;
    while(t--)
    {
       int n;
       cin>>n;
       vector<int>g[n+1];
       vector<int>degree(n+1,0);
       int ans=1;
       for(int i=1;i<n;i++)
       {
           int u,v;
           cin>>u>>v;
           g[u].push_back(v);
           g[v].push_back(u);
           degree[u]++;
           degree[v]++;
       }
       vector<int>V=my_func(degree,n+1);
       set<int>st;
       for(auto it: V)
       {
           st.insert(it);
       }
       int m=V.size()-1;
       int node=0;
       for(auto it: V)
       {
           int cnt=0;
           for(auto itt: g[it])
           {
               if(st.find(itt)!=st.end())
                cnt++;
           }
           if(cnt<m)
           {
               node=it;
               break;
           }
       }
       if(node==0)
        node=V[0];

       ans=ans+degree[node]-1;
       degree[node]=0;
       for(auto it : g[node])
       {
           degree[it]--;
       }
       node=find_max(degree,n+1);
       ans=ans+degree[node]-1;
       if(ans<0)
       ans=0;
       cout<<ans<<nl;
    }

}