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

Solution 1 (runtime: 921 ms):
#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;  
	}
}

Solution 2 (runtime: 296 ms):
#include<bits/stdc++.h>
using namespace std;
inline int read(){
    int x=0,f=1;char c=getchar();
    while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
    while(isdigit(c)){x=(x<<3)+(x<<1)+(c^48);c=getchar();}
    return f==1?x:-1;
}
const int N=2e5+4;
int n,ans,deg[N],c[N];
vector<int>e[N];
inline int comp(int x,int y){return deg[x]>deg[y];}
inline void clear(){
    for(int i=1;i<=n;i++)e[i].clear();
    memset(deg,0,sizeof(int)*(n+1));
}
inline int calc(int x,int y){
    if(x==y) return 0;
    for(auto v:e[x])if(v==y)return deg[x]+deg[y]-2;
    return deg[x]+deg[y]-1;
}
int main(){
    for(int T=read();T--;){
        n=read();
        for(int i=1,u,v;i<n;i++){
            u=read();v=read();
            e[u].push_back(v); e[v].push_back(u);
            ++deg[u]; ++deg[v];
        }
        if(n==2){puts("0");clear();continue;}
        for(int i=1;i<=n;i++)c[i]=i;
        sort(c+1,c+n+1,comp);
        ans=max(calc(c[1],c[2]), max(calc(c[3],c[1]), calc(c[2],c[3])));
        for(int i=1;i<=n;i++) ans=max(ans, max(calc(i,c[1]),calc(i,c[2])));
        cout<<ans<<"\n";
        clear();
    }
    return (0-0);
}