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( )
{

}


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 ans = 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] ) ;
	}
	ans += mn ;
	cout << ans ;
}	

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: 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 tmp = center(tree) ;
	if ( tmp == cur )
	{
		for ( int i = 1 ; i <= n ; i++ )
		{
			if ( dig[i] == ma && i != tmp )
			{
				cur = i ;
			}
		}
	}

	int ans = 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] ) ;
	}
	ans += mn ;
	cout << ans ;
}	

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