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

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

#define pb push_back
#define sz(v) ((int)(v).size())

using namespace std;

class Task {
public:
	bool oneMore;
	set<int> touched;
	void run(int node, int from, map<int, vector<int>>& conn, int vip_node, set<int> gp) {
		for (int next_node : conn[node]) {
			if (gp.find(next_node) != gp.end()) {
				touched.insert(next_node);
			}
		}
	}
	void solveOne() {
		int l, x, y, result;
		touched.clear();
		cin >> l;
		map<int, vector<int>> conn;
		for (int i = 0; i < l-1; i++) {
			cin >> x >> y;
			conn[x].pb(y);
			conn[y].pb(x);
		}
		if (l <= 4) {
			cout << l-2 << "\n";
			return;
		}
		map<int, vector<int>> deg;
		for (int i = 1; i <= l; i++) {
			x = sz(conn[i]);
			deg[x-1].pb(i);
		}
		auto ptr = --deg.end();
		oneMore = false;
		if (sz((*ptr).second) >= 2) {
			result = (*ptr).first * 2;
			vector<int> gp_v = (*ptr).second;
			set<int> gp;
			for (int i : gp_v) {
				gp.insert(i);
			}
			run(gp_v[0], 0, conn, gp_v[0], gp);
			if (sz(touched) < sz(gp_v)) {
				oneMore = true;
			}
		}
		else {
			auto ptr2 = ptr;
			--ptr2;
			result = (*ptr).first + (*ptr2).first;
			x = (*ptr).second[0];
			vector<int> gp_v = (*ptr2).second;
			set<int> gp;
			for (int i : gp_v) {
				gp.insert(i);
			}
			run(x, 0, conn, x, gp);
			if (sz(touched) < sz(gp_v)) {
				oneMore = true;
			}
		}
		cout << result + oneMore << "\n";
	}
	void solve() {
		int t;
		cin >> t;
		for (int i = 1; i <= t; i++) {
			solveOne();
		}
	}
};
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	Task solver;
	solver.solve();
	return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>

#define pb push_back
#define sz(v) ((int)(v).size())

using namespace std;

class Task {
public:
	void solveOne() {
		int l, x, y, result;
		cin >> l;
		map<int, vector<int>> conn;
		for (int i = 0; i < l-1; i++) {
			cin >> x >> y;
			conn[x].pb(y);
			conn[y].pb(x);
		}
		if (l <= 4) {
			cout << l-2 << "\n";
			return;
		}
		map<int, vector<int>> deg;
		for (int i = 1; i <= l; i++) {
			x = sz(conn[i]);
			deg[x-1].pb(i);
		}
		auto ptr = --deg.end();
		bool oneMore = false;
		if (sz((*ptr).second) >= 2) {
			// check if 2 in this group are not adjacent
			result = (*ptr).first * 2;
			vector<int> gp_v = (*ptr).second;
			if (sz(gp_v) > 2) {
				oneMore = true;
			}
			else { // == 2
				x = gp_v[0];
				y = gp_v[1];
				oneMore = true;
				for (int node : conn[x]) {
					if (node == y) {
						oneMore = false;
					}
				}
			}
		}
		else {
			// check the max one has a non-adjacent in the group of second most
			auto ptr2 = ptr;
			--ptr2;
			result = (*ptr).first + (*ptr2).first;
			x = (*ptr).second[0];
			vector<int> gp_v = (*ptr2).second;
			set<int> gp;
			for (int i : gp_v) {
				gp.insert(i);
			}
			set<int> touched;
			for (int i : conn[x]) {
				if (gp.find(i) != gp.end()) {
					touched.insert(i);
				}
 			}
			if (sz(touched) < sz(gp_v)) {
				oneMore = true;
			}
		}
		cout << result + oneMore << "\n";
	}
	void solve() {
		int t;
		cin >> t;
		for (int i = 1; i <= t; i++) {
			solveOne();
		}
	}
};
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	Task solver;
	solver.solve();
	return 0;
}