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

Previous submission (verdict: Time limit exceeded on test 9):
#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 (don.second == pr->second) {
			vertixes.push_back(don.first);
		}
	}

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

	for (auto vert : vertixes) {
		int count = 0;
		for (auto vert2 : vertixes) {
			count += (std::find(oreiller[vert].begin(), oreiller[vert].end(), vert2) != oreiller[vert].end());
		}
		if (count == vertixes.size() - 1) {
			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();
	}
}


Submission (verdict: Accepted):
#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 (don.second == pr->second) {
			vertixes.push_back(don.first);
		}
	}

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

	for (auto vert : vertixes) {
		int count = 0;
		for (auto vert2 : vertixes) {
			if (vert == vert2)
				continue;
			if (std::find(oreiller[vert].begin(), oreiller[vert].end(), vert2) != oreiller[vert].end() )
				count += 1;
			else
				break;
		}
		if (count == vertixes.size() - 1) {
			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();
	}
}