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

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 ll long long
#define vll vector <long long>
#define vc vector <char>
#define vi vector <int>
#define all(v) v.begin(),v.end()
#define repi(i,a,b) for(ll i=a;i<b;i++)
#define repd(i,a,b) for(ll i=a;i>=b;i--)
#define F first
#define S second
#define pb push_back
#define Yes cout<<"YES"<<endl;
#define No cout<<"NO"<<endl;
#define hey cout<<"hey"<<endl;
#define vpi vector<pair<int,int>>
#define vpll vector<pair<long long,long long>>
#define lb(a,x) lower_bound(a.begin(),a.end(),x)
#define ub(a,x) upper_bound(a.begin(),a.end(),x)


vll input(ll n){
    vll v(n);
    repi(i,0,n)     cin>>v[i];
    return v;
}


void dfs(map<ll,set<ll>> &g,ll &node,vll &vis){
    vis[node]=1;
    for(auto it : g[node]){
        // if(g.find(it)==g.end()) continue;
        if(vis[it]==0){
            dfs(g,it,vis);
        }
    }
}


int main()
{
  // sieve();
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);


int t;
cin>>t;
while(t--)
    {
        ll n;
        cin>>n;
        
        map<ll,set<ll>> g;

        repi(i,0,n-1){
            ll u,v;
            cin>>u>>v;
            g[u].insert(v);
            g[v].insert(u);
        }

        
        ll first=-1;
        ll curr=0;
        for(auto it:g){ 
            if( it.second.size()>=curr){
                curr= it.second.size();
                first=it.first;
            }
        }

        for(auto it:g[first]){
            g[it].erase(first);
        }
        g.erase(first);
        
        first=-1;
        curr=0;
        for(auto it:g){
            if( it.second.size()>=curr){
                curr= it.second.size();
                first=it.first;
            }
        }
        // cout<<g[first].size()<<endl;
        // cout<<"first2="<<first<<endl;
        for(auto it:g[first]){
            g[it].erase(first);
        }
        g.erase(first);


        ll cnt=0;
        vll vis(n+1);
        repi(i,1,n+1){
            if(g.find(i)!=g.end()){
                if(vis[i]==0){
                    cnt++;
                    dfs(g,i,vis);
                }
            }
        }

        cout<<cnt<<endl;
    }
    return 0;
}

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 ll long long
#define vll vector <long long>
#define vc vector <char>
#define vi vector <int>
#define all(v) v.begin(),v.end()
#define repi(i,a,b) for(ll i=a;i<b;i++)
#define repd(i,a,b) for(ll i=a;i>=b;i--)
#define F first
#define S second
#define pb push_back
#define Yes cout<<"YES"<<endl;
#define No cout<<"NO"<<endl;
#define hey cout<<"hey"<<endl;
#define vpi vector<pair<int,int>>
#define vpll vector<pair<long long,long long>>
#define lb(a,x) lower_bound(a.begin(),a.end(),x)
#define ub(a,x) upper_bound(a.begin(),a.end(),x)


vll input(ll n){
    vll v(n);
    repi(i,0,n)     cin>>v[i];
    return v;
}


void dfs(map<ll,set<ll>> &g,ll &node,vll &vis){
    vis[node]=1;
    for(auto it : g[node]){
        if(vis[it]==0){
            dfs(g,it,vis);
        }
    }
}


int main()
{
  // sieve();
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);


int t;
cin>>t;
while(t--)
    {
        ll n;
        cin>>n;
        
        map<ll,set<ll>> g;

        repi(i,0,n-1){
            ll u,v;
            cin>>u>>v;
            g[u].insert(v);
            g[v].insert(u);
        }

        ll ans=0;
        for(auto it:g){
            for(auto ele:it.second){
                ans=max(ans,it.second.size()+g[ele].size()-2LL);
            }
        }

        set<pair<ll,ll>> st;
        for(auto it:g){
            st.insert({it.second.size(),it.first});
        }

        auto it=++st.rbegin();
        auto it1=st.rbegin();
        while(it!=st.rend()){
            if(g[it1->second].find(it->second)==g[it1->second].end()){
                // hey
                // cout<<"it.s="<<it->second<<" it.f="<<it->first<<endl;
                ans=max(ans,it1->first-1+it->first);
                // break;
            }
            it++;
        }
        cout<<ans<<endl;
    }
    return 0;
}