Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define range(i, a, b, s) for (int i = a; (s > 0 && i < (b)) || (s < 0 && i > (b)); i += s)
#define trav(a, x) for (auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;

template <typename T>
void print_arr(vector<T> a) {
	trav(x, a) cout << to_string(x) << " ";
	cout << "\n";
}

int T;
int n;
vector<set<int>> g;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> T;
	while (T--) {
		cin >> n;
		g = vector<set<int>>(n);
		rep(i, 0, n-1) {
			int u, v;
			cin >> u >> v;
			u--; v--;
			g[u].insert(v);
			g[v].insert(u);
		}

		if (n == 2) {
			cout << 0 << '\n';
			continue;
		}

		vpi pairs(n);
		rep(i, 0, n) {
			pairs[i] = pii(sz(g[i]), i);
		}

		sort(all(pairs));
		// 1. 3 of the highest value (find two not connected)
		// 2. 2 of the highest value (take both)
		// 3. 1 of the highest value (check if there's a value 
		//    among the next highest that doesn't overlap)

		if (pairs[n-1].first == pairs[n-2].first && pairs[n-1].first == pairs[n-3].first) {
			cout << pairs[n-1].first * 2 << '\n';
			continue;
		}

		if (pairs[n-1].first == pairs[n-2].first) {
			int u = pairs[n-1].second;
			int v = pairs[n-2].second;
			cout << sz(g[u]) * 2 - (g[u].count(v) ? 1 : 0) - 1 << '\n';
			continue;
		}

		bool found = false;
		int cnt = pairs[n-2].first;
		rep(i, 0, n-1) {
			int v = pairs[n-1].second;
			if (sz(g[i]) == cnt && g[v].count(i) == 0) {
				found = true;
				break;
			}
		}

		//cout << "last " << pairs[n-1].first << " " << cnt << " " << found << '\n';
		cout << pairs[n-1].first + cnt - (found ? 0 : 1) - 1 << '\n';
	}

	return 0;
}


Submission (verdict: Wrong answer on test 4):
#include<bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define range(i, a, b, s) for (int i = a; (s > 0 && i < (b)) || (s < 0 && i > (b)); i += s)
#define trav(a, x) for (auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;

template <typename T>
void print_arr(vector<T> a) {
	trav(x, a) cout << to_string(x) << " ";
	cout << "\n";
}

int T;
int n;
vector<set<int>> g;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> T;
	while (T--) {
		cin >> n;
		g = vector<set<int>>(n);
		rep(i, 0, n-1) {
			int u, v;
			cin >> u >> v;
			u--; v--;
			g[u].insert(v);
			g[v].insert(u);
		}

		if (n == 2) {
			cout << 0 << '\n';
			continue;
		}

		vpi pairs(n);
		rep(i, 0, n) {
			pairs[i] = pii(sz(g[i]), i);
		}

		sort(all(pairs));
		// 1. 3 of the highest value (find two not connected)
		// 2. 2 of the highest value (take both)
		// 3. 1 of the highest value (check if there's a value 
		//    among the next highest that doesn't overlap)

		if (pairs[n-1].first == pairs[n-2].first && pairs[n-1].first == pairs[n-3].first) {
			cout << pairs[n-1].first * 2 - 1 << '\n';
			continue;
		}

		if (pairs[n-1].first == pairs[n-2].first) {
			int u = pairs[n-1].second;
			int v = pairs[n-2].second;
			cout << sz(g[u]) * 2 - (g[u].count(v) ? 1 : 0) - 1 << '\n';
			continue;
		}

		bool found = false;
		int cnt = pairs[n-2].first;
		rep(i, 0, n-1) {
			int v = pairs[n-1].second;
			if (sz(g[i]) == cnt && g[v].count(i) == 0) {
				found = true;
				break;
			}
		}

		//cout << "last " << pairs[n-1].first << " " << cnt << " " << found << '\n';
		cout << pairs[n-1].first + cnt - (found ? 0 : 1) - 1 << '\n';
	}

	return 0;
}