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;
typedef long long  ll;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const int N=2e5+5;
vector<ll>n1[N];
set<ll>vis;
ll pla,pla1;
void bfs(ll now,ll fa){
    vis.insert(now);
    for(auto num:n1[now]){
        if(num==fa || num==pla || num==pla1)continue;
        bfs(num,now);
    }
}
void solve(){
    vis.clear();
    ll m,mx=-1;
    pla=0;
    cin>>m;
    for(int i=1;i<m;i++){
        ll a,b;
        cin>>a>>b;
        n1[a].push_back(b);
        n1[b].push_back(a);
        if((ll)n1[a].size()>mx){
            mx=n1[a].size();
            pla=a;
        }
        if((ll)n1[b].size()>mx){
            mx=n1[b].size();
            pla=b;
        }
    }
    // cout<<mx<<'\n';
    //mx删掉
    ll mx1=-1;
    pla1=0;
    for(int i=1;i<=m;i++){
        if(i==pla)continue;
        ll number=0;
        for(auto num:n1[i]){
            if(num!=pla)number++;
        }
        if(number>mx1){
            mx1=number;
            pla1=i;
        }
    }
    
    ll res=0;
    for(int i=1;i<=m;i++){
        if(i==pla || i==pla1)continue;
        if(!vis.count(i)){
            // cout<<i<<'\n';
            res++;
            bfs(i,i);
        }
    }
    cout<<res<<'\n';
    for(int i=1;i<=m;i++)n1[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: 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;
}