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

Previous 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;
}

Submission (verdict: Accepted):
#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()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	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;
		
		
		for(int i=0;i<(signed)p.size()-1;i++)
		{
			ll tmp=p[i].second;
			for(int j=i+1;j<(signed)p.size();j++)
			{
			if(adj[p[j].second].find(tmp)==adj[p[j].second].end())
			{
				ans=max(ans,p[i].first+p[j].first-1);
				break;
			}
			}
		}
		
		for(int i=0;i<(signed)pairs.size();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;
}