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

Previous submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
#define fi first
#define se second
#define sajz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll x, ll y) {return uniform_int_distribution<ll>(x, y)(rng);}

void solve(){
	int n; cin >> n;
	vector g(n+1, vector<int>()), dp(n+1, vector<int>(3));
	vector<int> deg(n+1);
	for (int i=0; i<n-1; i++){
		int x, y; cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
		deg[x]++;
		deg[y]++;
	}
	int res = 0;
	function<void(int,int)> dfs = [&](int v, int par){
		pair<int,int> mx[2] = {{0, 0}, {0, 0}};
		for (auto u : g[v]) if (u != par){
			dfs(u, v);
			res = max(res, dp[u][0] + deg[v] - 2);
			res = max(res, dp[u][1] + deg[v] - 1);
			dp[v][0] = max(dp[v][0], deg[v]);
			dp[v][1] = max({dp[v][1], dp[u][0], dp[u][1]});
			dp[v][2] = max({dp[v][2], dp[u][1], dp[u][2]});
			if (dp[u][0] >= mx[0].fi){
				mx[1] = mx[0];
				mx[0] = {dp[u][0], u};
			}else if (dp[u][0] > mx[1].fi) mx[1] = {dp[u][0], u};
		}
		res = max(res, mx[0].fi + mx[1].fi - 1);
		for (auto u : g[v]) if (u != par){
			if (u != mx[0].se) res = max(res, mx[0].fi + dp[u][1] - 1);
			else res = max(res, mx[1].fi + dp[u][1] - 1);
		}
	};
	dfs(1, 0);
	//for (int i=1; i<=n; i++) cout << i << ": " << dp[i][0] << ' ' << dp[i][1] << ' ' << dp[i][2] << '\n';
	cout << res << '\n';
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int tc; cin >> tc;
	while (tc--) solve();
	return 0;
}
/*

1
8
1 2
1 3
2 4
4 5
1 6
5 7
5 8


*/


Submission (verdict: Accepted):
#include <bits/stdc++.h>
#define fi first
#define se second
#define sajz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll x, ll y) {return uniform_int_distribution<ll>(x, y)(rng);}

void solve(){
	int n; cin >> n;
	vector g(n+1, vector<int>()), dp(n+1, vector<int>(3));
	vector<int> deg(n+1);
	for (int i=0; i<n-1; i++){
		int x, y; cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
		deg[x]++;
		deg[y]++;
	}
	int res = 0;
	function<void(int,int)> dfs = [&](int v, int par){
		pair<int,int> mx[2] = {{0, 0}, {0, 0}};
		for (auto u : g[v]) if (u != par){
			dfs(u, v);
			res = max(res, dp[u][0] + deg[v] - 2);
			res = max(res, dp[u][1] + deg[v] - 1);
			dp[v][0] = max({dp[v][0], deg[v], dp[u][0]});
			dp[v][1] = max({dp[v][1], dp[u][0], dp[u][1]});
			dp[v][2] = max({dp[v][2], dp[u][1], dp[u][2]});
			if (dp[u][0] >= mx[0].fi){
				mx[1] = mx[0];
				mx[0] = {dp[u][0], u};
			}else if (dp[u][0] > mx[1].fi) mx[1] = {dp[u][0], u};
		}
		res = max(res, mx[0].fi + mx[1].fi - 1);
		for (auto u : g[v]) if (u != par){
			if (u != mx[0].se) res = max(res, mx[0].fi + dp[u][1] - 1);
			else res = max(res, mx[1].fi + dp[u][1] - 1);
		}
	};
	dfs(1, 0);
	//for (int i=1; i<=n; i++) cout << i << ": " << dp[i][0] << ' ' << dp[i][1] << ' ' << dp[i][2] << '\n';
	cout << res << '\n';
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int tc; cin >> tc;
	while (tc--) solve();
	return 0;
}
/*

1
10
1 2
1 3
3 4
2 5
5 6
5 7
4 8
5 9
4 10



*/