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;
	int vip_node;
	void run1(int node, int from, int now_depth, int first_gp_member_depth, map<int, vector<int>>& conn, set<int> gp) {
		if (oneMore) {
			return;
		}
		bool is_gp_member = gp.find(node) != gp.end();
		if (is_gp_member) {
			if (first_gp_member_depth == -1) {
				first_gp_member_depth = now_depth;
			}
			if (first_gp_member_depth + 1 < now_depth) {
				oneMore = true;
				return;
			}
		}
		for (int next_node : conn[node]) {
			if (next_node != from) {
				run1(next_node, node, now_depth + 1, first_gp_member_depth, conn, gp);
			}
		}
	}
	void run2(int node, int from, int now_depth, int vip_depth, int first_gp_member_depth, map<int, vector<int>>& conn, set<int> gp) {
		if (oneMore) {
			return;
		}
		if (node == vip_node) {
			vip_depth = now_depth;
			if (first_gp_member_depth != -1 && first_gp_member_depth + 1 < now_depth) {
				oneMore = true;
				return;
			}
		}
		bool is_gp_member = gp.find(node) != gp.end();
		if (is_gp_member) {
			if (first_gp_member_depth == -1) {
				first_gp_member_depth = now_depth;
			}
			if (vip_depth != -1 && vip_depth + 1 < now_depth) {
				oneMore = true;
				return;
			}
		}
		for (int next_node : conn[node]) {
			if (next_node != from) {
				run2(next_node, node, now_depth + 1, vip_depth, first_gp_member_depth, conn, gp);
			}
		}
	}
	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();
		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);
			}
			run1(1, 0, 0, -1, conn, gp);
		}
		else {
			auto ptr2 = ptr;
			--ptr2;
			result = (*ptr).first + (*ptr2).first;
			vip_node = (*ptr).second[0];
			vector<int> gp_v = (*ptr2).second;
			set<int> gp;
			for (int i : gp_v) {
				gp.insert(i);
			}
			run2(1, 0, 0, -1, -1, conn, gp);
		}
		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: 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;
}