Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
// Problem: C. Remove Exactly Two
// Contest: Codeforces - Codeforces Round 1000 (Div. 2)
// URL: https://codeforces.com/contest/2063/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define endl '\n'
#define pb push_back
#define mod 1000000007

ll power(ll x, ll y) 
{ 
    if (y == 0) 
        return 1; 
    ll p = power(x, y/2) % mod; 
    p = (p * p) % mod; 
  
    return (y%2 == 0)? p : (x * p) % mod; 
} 
ll factorial[1000007];
void fact(ll n)
{
    memset(factorial,0,sizeof(factorial));
    factorial[0]=1;
	factorial[1]=1;

    for(int i=1;i<n;i++)
    {
        factorial[i]=(factorial[i-1]*i)%mod;
    } 
}
ll ncr(ll n,ll r)
{
    ll ans=1;
    ans=factorial[n];
    ll ans1=power(factorial[n-r],mod-2);
    ll ans2=power(factorial[r],mod-2);
    ans=((ans*ans1)%mod*ans2)%mod;
    return ans;
}
ll gcd(ll a, ll b) 
{ 
    if (b == 0) 
        return a; 
    return gcd(b, a % b);  
      
} 

bool prime[100000006];
void sieve(ll n){
    memset(prime,true,sizeof(prime));
	prime[1]=false; 
    for (ll p=2;p*p<=n;p++){
        if (prime[p]){  
            for (ll i=p*p;i<=n;i+=p)
                prime[i]=false;
        }
    }
}

// string s=bitset<64>(n).to_string();

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		map<ll,set<ll> > mp;
		vector<ll> degree(n+1,0);
		for(int i=0;i<n-1;i++)
		{
			ll u,v;
			cin>>u>>v;
			mp[u].insert(v);
			mp[v].insert(u);
			degree[u]++;
			degree[v]++;
		}
		ll ans = 0;
		// traverse over all the edges for degree[u]+degree[v] - 2
		for(auto it1:mp)
		{
			for(const auto &it2 : it1.second)
			{
				// cout<<it2<<endl;
				ans = max(ans,degree[it1.first]+degree[it2]-2);
			}
		}
		vector<pair<ll,ll> > deg;
		for(int i=1;i<=n;i++)
		{
			deg.pb(make_pair(degree[i],i));
		}
		sort(deg.begin(),deg.end());
		// check all for non adj
		for(int i = 0;i<deg.size();i++)
		{
			for(int j = i+1;j<deg.size();j++)
			{
				if(mp[deg[i].second].find(deg[j].second)==mp[deg[i].second].end())
				{
					ans = max(ans,deg[i].first+deg[j].first-1);
					break;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


Submission (verdict: Accepted):
// Problem: C. Remove Exactly Two
// Contest: Codeforces - Codeforces Round 1000 (Div. 2)
// URL: https://codeforces.com/contest/2063/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define endl '\n'
#define pb push_back
#define mod 1000000007

ll power(ll x, ll y) 
{ 
    if (y == 0) 
        return 1; 
    ll p = power(x, y/2) % mod; 
    p = (p * p) % mod; 
  
    return (y%2 == 0)? p : (x * p) % mod; 
} 
ll factorial[1000007];
void fact(ll n)
{
    memset(factorial,0,sizeof(factorial));
    factorial[0]=1;
	factorial[1]=1;

    for(int i=1;i<n;i++)
    {
        factorial[i]=(factorial[i-1]*i)%mod;
    } 
}
ll ncr(ll n,ll r)
{
    ll ans=1;
    ans=factorial[n];
    ll ans1=power(factorial[n-r],mod-2);
    ll ans2=power(factorial[r],mod-2);
    ans=((ans*ans1)%mod*ans2)%mod;
    return ans;
}
ll gcd(ll a, ll b) 
{ 
    if (b == 0) 
        return a; 
    return gcd(b, a % b);  
      
} 

bool prime[100000006];
void sieve(ll n){
    memset(prime,true,sizeof(prime));
	prime[1]=false; 
    for (ll p=2;p*p<=n;p++){
        if (prime[p]){  
            for (ll i=p*p;i<=n;i+=p)
                prime[i]=false;
        }
    }
}

// string s=bitset<64>(n).to_string();

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		map<ll,set<ll> > mp;
		vector<ll> degree(n+1,0);
		for(int i=0;i<n-1;i++)
		{
			ll u,v;
			cin>>u>>v;
			mp[u].insert(v);
			mp[v].insert(u);
			degree[u]++;
			degree[v]++;
		}
		ll ans = 0;
		// traverse over all the edges for degree[u]+degree[v] - 2
		for(auto it1:mp)
		{
			for(const auto &it2 : it1.second)
			{
				// cout<<it2<<endl;
				ans = max(ans,degree[it1.first]+degree[it2]-2);
			}
		}
		vector<pair<ll,ll> > deg;
		for(int i=1;i<=n;i++)
		{
			deg.pb(make_pair(degree[i],i));
		}
		sort(deg.begin(),deg.end(),greater<pair<ll,ll> >());
		// check all for non adj
		for(int i = 0;i<deg.size();i++)
		{
			for(int j = i+1;j<deg.size();j++)
			{
				if(mp[deg[i].second].find(deg[j].second)==mp[deg[i].second].end())
				{
					ans = max(ans,deg[i].first+deg[j].first-1);
					break;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}