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

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void removeNode(map<int, set<int>>&adjList, int node){
	for(auto ri = adjList[node].begin(); ri != adjList[node].end(); ri++){
		int adjNode = *ri;
		adjList[adjNode].erase(node);
	}
	adjList.erase(node);
}

int findHighestDegreeNode(map<int, set<int>>& adjList){
	int maxSize = 0, node;
	for(auto ri = adjList.begin(); ri != adjList.end(); ri++){
		if((ri -> second).size() >= maxSize){
			maxSize = (ri -> second).size();
			node = ri -> first;
		}
	}
	return node;
}

void visitComponent(map<int, set<int>>& adjList, int currNode, vector<int>& visited){
	if(visited[currNode])
		return;
	visited[currNode] = 1;
	for(auto ri = adjList[currNode].begin(); ri != adjList[currNode].end(); ri++){
		int adjNode = *ri;
		visitComponent(adjList, adjNode, visited);
	}
}

void solve(){
    int n;
    cin>>n;
    map<int, set<int>> adjList;
    for(int ri = 1; ri < n; ri++){
    	int u, v;
    	cin>>u>>v;
    	adjList[u].insert(v);
    	adjList[v].insert(u);
    }
    int highestDegreeNode = findHighestDegreeNode(adjList);
    removeNode(adjList, highestDegreeNode);
    highestDegreeNode = findHighestDegreeNode(adjList);
    removeNode(adjList, highestDegreeNode);
    int connectedComponentsCount = 0;
    vector<int> visited(n + 1);
    for(auto ri = adjList.begin(); ri != adjList.end(); ri++){
    	int currNode = ri -> first;
    	if(!visited[currNode]){
    		visitComponent(adjList, currNode, visited);
    		connectedComponentsCount++;
    	}
    }
    cout<<connectedComponentsCount<<endl;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        solve();
    }
    return 0;
}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void removeNode(map<int, set<int>>&adjList, int node){
	for(auto ri = adjList[node].begin(); ri != adjList[node].end(); ri++){
		int adjNode = *ri;
		adjList[adjNode].erase(node);
	}
	adjList.erase(node);
}

int findHighestDegreeNode(map<int, set<int>>& adjList){
	int maxSize = -1, node;
	for(auto ri = adjList.begin(); ri != adjList.end(); ri++){
		int adjNodesNum = (ri -> second).size();
		if(adjNodesNum > maxSize){
			maxSize = adjNodesNum;
			node = ri -> first;
		}
	}
	return node;
}

void visitComponent(map<int, set<int>>& adjList, int currNode, vector<int>& visited){
	if(visited[currNode])
		return;
	visited[currNode] = 1;
	for(auto ri = adjList[currNode].begin(); ri != adjList[currNode].end(); ri++){
		int adjNode = *ri;
		visitComponent(adjList, adjNode, visited);
	}
}

void solve(){
    int n;
    cin>>n;
    map<int, set<int>> adjList;
    for(int ri = 1; ri < n; ri++){
    	int u, v;
    	cin>>u>>v;
    	adjList[u].insert(v);
    	adjList[v].insert(u);
    }
    int highestDegreeNode = findHighestDegreeNode(adjList);
    removeNode(adjList, highestDegreeNode);
    highestDegreeNode = findHighestDegreeNode(adjList);
    removeNode(adjList, highestDegreeNode);
    int connectedComponentsCount = 0;
    vector<int> visited(n + 1);
    for(auto ri = adjList.begin(); ri != adjList.end(); ri++){
    	int currNode = ri -> first;
    	if(!visited[currNode]){
    		visitComponent(adjList, currNode, visited);
    		connectedComponentsCount++;
    	}
    }
    cout<<connectedComponentsCount<<endl;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        solve();
    }
    return 0;
}