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

Previous submission (verdict: Wrong answer on test 4):
// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;

#define ll long long

void get(ll ind,vector<vector<ll>> &mad,vector<bool> &vis,priority_queue<pair<ll,ll>> &lad,int count)
{
    for(int i=0;i<mad[ind].size();i++)
    {
        if(vis[mad[ind][i]]==false)
        {
            vis[mad[ind][i]]=true;
            get(mad[ind][i],mad,vis,lad,1);
            count++;
        }
    }
    lad.push({count,ind});
}

void get1(ll ind,vector<vector<ll>> &mad,vector<bool> &vis)
{
    for(int i=0;i<mad[ind].size();i++)
    {
        if(vis[mad[ind][i]]==false)
        {
            vis[mad[ind][i]]=true;
            get1(mad[ind][i],mad,vis);
        }
    }
}

int main() {
    // Write C++ code here
    ll t,n,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<vector<ll>> mad(n+1);
        for(int i=1;i<n;i++)
        {
            cin>>x>>y;
            mad[x].push_back(y);
            mad[y].push_back(x);
        }
        
        if(n<=2)
        {
            cout<<0<<endl;
            continue;
        }
        
        vector<bool> vis(n+1);
        ll a=mad[1].size(),b=1;
        for(int i=2;i<=n;i++)
        {
            if(mad[i].size()>=a)
            {
                a=mad[i].size();
                b=i;
            }
        }
        
        vis[b]=true;
        vector<bool> vis2(n+1);
        vis2[b]=true;
        priority_queue<pair<ll,ll>> lad;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==false)
            {
                vis[i]=true;
                get(i,mad,vis,lad,0);
            }
        }
        
        vis2[lad.top().second]=true;
        ll count=0;
        for(int i=1;i<=n;i++)
        {
            if(vis2[i]==false)
            {
                vis2[i]=true;
                get1(i,mad,vis2);
                count++;
            }
        }
        cout<<count<<endl;
        
        
    }

    return 0;
}

Submission (verdict: Runtime error on test 2):
// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;

#define ll long long

void get(ll ind,vector<vector<ll>> &mad,vector<bool> &vis,priority_queue<pair<ll,ll>> &lad,int count)
{
    for(int i=0;i<mad[ind].size();i++)
    {
        if(vis[mad[ind][i]]==false)
        {
            vis[mad[ind][i]]=true;
            get(mad[ind][i],mad,vis,lad,1);
            count++;
        }
    }
    lad.push({count,ind});
}

void get1(ll ind,vector<vector<ll>> &mad,vector<bool> &vis)
{
    for(int i=0;i<mad[ind].size();i++)
    {
        if(vis[mad[ind][i]]==false)
        {
            vis[mad[ind][i]]=true;
            get1(mad[ind][i],mad,vis);
        }
    }
}

int main() {
    // Write C++ code here
    ll t,n,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<vector<ll>> mad(n+1);
        for(int i=1;i<n;i++)
        {
            cin>>x>>y;
            mad[x].push_back(y);
            mad[y].push_back(x);
        }
        
        if(n<=2)
        {
            cout<<0<<endl;
            continue;
        }
        
        vector<bool> vis(n+1);
        ll a=mad[1].size(),b=1,c=0;
        for(int i=2;i<=n;i++)
        {
            if(mad[i].size()>a)
            {
                a=mad[i].size();
                b=i;
                c=1;
            }
            else if(mad[i].size()>=a)
            {
                c++;
            }
        }
        if(c==1)
        {
            
        vis[b]=true;
        vector<bool> vis2(n+1);
        vis2[b]=true;
        priority_queue<pair<ll,ll>> lad;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==false)
            {
                vis[i]=true;
                get(i,mad,vis,lad,0);
            }
        }
        
        vis2[lad.top().second]=true;
        ll count=0;
        for(int i=1;i<=n;i++)
        {
            if(vis2[i]==false)
            {
                vis2[i]=true;
                get1(i,mad,vis2);
                count++;
            }
        }
        cout<<count<<endl;
        }
        else{
            vector<ll> sad;
            for(int i=1;i<=n;i++)
            {
                if(mad[i].size()==a)
                {
                    sad.push_back(i);
                }
            }
            
            if(sad.size()==2)
            {
                vector<bool> vis2(n+1);
                vis2[sad[0]]=true;
                vis2[sad[1]]=true;
                ll count=0;
        for(int i=1;i<=n;i++)
        {
            if(vis2[i]==false)
            {
                vis2[i]=true;
                get1(i,mad,vis2);
                count++;
            }
        }
        cout<<count<<endl;
            }
            else{
                ll ans=0;
                for(int i=0;i<3;i++)
                {
                    vector<bool> vis2(n+1);
                vis2[sad[i]]=true;
                vis2[sad[(i+1)%3]]=true;
                ll count=0;
                    for(int i=1;i<=n;i++)
                    {
                        if(vis2[i]==false)
                        {
                            vis2[i]=true;
                            get1(i,mad,vis2);
                            count++;
                        }
                    }
                    if(count>ans)
                    ans=count;
                }
                cout<<ans<<endl;
            }
        }
        
        
    }

    return 0;
}