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

Previous submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>


int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout.tie(nullptr);

	unsigned t;
	std::cin >> t;
	std::map<int, int> al;
	std::vector<std::vector<int>> oreiller;
	while (t --> 0) {
		unsigned n;
		std::cin >> n;

		oreiller.resize(n + 1);
		for (unsigned i = 0; i < n - 1; ++i) {
			unsigned u, v;
			std::cin >> u >> v;
			++al[u];
			++al[v];
			oreiller[u].push_back(v);
			oreiller[v].push_back(u);
		}

		using pair_type = decltype(al)::value_type;
		auto pr = std::max_element(
			std::begin(al), std::end(al),
			[] (const pair_type & p1, const pair_type & p2) {
				return p1.second < p2.second;
			}
		);
		unsigned counter = pr->second;

		int the = pr->first;
		for (int a : oreiller[the]) {
			oreiller[a].erase(std::find(oreiller[a].begin(), oreiller[a].end(), the));
			--al[a];
		}

		al.erase(pr);


		pr = std::max_element(
			std::begin(al), std::end(al),
			[] (const pair_type & p1, const pair_type & p2) {
				return p1.second < p2.second;
			}
		);
		counter += pr->second != 1 ?(pr->second - 1) : 1;

		std::cout << counter << '\n';

		oreiller.clear();
		al.clear();
	}
}


Submission (verdict: Wrong answer on test 28):
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>



std::map<int, int> al;
using pair_type = decltype(al)::value_type;



std::pair<bool, unsigned> spec_case(std::map<int, int> &al, std::vector<std::vector<int>> &oreiller) {
	auto pr = std::max_element(
		std::begin(al), std::end(al),
		[] (const pair_type & p1, const pair_type & p2) {
			return p1.second < p2.second;
		}
	);


	std::vector<int> vertixes;
	for (auto &don : al) {
		if (vertixes.size() > 3) {
			return {false, 0};
		}

		if (don.second == pr->second) {
			vertixes.push_back(don.first);
		}
	}

	if (vertixes.size() != 3)
		return {false, 0};

	for (auto vert : vertixes) {
		int count = 0;
		count += (std::find(oreiller[vert].begin(), oreiller[vert].end(), vertixes[0]) != oreiller[vert].end());
		count += (std::find(oreiller[vert].begin(), oreiller[vert].end(), vertixes[1]) != oreiller[vert].end());
		count += (std::find(oreiller[vert].begin(), oreiller[vert].end(), vertixes[2]) != oreiller[vert].end());

		if (count == 2) {
			return {true, 2 * pr->second - 1};
		}
	}

	return {false, 0};
}


int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout.tie(nullptr);

	unsigned t;
	std::cin >> t;
	std::vector<std::vector<int>> oreiller;
	while (t --> 0) {
		unsigned n;
		std::cin >> n;

		oreiller.resize(n + 1);
		for (unsigned i = 0; i < n - 1; ++i) {
			unsigned u, v;
			std::cin >> u >> v;
			++al[u];
			++al[v];
			oreiller[u].push_back(v);
			oreiller[v].push_back(u);
		}


		auto speccase = spec_case(al, oreiller);
		if (speccase.first) {
			std::cout << speccase.second << '\n';

			oreiller.clear();
			al.clear();
			continue;
		}

		auto pr = std::max_element(
			std::begin(al), std::end(al),
			[] (const pair_type & p1, const pair_type & p2) {
				return p1.second < p2.second;
			}
		);
		unsigned counter = pr->second;

		int the = pr->first;
		for (int a : oreiller[the]) {
			oreiller[a].erase(std::find(oreiller[a].begin(), oreiller[a].end(), the));
			--al[a];
		}

		al.erase(pr);


		pr = std::max_element(
			std::begin(al), std::end(al),
			[] (const pair_type & p1, const pair_type & p2) {
				return p1.second < p2.second;
			}
		);
		counter += pr->second - 1;

		std::cout << counter << '\n';

		oreiller.clear();
		al.clear();
	}
}