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;

#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define int long long
#define pi pair<int,int>

int connected_comp(vector<set<int>> graph, int a, int b){
	int n = graph.size()-1;
	vector<int> visited(n+1);
	// lambda expression for dfs call	
	function< void(int,int)> dfs = [&](int curr,int par) {
		visited[curr] = 1;
		for( int i : graph[curr]){
			if( i != par)
				dfs(i , curr);
		}
		
	};
	
	int ans = 0;
	for( int i = 1; i <= n; ++i){
		if( !visited[i] and graph[i].size() > 0){
			ans += 1;
			dfs(i,-1);
		}
		else if( !visited[i] and graph[i].size() == 0){
			if( i != a and i != b)	{
				ans += 1;
			}
		}
	}
	return ans;
}

int remove( vector< set<int>>& graph){
	int n = graph.size()-1;
	
	vector<pi> mp(n);
	for( int i = 1; i <= n; ++i)
		mp[i-1] = {graph[i].size() , i};

	sort( rall(mp));
	int a = mp[0].second;
	
	// remove node 'a' and all of its edges

	graph[a].clear();
	for( int k = 1; k <= n; ++k){
	    if( graph[k].find(a) != graph[k].end())
			graph[k].erase( graph[k].find(a));
	}
	return a;	
	
}
void print(vector<set<int>> graph){
	int n = graph.size()-1;
	for( int i = 1; i <= n; ++i){
		cout << i << " : ";
		for( int j : graph[i])
			cout << j << " ";
		cout << endl;
	}
}
void solve(){
	int n;	cin >> n;
	vector< set<int>> graph(n+1);
	vector<int> freq(n+1);
	for( int i = 0; i < n-1; ++i){
		int a,b;	cin >> a >> b;
		graph[a].insert(b);
		graph[b].insert(a);		
		freq[a] += 1;
		freq[b] += 1;
	}	

	int a = remove( graph);
	int b = remove(graph);

	int ans = connected_comp(graph,a,b);
	
	if( n == 2)	cout << 0;
	else
		cout << ans;
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    
    int test = 1;   
    cin >> test;
    while( test--){
        solve();
	    cout << "\n";
    }

    return 0;
}


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

#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define int long long
#define pi pair<int,int>

int connected_comp(vector<set<int>> graph, int a, int b){
	int n = graph.size()-1;
	vector<int> visited(n+1);
	// lambda expression for dfs call	
	function< void(int,int)> dfs = [&](int curr,int par) {
		visited[curr] = 1;
		for( int i : graph[curr]){
			if( i != par)
				dfs(i , curr);
		}
		
	};
	
	int ans = 0;
	for( int i = 1; i <= n; ++i){
		if( !visited[i] and graph[i].size() > 0){
			ans += 1;
			dfs(i,-1);
		}
		else if( !visited[i] and graph[i].size() == 0){
			if( i != a and i != b)	{
				ans += 1;
			}
		}
	}
	return ans;
}

int remove( vector< set<int>>& graph){
	int n = graph.size()-1;
	
	vector<pi> mp(n);
	for( int i = 1; i <= n; ++i)
		mp[i-1] = {graph[i].size() , i};

	sort( rall(mp));
	int a = mp[0].second;
	if( mp[0].first == n-1)	// connected to all
		return -1;
	
	// remove node 'a' and all of its edges

	graph[a].clear();
	for( int k = 1; k <= n; ++k){
	    if( graph[k].find(a) != graph[k].end())
			graph[k].erase( graph[k].find(a));
	}
	return a;	
	
}
void print(vector<set<int>> graph){
	int n = graph.size()-1;
	for( int i = 1; i <= n; ++i){
		cout << i << " : ";
		for( int j : graph[i])
			cout << j << " ";
		cout << endl;
	}
}
void solve(){
	int n;	cin >> n;
	vector< set<int>> graph(n+1);
	vector<int> freq(n+1);
	for( int i = 0; i < n-1; ++i){
		int a,b;	cin >> a >> b;
		graph[a].insert(b);
		graph[b].insert(a);		
		freq[a] += 1;
		freq[b] += 1;
	}	

	int a = remove( graph);
	if( a == -1){
		cout << n-2;return;
	}
	
	int b = remove(graph);

	int ans = connected_comp(graph,a,b);
	cout << ans;
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    
    int test = 1;   
    cin >> test;
    while( test--){
        solve();
	    cout << "\n";
    }

    return 0;
}