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


Submission (verdict: Accepted):
#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>

void solve(){
	int n;	cin >> n;
	vector< set<int>> graph(n+1);

	for( int i = 0; i < n-1; ++i){
		int a,b;	cin >> a >> b;
		graph[a].insert(b);
		graph[b].insert(a);		
	}	
	set<pi> st;
	for( int i = 1; i <= n; ++i){
		st.insert({ graph[i].size() , i});
	}
	int ans = 0;
	for( int i = 1; i <= n; ++i){
		// what if we pick i as first vertex to be removed
		st.erase( st.find({graph[i].size() ,i}));

		// if i and j are adjacent
		int j = (*st.rbegin()).second;
		int score = (int)graph[i].size() + (int)graph[j].size()-2;
		
		// if i and j are not adjacent
		for( int a : graph[i])
			st.erase( st.find( {graph[a].size() , a}));
		if( st.size()){
			j = (*st.rbegin()).second;			
			score = max( score , (int)graph[i].size() + (int)graph[j].size() - 1);
		}
		
		ans = max( ans , score);
		// insert all the  values
		st.insert( {graph[i].size(),i});
		for( int a : graph[i])
			st.insert( {graph[a].size(),a});
	}
	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;
}