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

Previous submission (verdict: Time limit exceeded on test 2):
#include<bits/stdc++.h>
using namespace std;
typedef long long  ll;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const int N=2e5+5;
vector<ll>n1[N];
set<ll>ed[N];
vector<ll>num[N];
void solve(){       
    ll m;
    cin>>m;
    for(int i=1;i<m;i++){
        ll a,b;
        cin>>a>>b;
        ed[a].insert(b);
        ed[b].insert(a);
        n1[a].push_back(b);
        n1[b].push_back(a);
    }
    for(int i=1;i<=m;i++){
        num[n1[i].size()].push_back(i);
        
    }
    ll sum=0;
    ll pla1=0,pla2=0;
    for(int i=2e5+1;i>=0;i--){
        if(num[i].size()!=0){
            pla1=i;
            if(pla1>pla2)swap(pla1,pla2);
        }
        if(sum+num[i].size()>=2){
            if(pla1==0){
                for(int j=0;j<num[pla2].size();j++){
                    for(int z=j+1;z<num[pla2].size();z++){
                        ll a=num[pla2][j],b=num[pla2][z];
                        if(!(ed[a].count(b))){
                            cout<<2*pla2-1<<'\n';
                            goto loop;
                        }
                    }
                }
                cout<<pla2*2-2<<'\n';
                goto loop;
            }
            else{
                ll a=num[pla2][0];
                for(int j=0;j<num[pla1].size();j++){
                    ll b=num[pla1][j];
                    if(!ed[a].count(b)){
                        cout<<pla2+pla1-1<<'\n';
                        goto loop;
                    }
                }
                cout<<pla2+pla1-2<<'\n';
                goto loop;
            }
            break;
        }
        sum+=num[i].size();
    }
    loop:for(int i=0;i<=m;i++){n1[i].clear();num[i].clear();ed[i].clear();}
}  
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
typedef long long  ll;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const int N=2e5+5;
vector<ll>n1[N];
set<ll>ed[N];
vector<ll>num[N];
void solve(){       
    ll m,mx=0;
    cin>>m;
    for(int i=1;i<m;i++){
        ll a,b;
        cin>>a>>b;
        ed[a].insert(b);
        ed[b].insert(a);
        n1[a].push_back(b);
        n1[b].push_back(a);
    }
    for(int i=1;i<=m;i++){
        num[n1[i].size()].push_back(i);
        mx=max(mx,(ll)n1[i].size());
    }
    ll sum=0;
    ll pla1=0,pla2=0;
    for(int i=mx;i>=0;i--){
        if(num[i].size()!=0){
            pla1=i;
            if(pla1>pla2)swap(pla1,pla2);
        }
        if(sum+num[i].size()>=2){
            if(pla1==0){
                for(int j=0;j<num[pla2].size();j++){
                    for(int z=j+1;z<num[pla2].size();z++){
                        ll a=num[pla2][j],b=num[pla2][z];
                        if(!(ed[a].count(b))){
                            cout<<2*pla2-1<<'\n';
                            goto loop;
                        }
                    }
                }
                cout<<pla2*2-2<<'\n';
                goto loop;
            }
            else{
                ll a=num[pla2][0];
                for(int j=0;j<num[pla1].size();j++){
                    ll b=num[pla1][j];
                    if(!ed[a].count(b)){
                        cout<<pla2+pla1-1<<'\n';
                        goto loop;
                    }
                }
                cout<<pla2+pla1-2<<'\n';
                goto loop;
            }
            break;
        }
        sum+=num[i].size();
    }
    loop:for(int i=0;i<=m;i++){n1[i].clear();num[i].clear();ed[i].clear();}
}  
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}