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

Previous submission (verdict: Wrong answer on test 1):
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <complex>
#include <string> 
#include <cstring>
#include <sstream>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <utility>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <tuple>
#include <random>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int modn = 1000000007;
const int maxn = 2e6;
const long double eps = 1e-8;

struct node {
	int x, y, z;
};

int _;
int n, m;
vector<int>a;
ll deg[maxn];
vector<int>g[maxn];

int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> _;
	while (_--) {
		cin >> n;
		for (int i = 1;i <= n;++i) {
			deg[i] = 0;
			g[i].clear();
		}
		for (int i = 1, u, v;i < n;++i) {
			cin >> u >> v;
			g[u].push_back(v);
			g[v].push_back(u);
			deg[u] += 1;
			deg[v] += 1;
		}
		ll ans = 1;
		ll ma = 0, mp = 0;
		for (int i = 1;i <= n;++i) {
			if (deg[i] > ma) {
				ma = deg[i];
				mp = i;
			}
		}
		if (ma == 1) {
			cout << 0 << '\n';
			continue;
		}
		ans += ma - 1;
		for (auto x : g[mp]) {
			deg[x] -= 1;
		}
		deg[mp] = -deg[mp];
		ma = 0;
		for (int i = 1;i <= n;++i) {
			if (deg[i] > ma) {
				ma = deg[i];
			}
		}
		ans += ma - 1;
		for (auto x : g[mp]) {
			deg[x] += 1;
		}
		deg[mp] = -deg[mp];
		ll Ans = 1;
		ll Ma = 0, Mp = 0;
		for (int i = 1;i <= n;++i) {
			if (i == mp) { continue; }
			if (deg[i] > Ma) {
				Ma = deg[i];
				Mp = i;
			}
		}
		if (Ma == 1) {
			cout << 0 << '\n';
			continue;
		}
		Ans += Ma - 1;
		for (auto x : g[Mp]) {
			deg[x] -= 1;
		}
		deg[Mp] = 0;
		Ma = 0;
		for (int i = 1;i <= n;++i) {
			if (deg[i] > Ma) {
				Ma = deg[i];
			}
		}
		Ans += Ma - 1;
		cout << max(ans, Ans) << '\n';
	}
	return 0;
}

Submission (verdict: Accepted):
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <complex>
#include <string> 
#include <cstring>
#include <sstream>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <utility>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <tuple>
#include <random>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int modn = 1000000007;
const int maxn = 2e6;
const long double eps = 1e-8;

struct node {
	int x, y, z;
};

int _;
int n, m;
vector<int>a;
ll deg[maxn];
vector<int>g[maxn];

int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> _;
	while (_--) {
		cin >> n;
		for (int i = 1;i <= n;++i) {
			deg[i] = 0;
			g[i].clear();
		}
		for (int i = 1, u, v;i < n;++i) {
			cin >> u >> v;
			g[u].push_back(v);
			g[v].push_back(u);
			deg[u] += 1;
			deg[v] += 1;
		}
		ll ans = 1;
		ll ma = 0, mp = 0;
		for (int i = 1;i <= n;++i) {
			if (deg[i] > ma) {
				ma = deg[i];
				mp = i;
			}
		}
		if (ma == 1) {
			cout << 0 << '\n';
			continue;
		}
		ans += ma - 1;
		for (auto x : g[mp]) {
			deg[x] -= 1;
		}
		deg[mp] = -deg[mp];
		ma = 0;
		for (int i = 1;i <= n;++i) {
			if (deg[i] > ma) {
				ma = deg[i];
			}
		}
		ans += ma - 1;
		for (auto x : g[mp]) {
			deg[x] += 1;
		}
		deg[mp] = -deg[mp];
		ll Ans = 1;
		ll Ma = 0, Mp = 0;
		for (int i = 1;i <= n;++i) {
			if (i == mp) { continue; }
			if (deg[i] > Ma) {
				Ma = deg[i];
				Mp = i;
			}
		}
		Ans += Ma - 1;
		for (auto x : g[Mp]) {
			deg[x] -= 1;
		}
		deg[Mp] = 0;
		Ma = 0;
		for (int i = 1;i <= n;++i) {
			if (deg[i] > Ma) {
				Ma = deg[i];
			}
		}
		Ans += Ma - 1;
		cout << max(ans, Ans) << '\n';
	}
	return 0;
}