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

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define endl '\n';
#define __builtin_popcount(x) __builtin_popcountll(x)
using namespace std;

#ifdef LOCAL
#include "debuglib.h"
#else
#endif

const int INF=1e17;
const int M=1e9+7;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};

int Remove(vector<vector<int>>& edges, set<int> removed){
	int n=edges.size()+1;
	vector<int> deg(n);
	for(int i=0;i<n-1;i++){
		if(removed.count(edges[i][0]) || removed.count(edges[i][1])){
			continue;
		}
		
		++deg[edges[i][0]];
		++deg[edges[i][1]];
	}
	
	int to_rem=max_element(deg.begin(), deg.end())-deg.begin();
	return to_rem;
}

void test_cases(){
	int n;
	cin >> n;
	vector<vector<int>> edges;
	for(int i=0;i<n-1;i++){
		int u, v;
		cin >> u >> v;
		--u, --v;
		edges.push_back({u, v});
	}
	
	if(n == 2){
		cout << 0 << endl;
		return;
	}
	
	set<int> removed;
	removed.insert(Remove(edges, removed));
	removed.insert(Remove(edges, removed));
	vector<vector<int>> adj(n);
	for(auto& x: edges){
		if(removed.count(x[0]) || removed.count(x[1])){
			continue;
		}
		
		adj[x[0]].push_back(x[1]);
		adj[x[1]].push_back(x[0]);
	}
	
	vector<int> vis(n);
	function<void(int)> dfs = [&](int u){
		vis[u]=1;
		for(int x: adj[u]){
			if(!vis[x]){
				dfs(x);
			}
		}
	};
	int cnt=0;
	for(int i=0;i<n;i++){
		if(!vis[i] && (removed.count(i) == 0)){
			dfs(i);
			cnt += 1;
		}
	}
	
	cout << cnt << endl;
}

int32_t main(){
	fastio;
	int t=1;
	cin >> t;
	while(t--){
		test_cases();
	}
}

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define endl '\n';
#define __builtin_popcount(x) __builtin_popcountll(x)
using namespace std;

#ifdef LOCAL
#include "debuglib.h"
#else
#endif

const int INF=1e17;
const int M=1e9+7;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};

int Remove(vector<vector<int>>& edges, set<int> removed){
	int n=edges.size()+1;
	vector<int> deg(n);
	for(int i=0;i<n-1;i++){
		if(removed.count(edges[i][0]) || removed.count(edges[i][1])){
			continue;
		}
		
		++deg[edges[i][0]];
		++deg[edges[i][1]];
	}
	
	int to_rem=max_element(deg.begin(), deg.end())-deg.begin();
	if(removed.count(to_rem)){
		if(to_rem == 0){
			to_rem += 1;
		}
		else{
			to_rem -= 1;
		}
	}
	
	return to_rem;
}

void test_cases(){
	int n;
	cin >> n;
	vector<vector<int>> edges;
	for(int i=0;i<n-1;i++){
		int u, v;
		cin >> u >> v;
		--u, --v;
		edges.push_back({u, v});
	}
	
	if(n == 2){
		cout << 0 << endl;
		return;
	}
	
	set<int> removed;
	removed.insert(Remove(edges, removed));
	removed.insert(Remove(edges, removed));
	vector<vector<int>> adj(n);
	for(auto& x: edges){
		if(removed.count(x[0]) || removed.count(x[1])){
			continue;
		}
		
		adj[x[0]].push_back(x[1]);
		adj[x[1]].push_back(x[0]);
	}
	
	vector<int> vis(n);
	function<void(int)> dfs = [&](int u){
		vis[u]=1;
		for(int x: adj[u]){
			if(!vis[x]){
				dfs(x);
			}
		}
	};
	int cnt=0;
	for(int i=0;i<n;i++){
		if(!vis[i] && (removed.count(i) == 0)){
			dfs(i);
			cnt += 1;
		}
	}
	
	cout << cnt << endl;
}

int32_t main(){
	fastio;
	int t=1;
	cin >> t;
	while(t--){
		test_cases();
	}
}