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

Solution 1 (runtime: 468 ms):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fastio std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl '\n'
const ll mod = 998244353;
const int N = 2e5+10;
int head[200005],flag,du[200005];
struct node {
	int to,next;
} p[400005];
void add(int x,int y) {
	p[++flag].to=y;
	p[flag].next=head[x];
	head[x]=flag;
}
struct node1 {
	int a,b;
	friend bool operator <(node1 a,node1 b) {
		return a.b>b.b;
	}
};
int main() {
	//fastio;
	int T;
	cin>>T;
	while(T--) {
		int n;
		cin>>n;
		flag=0;
		for(int i=1; i<=n; i++) du[i]=0,head[i]=0;
		int mx1=0,num1=0,mx2=0,num2=0;
		for(int i=1; i<=n-1; i++) {
			int a,b;
			cin>>a>>b;
			add(a,b);
			add(b,a);
			du[a]++;
			du[b]++;
		}
		for(int i=1;i<=n;i++){
			if(du[i]>mx1) {
				mx2=mx1;
				mx1=du[i];
			}
			else if(du[i]>mx2) mx2=du[i]; 
		}
		for(int i=1;i<=n;i++){
			if(du[i]==mx1)num1++;
			else if(du[i]==mx2) num2++;
		}
		int ans=0;
		if(num1==1) {
			for(int i=1; i<=n; i++) {
				if(du[i]!=mx1) continue;
				int temp=num2;
				for(int t=head[i]; t; t=p[t].next) {
					if(du[p[t].to]==mx2) temp--;
				}
				if(temp==0) ans=mx1+mx2-2;
				else ans=mx1+mx2-1;
			}
			
		}
		else{
			for(int i=1; i<=n; i++) {
				if(du[i]!=mx1) continue;
				int temp=num1;
				ans=mx1+mx1-2;
				for(int t=head[i]; t; t=p[t].next) {
					if(du[p[t].to]==mx1) temp--;
				}
				if(temp!=1){
					ans++;
					break;
				}
			}
		}
		

		cout<<ans<<endl;
	}
	return 0;
}


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