Problem: 2063C - Remove Exactly Two
Language: c++17 

Code 1:
#include <bits/stdc++.h>
#define all(s) s.begin(), s.end()
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int _N = 1e5 + 5;
ll n;

void check(ll v, vector<set<ll>> &adj){
    for(ll i=1; i<adj.size(); i++){
        if(adj[i].find(v)!=adj[i].end()){
            adj[i].erase(v);
        }
    }
}

void dfs(ll node, vector<set<ll>> &adj, vector<ll> &vis){
    vis[node]=1;
    for(auto i: adj[node]){
        if(vis[i]) continue;
        dfs(i,adj,vis);
    }
}

int main(){
	int t;
	cin>>t;
	while(t--){
		cin>>n;
        ll ans1,ans2;
        ll maxi=0;
		vector<set<ll>> adj(n+1);
        set<pair<ll,ll>> st;
        for(ll i=0; i<n-1; i++){
            ll u,v;
            cin>>u>>v;
            st.insert({u,v});
            adj[u].insert(v);
            adj[v].insert(u);
        }

        for(ll i=1; i<=n; i++){
            if(adj[i].size()>maxi){
                maxi=adj[i].size();
            }
        }

        vector<ll> v;
        for(ll i=1; i<=n; i++){
            if(adj[i].size()==maxi) v.push_back(i);
        }

        if(v.size()>2){
            ll x=v[0],y=v[1],z=v[2];
            if(st.find({x,y})==st.end()){
                ans1=x;
                ans2=y;
            }
            if(st.find({y,z})==st.end()){
                ans1=y;
                ans2=z;
            }
            if(st.find({x,z})==st.end()){
                ans1=x;
                ans2=z;
            }
            check(ans1,adj);
            check(ans2,adj);
        }
        if(v.size()==2){
            ans1=v[0],ans2=v[1];
            check(ans1,adj);
            check(ans2,adj);
        }
        if(v.size()==1){
            ans1=v[0];
            check(ans1,adj);
            maxi=0;
            for(ll i=1; i<=n; i++){
                if(i==ans1) continue;
                if(adj[i].size()>maxi){
                    maxi=adj[i].size();
                }
            }
            for(ll i=1; i<=n; i++){
                if(adj[i].size()==maxi) ans2=i;
            }
            check(ans2,adj);
        }

        ll ans=0;
        vector<ll> vis(n+1,0);
        vis[ans1]=1;
        vis[ans2]=1;
        for(ll i=1; i<=n; i++){
            if(!vis[i]){
                dfs(i,adj,vis);
                ans++;
            }
        }

        cout<<ans<<endl;  
	}
}

Code 2:
#include <bits/stdc++.h>
#define all(s) s.begin(), s.end()
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int _N = 1e5 + 5;
ll n;

void check(ll v, vector<set<ll>> &adj){
    for(ll i=1; i<adj.size(); i++){
        if(adj[i].find(v)!=adj[i].end()){
            adj[i].erase(v);
        }
    }
}

void dfs(ll node, vector<set<ll>> &adj, vector<ll> &vis){
    vis[node]=1;
    for(auto i: adj[node]){
        if(vis[i]) continue;
        dfs(i,adj,vis);
    }
}

int main(){
	int t;
	cin>>t;
	while(t--){
		cin>>n;
        ll ans1,ans2;
        ll maxi=0;
		vector<set<ll>> adj(n+1);
        set<pair<ll,ll>> st;
        for(ll i=0; i<n-1; i++){
            ll u,v;
            cin>>u>>v;
            st.insert({u,v});
            adj[u].insert(v);
            adj[v].insert(u);
        }

        for(ll i=1; i<=n; i++){
            if(adj[i].size()>maxi){
                maxi=adj[i].size();
            }
        }

        vector<ll> v;
        for(ll i=1; i<=n; i++){
            if(adj[i].size()==maxi) v.push_back(i);
        }

        if(v.size()>2){
            ll x=v[0],y=v[1],z=v[2];
            if(st.find({x,y})==st.end() && st.find({y,x})==st.end()){
                ans1=x;
                ans2=y;
            }
            if(st.find({y,z})==st.end() && st.find({z,y})==st.end()){
                ans1=y;
                ans2=z;
            }
            if(st.find({x,z})==st.end() && st.find({z,x})==st.end()){
                ans1=x;
                ans2=z;
            }
        }
        else if(v.size()==2){
            ans1=v[0],ans2=v[1];
        }
        else{
            ans1=v[0];
            check(ans1,adj);
            maxi=0;
            vector<ll> temp;
            for(ll i=1; i<=n; i++){
                if(i==ans1) continue;
                if(adj[i].size()>=maxi){
                    maxi=adj[i].size();
                }
            }
            for(ll i=1; i<=n; i++){
                if(adj[i].size()==maxi && i!=ans1) temp.push_back(i);
            }
            ans2=temp[0];
            for(ll i=0; i<temp.size(); i++){
                if(st.find({ans1,temp[i]})==st.end() && st.find({temp[i],ans1})==st.end()){
                    ans2=temp[i];
                    break;
                }
            }
        }
        check(ans1,adj);
        check(ans2,adj);

        ll ans=0;
        vector<ll> vis(n+1,0);
        vis[ans1]=1;
        vis[ans2]=1;
        for(ll i=1; i<=n; i++){
            if(!vis[i]){
                dfs(i,adj,vis);
                ans++;
            }
        }

        cout<<ans<<endl;  
	}
}

Answer: 2