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/problemset/problem/2063/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>

#define NoTLE ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ld long double
#define ll long long
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define nl '\n'
#define YES cout << "YES"
#define NO cout << "NO"
#define fx(i) fixed<<setprecision(i)

using namespace std;

const int MOD = 1e9+7 ;
const int MOD2 = 998244353 ;
const ld eps = 1e-10;
const ld pi = acos(-1) ;

void noBrainer () 
{

}

void new_test( )
{

}


int center( map < int , vector < int > > & tree )
{
	vector < int > leafs ;
	vector < int > dgr ( tree.size() + 1 );
	for ( auto el : tree )
	{
		dgr[el.F] = el.S.size() ;
	}
	for ( int i = 1 ; i <= tree.size() ; i++ )
	{
		if ( dgr[i] == 1 ) leafs.pb(i) ;
	}
	int ans = 2 ;
	while ( leafs.size() > 0 )
	{
		vector < int > new_leafs ;
		for ( auto el : leafs )
		{
			for ( auto nd : tree[el] )
			{
				dgr[nd] -- ;
				if ( dgr[nd] == 1 ) new_leafs.pb(nd) ;
			}
		}
		leafs = new_leafs ;
		if ( leafs.size() > 1 ) ans += 2 ;
		else if ( leafs.size() == 1 ) ans += 1 ;
	} 
	return leafs[0] ;
}

void solve()
{
	int n ; cin >> n ;
	map < int , vector < int > > tree ;
	vector < int > dig ( n + 1 ) ;
	int cur = 0 ;
	int ma = 0 ;
	for ( int i = 0 ; i < n - 1 ; i++ )
	{
		int a , b ; cin >> a >> b ;
		tree[a].pb(b) ;
		tree[b].pb(a) ;
		dig[a]++ ;
		dig[b]++ ;
		if ( ma < dig[a] )
		{
			ma = dig[a] ;
			cur = a ;
		}
		if ( ma < dig[b] )
		{
			ma = dig[b] ;
			cur = b ;
		}
	}
	int ans1 = ma - 1 ;
	for ( auto el : tree[cur] )
	{
		dig[el]-- ;
	}
	int mn = 0 ;
	for ( int i = 1 ; i <= n ; i++ )
	{
		if ( i != cur )
		mn = max ( mn , dig[i] ) ;
	}
	ans1 += mn ;
	int tmp = center(tree) ;
	int ans2 = ma - 1 ;
	if ( tmp == cur )
	{
		for ( auto el : tree[cur] )
		{
			dig[el]++ ;
		}
		mn = 0 ;
		for ( int i = 1 ; i <= n ; i++ )
		{
			if ( dig[i] == ma && i != tmp )
			{
				cur = i ;
			}
		}
		for ( auto el : tree[cur] )
		{
			dig[el]-- ;
		}
		for ( int i = 1 ; i <= n ; i++ )
		{
			if ( i != cur )
			mn = max ( mn , dig[i] ) ;
		}
		ans2 += mn ;
	}
	cout << max ( ans1 , ans2 ) ;
}	

signed main()
{
    NoTLE
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );
    int T = 1;
    cin >> T ;
    //int X = T;
    while( T-- )
    {
    	// int ctest = X-T ;
        //noBrainer() ;
        solve();
        cout << nl ;
    }
    return 0;
}

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

#include<bits/stdc++.h>

#define NoTLE ios_base::sync_with_stdio(false);cin.tie(NULL);

#define ld long double
#define ll long long
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define nl '\n'
#define YES cout << "YES"
#define NO cout << "NO"
#define fx(i) fixed<<setprecision(i)

using namespace std;

const int MOD = 1e9+7 ;
const int MOD2 = 998244353 ;
const ld eps = 1e-10;
const ld pi = acos(-1) ;

void noBrainer () 
{

}

void new_test( )
{

}


void center( map < int , vector < int > > & tree , vector < int >& lvl )
{
	vector < int > leafs ;
	vector < int > dgr ( tree.size() + 1 );
	for ( auto el : tree )
	{
		dgr[el.F] = el.S.size() ;
	}
	for ( int i = 1 ; i <= tree.size() ; i++ )
	{
		if ( dgr[i] == 1 ) leafs.pb(i) , lvl[i] = 1 ;
	}
	int ans = 2 ;
	while ( leafs.size() > 0 )
	{

		vector < int > new_leafs ;
		for ( auto el : leafs )
		{
			for ( auto nd : tree[el] )
			{
				dgr[nd] -- ;
				if ( dgr[nd] == 1 ) new_leafs.pb(nd) , lvl[nd] = lvl[el]+1 ;
			}
		}
		leafs = new_leafs ;
		if ( leafs.size() > 1 ) ans += 2 ;
		else if ( leafs.size() == 1 ) ans += 1 ;
	} 
}


void solve()
{
	int n ; cin >> n;
	map < int , vector < int > > tree ;
	vector < int > dgr ( n + 1 ) ;
	for ( int i = 1 ; i < n ; i++ )	
	{
		int a , b ; cin >> a >> b ;
		tree[a].pb(b) ;
		tree[b].pb(a) ;
		dgr[a]++ ;
		dgr[b]++ ;
	}

	vector < int > lvl ( n + 1 ) ;
	center( tree , lvl ) ;

	int mnl = 1e6 , mx = 0 ;
	int del = 0 ;
	for ( int i = 1 ; i <= n ; i++ )
	{
		if ( dgr[i] > mx )
		{
			mx = dgr[i] ;
			mnl = lvl[i] ;
			del = i ;
		}
		if ( dgr[i] == mx && mnl > lvl[i] )
		{
			mx = dgr[i] ;
			mnl = lvl[i] ;
			del = i ;
		}
	}
	int ans = mx - 1 ;
	for ( auto el : tree[del] )
	{
		dgr[el]-- ;
	}
	dgr[del] = 0 ;
	int t = 0 ;
	for ( int i = 1 ; i <= n ; i++ )
	{
		t = max ( t , dgr[i] ) ;
	}
	cout << ans + t ;
}	

signed main()
{
    NoTLE
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );
    int T = 1;
    cin >> T ;
    //int X = T;
    while( T-- )
    {
    	// int ctest = X-T ;
        //noBrainer() ;
        solve();
        cout << nl ;
    }
    return 0;
}