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

Previous submission (verdict: Time limit exceeded on test 11):
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define pb push_back

bool checkOverlap(int node1, int node2, vector<vector<int>> &adj){
  for(auto it: adj[node1]){
    if(it == node2){
      return false;
    }
  }

  return true;
}

void getCardinality(vector<vector<int>> &adj, int n, vector<pair<int,int>> &cardinality){
  for(int i = 1; i<=n; i++){
    int size = adj[i].size();
    cardinality.pb({size, i});
  }
}

int findComponents(vector<vector<int>> &adj, vector<pair<int,int>> &c, int n){
  if(n <= 2) return 0;
  if(c[0].first == c[1].first && c[1].first == c[2].first){
    return (2 * c[0].first) - 1;
  }
  else if(c[0].first == c[1].first){
    if(checkOverlap(c[0].second, c[1].second, adj)){
      return (2 * c[0].first) - 1;
    }
    else{
      return (2 * c[0].first) - 2;
    }
  }
  else{
      if(checkOverlap(c[0].second, c[1].second, adj)){
          return (c[0].first + c[1].first) - 1;
      }
      else{
        int temp = (c[0].first + c[1].first) - 2;
        for(int i = 2; i<n; i++){
          if(c[i - 1].first == c[i].first){
            if(checkOverlap(c[0].second, c[i].second, adj)){
              temp = max(temp,(c[0].first + c[i].first) - 1);
              break;
            }
          }
        }
        return temp;
      }
  }
  return -1;
}

int main()
{
  int t;
  cin>>t;
  while(t){
    int n;
    cin>>n;
    int u, v;
    vector<vector<int>> adj(n+1);
    for(int i = 1; i<n; i++){
     cin>>u>>v;
     adj[u].push_back(v);
     adj[v].push_back(u);
   }
   vector<pair<int, int>> cardinality;
   getCardinality(adj, n, cardinality);
   sort(cardinality.begin(), cardinality.end(), greater<pair<int, int>>());
   cout<<findComponents(adj, cardinality, n)<<"\n";
   // for(auto it: cardinality){
   //  cout<<it.first<<" "<<it.second<<"\n";
   // }
   t--;
 }
 return 0;
}

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

bool checkOverlap(int node1, int node2, set<pair<int, int>> &st){
  if(st.find({node1, node2}) != st.end() || st.find({node2, node1}) != st.end()){
    return false;
  }

  return true;
}

void getCardinality(unordered_map<int, int> &mp, vector<pair<int,int>> &cardinality){
   for(auto it: mp){
    cardinality.pb({it.second, it.first});
   }
}

int findComponents(set<pair<int, int>> &st, vector<pair<int,int>> &c, int n){
  if(n <= 2) return 0;
  if(c[0].first == c[1].first && c[1].first == c[2].first){
    return (2 * c[0].first) - 1;
  }
  else if(c[0].first == c[1].first){
    if(checkOverlap(c[0].second, c[1].second, st)){
      return (2 * c[0].first) - 1;
    }
    else{
      return (2 * c[0].first) - 2;
    }
  }
  else{
    if(checkOverlap(c[0].second, c[1].second, st)){
      return (c[0].first + c[1].first) - 1;
    }
    else{
      int temp = (c[0].first + c[1].first) - 2;
      for(int i = 2; i<n; i++){
        if(c[i - 1].first == c[i].first){
          if(checkOverlap(c[0].second, c[i].second, st)){
            temp = (c[0].first + c[i].first) - 1;
            break;
          }
        }
        else{
          break;
        }
      }
      return temp;
    }
  }
  return -1;
}

int main()
{
  int t;
  cin>>t;
  while(t){
    int n;
    cin>>n;
    int u, v;
    set<pair<int, int>> st;
    unordered_map<int, int> mp;
    for(int i = 1; i<n; i++){
     cin>>u>>v;
     st.insert({u, v});
     mp[u]++;
     mp[v]++;
   }
    
   vector<pair<int, int>> cardinality;
   getCardinality(mp, cardinality);
   sort(cardinality.begin(), cardinality.end(), greater<pair<int, int>>());
   cout<<findComponents(st, cardinality, n)<<"\n";
   t--;
 }
 return 0;
}