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

Previous 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;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void solve(){
    int n;
    cin>>n;
    vector<vector<int>> adjList(n + 1);
    for(int ri = 1; ri < n; ri++){
    	int u, v;
    	cin>>u>>v;
    	adjList[u].push_back(v);
    	adjList[v].push_back(u);
    }
    vector<int> degree(n + 1), maxDegreeNodes;
    set<pair<int, int>> degreeToNode;
    int maxDegree = 0;
    for(int ri = 1; ri <= n; ri++){
    	degree[ri] = adjList[ri].size();
    	degreeToNode.insert({degree[ri], ri});
    	if(degree[ri] > maxDegree){
    		maxDegree = degree[ri];
    	}
    }
    for(int ri = 1; ri <= n; ri++){
    	if(degree[ri] == maxDegree)
    		maxDegreeNodes.push_back(ri);
    }
    
    int maxConnectedComponentCounts = 0;
    for(int ri = 0; ri < maxDegreeNodes.size(); ri++){
    	int currNode = maxDegreeNodes[ri];
    	int tmpConnectedComponentsCount = 1 + maxDegree - 1;
    	degreeToNode.erase({degree[currNode], currNode});
    	for(int ri = 0; ri < adjList[currNode].size(); ri++){
    		int adjNode = adjList[currNode][ri];
    		degreeToNode.erase({degree[adjNode], adjNode});
    		degreeToNode.insert({degree[adjNode] - 1, adjNode});
    	}
    	tmpConnectedComponentsCount += (*(degreeToNode.rbegin())).first - 1;
    	if(tmpConnectedComponentsCount > maxConnectedComponentCounts)
    		maxConnectedComponentCounts = tmpConnectedComponentsCount;
    	degreeToNode.insert({degree[currNode], currNode});
    	for(int ri = 0; ri < adjList[currNode].size(); ri++){
    		int adjNode = adjList[currNode][ri];
    		degreeToNode.erase({degree[adjNode] - 1, adjNode});
    		degreeToNode.insert({degree[adjNode], adjNode});
    	}
    }
    cout<<maxConnectedComponentCounts<<endl;
}

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