Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
using ll=long long int;

void fastio(){ios::sync_with_stdio(false);cin.tie(0);}

vector<ll> take_inputs_0(ll n){vector<ll> v(n);for(int i=0;i<n;i++){cin>>v[i];}return v;}
vector<ll> take_inputs_1(ll n){vector<ll> v(n);for(int i=1;i<n;i++){cin>>v[i];}return v;}

void show(auto x){ cout<<x<<"\n";}
void show1(auto v){ ll n=(signed)v.size();for(int i=0;i<n;i++){cout<<v[i]<<" ";}cout<<"\n";}
void show2(auto v){ ll n=(signed)v.size();for(int i=0;i<n;i++){	cout<<v[i]<<"\n";}}
void showset(set<ll> st){for(auto x:st){cout<<x<<" ";}cout<<"\n";}

void sort_it(auto &v){ sort(v.begin(),v.end());}
void reverse_it(auto &v){ reverse(v.begin(),v.end());}
void do_both(auto &v){ sort(v.begin(),v.end());reverse(v.begin(),v.end());}

auto find_max(auto v){ll maxi=-5e18;for(int i=0;i<(signed)v.size();i++){if(v[i]>maxi){maxi=v[i];}}return maxi;}
auto find_min(auto v){ll mini=5e18;for(int i=0;i<(signed)v.size();i++){if(v[i]<mini){mini=v[i];}}return mini;}

vector<ll> bitRep64(ll n){vector<ll> v;int i=0;while(i<=63){if(n&(1LL<<i)){v.push_back(1);}else{v.push_back(0);}i++;}return v;}
vector<ll> bitRep32(ll n){vector<ll> v;int i=0;while(i<=31){if(n&(1LL<<i)){v.push_back(1);}else{v.push_back(0);}i++;}return v;}
vector<string> all_subset(ll n){vector<string> v;for(int i=0;i<(1<<n);i++){string st="";for(int j=0;j<n;j++){if(i&(1<<j)){st+="1";}else{st+="0";}}v.push_back(st);}return v;}

vector<ll> prefix_sum_0(vector<ll> v){ll n=(signed)v.size();for(int i=1;i<n;i++){v[i]=v[i-1]+v[i];}return v;}
vector<ll> prefix_sum_1(vector<ll> v){ll n=(signed)v.size();vector<ll> summ(n+1);for(int i=1;i<=n;i++){summ[i]=summ[i-1]+v[i-1];}return summ;}

vector<ll> suffix_sum_0(vector<ll> v){ll n=(signed)v.size();vector<ll> suff(n);suff[n-1]=v[n-1];for(int i=n-2;i>=0;i--){suff[i]=suff[i+1]+v[i];}return suff;}
vector<ll> suffix_sum_1(vector<ll> v){ll n=(signed)v.size();vector<ll> suff(n+1);suff[n]=v[n-1];for(int i=n-1;i>=1;i--){suff[i]=suff[i+1]+v[i-1];}return suff;}

ll find_total(vector<ll> v){ll sum=0;ll n=(signed)v.size();for(int i=0;i<n;i++){sum+=v[i];}return sum;}

///////////////////////////////////

void dfs(ll n, const vector< set<ll> > &adj,vector<ll> &vis)
{
	vis[n]=1;
	
	for(auto x:adj[n])
	{
		if(!vis[x])
		{
			dfs(n,adj,vis);
		}
	}
}

int main()
{
	int t;cin>>t;
	
	while(t--)
	{
		ll n;cin>>n;
		
		vector< set<ll> > adj(n+1);
		
		for(int i=0;i<n-1;i++)
		{
			ll x,y;cin>>x>>y;
			
			adj[x].insert(y);
			adj[y].insert(x);
		}
		
		vector< pair<ll,ll> > p;
		
		for(int i=1;i<=n;i++)
		{
			if(adj[i].size()>0)
			{
				p.push_back({adj[i].size(),i});
			}
		}
		
		do_both(p);
		
		ll node=p[0].second;
		
		ll tmp1=node;
		
		vector<ll> tmp;
		for(auto x:adj[node])
		{
			tmp.push_back(x);
		}
		
		for(int i=0;i<(signed)tmp.size();i++)
		{
			ll x=tmp[i];
			adj[x].erase(node);
			adj[node].erase(x);
		}
		
		/////////////
		
		p={};
		tmp={};
		
		for(int i=1;i<=n;i++)
		{
			if(i!=tmp1)
			{
				p.push_back({adj[i].size(),i});
			}
		}
		
		ll tmp2=0;
		
		if(p.size()>0)
		{
		do_both(p);
		
		node=p[0].second;
		
		tmp2=node;
		
		
		for(auto x:adj[node])
		{
			tmp.push_back(x);
		}
		
		for(int i=0;i<(signed)tmp.size();i++)
		{
			ll x=tmp[i];
			adj[x].erase(node);
			adj[node].erase(x);
		}
		
		}
		vector<ll> vis(n+1);
		
		ll cnt=0;
		
		for(int i=1;i<=n;i++)
		{
			if(vis[i]!=1 && tmp1!=i && tmp2!=i)
			{
				cnt++;
				vis[i]=1;
			if(adj[i].size()>0)
			{
				for(auto x:adj[i])
				{
					vis[x]=1;
				}
			}
			}
		}	
		
		show(cnt);
		
	}
	
	return 0;
}

Submission (verdict: Wrong answer on test 4):
#include<bits/stdc++.h>
using namespace std;
using ll=long long int;

void fastio(){ios::sync_with_stdio(false);cin.tie(0);}

vector<ll> take_inputs_0(ll n){vector<ll> v(n);for(int i=0;i<n;i++){cin>>v[i];}return v;}
vector<ll> take_inputs_1(ll n){vector<ll> v(n);for(int i=1;i<n;i++){cin>>v[i];}return v;}

void show(auto x){ cout<<x<<"\n";}
void show1(auto v){ ll n=(signed)v.size();for(int i=0;i<n;i++){cout<<v[i]<<" ";}cout<<"\n";}
void show2(auto v){ ll n=(signed)v.size();for(int i=0;i<n;i++){	cout<<v[i]<<"\n";}}
void showset(set<ll> st){for(auto x:st){cout<<x<<" ";}cout<<"\n";}

void sort_it(auto &v){ sort(v.begin(),v.end());}
void reverse_it(auto &v){ reverse(v.begin(),v.end());}
void do_both(auto &v){ sort(v.begin(),v.end());reverse(v.begin(),v.end());}

auto find_max(auto v){ll maxi=-5e18;for(int i=0;i<(signed)v.size();i++){if(v[i]>maxi){maxi=v[i];}}return maxi;}
auto find_min(auto v){ll mini=5e18;for(int i=0;i<(signed)v.size();i++){if(v[i]<mini){mini=v[i];}}return mini;}

vector<ll> bitRep64(ll n){vector<ll> v;int i=0;while(i<=63){if(n&(1LL<<i)){v.push_back(1);}else{v.push_back(0);}i++;}return v;}
vector<ll> bitRep32(ll n){vector<ll> v;int i=0;while(i<=31){if(n&(1LL<<i)){v.push_back(1);}else{v.push_back(0);}i++;}return v;}
vector<string> all_subset(ll n){vector<string> v;for(int i=0;i<(1<<n);i++){string st="";for(int j=0;j<n;j++){if(i&(1<<j)){st+="1";}else{st+="0";}}v.push_back(st);}return v;}

vector<ll> prefix_sum_0(vector<ll> v){ll n=(signed)v.size();for(int i=1;i<n;i++){v[i]=v[i-1]+v[i];}return v;}
vector<ll> prefix_sum_1(vector<ll> v){ll n=(signed)v.size();vector<ll> summ(n+1);for(int i=1;i<=n;i++){summ[i]=summ[i-1]+v[i-1];}return summ;}

vector<ll> suffix_sum_0(vector<ll> v){ll n=(signed)v.size();vector<ll> suff(n);suff[n-1]=v[n-1];for(int i=n-2;i>=0;i--){suff[i]=suff[i+1]+v[i];}return suff;}
vector<ll> suffix_sum_1(vector<ll> v){ll n=(signed)v.size();vector<ll> suff(n+1);suff[n]=v[n-1];for(int i=n-1;i>=1;i--){suff[i]=suff[i+1]+v[i-1];}return suff;}

ll find_total(vector<ll> v){ll sum=0;ll n=(signed)v.size();for(int i=0;i<n;i++){sum+=v[i];}return sum;}

///////////////////////////////////

// 12:48 pm

int main()
{
	int t;cin>>t;
	
	while(t--)
	{
		ll n;cin>>n;
		
		vector< set<ll> > adj(n+1);
		
		vector< pair<ll,ll> > pairs;
		
		for(int i=0;i<n-1;i++)
		{
			ll x,y;cin>>x>>y;
			
			adj[x].insert(y);
			adj[y].insert(x);
			
			pairs.push_back({x,y});
		}
		
		vector< pair<ll,ll> > p;
		
		for(int i=1;i<=n;i++)
		{
			p.push_back({adj[i].size(),i});
		}
		
		do_both(p);
		
		ll ans=0;
		
		ll tmp=p[0].second;
		
		for(int i=1;i<(signed)p.size();i++)
		{
			if(adj[p[i].second].find(tmp)==adj[p[i].second].end())
			{
				ans+=p[0].first+p[i].first-1;
				break;
			}
		}
		
		for(int i=0;i<n-1;i++)
		{
			ll x=pairs[i].first;
			ll y=pairs[i].second;
			
			ll temp= adj[x].size()  + adj[y].size() -2;
			
			ans=max(ans,temp);
		}
		
		show(ans);
	}
	
	return 0;
}