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

Previous submission (verdict: Time limit exceeded on test 7):
#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;
  int temp;
  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{
        temp = (c[0].first + c[1].first) - 2;
        for(int i = 2; i<n; i++){
            if(checkOverlap(c[0].second, c[i].second, adj)){
              temp = max(temp, (c[0].first + c[i].first) - 1);
          }else{
            temp =max(temp, (c[0].first + c[i].first) - 2);
          }
        }
      }
  }
  return temp;
}

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