Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 1093 ms):
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int deg[N];
vector<int> G[N];
void _main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		deg[i]=0,G[i].clear();
	for(int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		deg[u]++;
		deg[v]++;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	set<pair<int,int>> st;
	for(int i=1;i<=n;i++)
		st.insert({deg[i],i});
	int ans=0;
	for(int u=1;u<=n;u++)
		for(auto v : G[u])
			ans=max(ans,deg[u]+deg[v]-2);
	for(int i=1;i<=n;i++){
		st.erase({deg[i],i});
		for(auto v : G[i])
			st.erase({deg[v],v});
		if(st.size()){
			int d=st.rbegin()->first;
			ans=max(ans,d+deg[i]-1);
		}
		st.insert({deg[i],i});
		for(auto v : G[i])
			st.insert({deg[v],v});
	}
	cout<<ans<<"\n";
}
int main(){
	int t;
	cin>>t;
	while(t--)
		_main();
	return 0;
}

Solution 2 (runtime: 233 ms):
#include<bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<long long int> vll;
typedef long long int lli;
typedef pair<int, int> pii;
typedef map<string, int> msi;
typedef map<int, vector<int>> miv;

// Funciones vector
#define PB(a) push_back(a);

bool sort_func(int a, int b) {
  if (a < b) {
    return true;
  } else {
    return false;
  }
}
#define ord(vect) sort(vect.begin(), vect.end(), sort_func);

#define borra_el(vect, el) vect.erase(vect.find(el));
#define borra_range(vect, a, b) vect.erase(a, b);
#define borra(vect, n) vect.erase(vect.begin() + n);
#define B begin();
#define E end();
#define copia(v1, v2)                                                          \
  ;                                                                            \
  copy(v1.begin(), v1.end(), back_inserter(v2));

// Funciones map
#define F first;
#define S second;

// Logaritmo de 2
double log_2 = log(2);
double log2(int a) { return (log(a) / log_2); }

void Imprime(vi vect) {
  for (int i = 0; i < vect.size(); i++) {
    cout << vect[i] << " ";
  }
  cout << "\n";
}

void Imprime2d(vector<vi> vect) {
  for (int j = 0; j<vect.size(); j++){
    for (int i = 0; i < vect[j].size(); i++) {
        cout << vect[j][i] << " ";
    }
    cout << "\n";
  }
}

void Imprime_set(set<int> s) {
  copy(s.begin(), s.end(), ostream_iterator<int>(cout, " "));
  cout << endl;
}

int maximo_comun_divisor(int a, int b) {
  int temporal; // Para no perder b
  while (b != 0) {
    temporal = b;
    b = a % b;
    a = temporal;
  }
  return a;
}

int minimo_comun_multiplo(int a, int b) {
  return (a * b) / maximo_comun_divisor(a, b);
}

bool isNumeric(string const &str) {
  auto it = str.begin();
  while (it != str.end() && isdigit(*it)) {
    it++;
  }
  return !str.empty() && it == str.end();
}

vi lee(int n) {
  int el;
  vi vect;
  for (int i = 0; i < n; i++) {
    cin >> el;
    vect.PB(el);
  }
  return (vect);
}

int solve() {
  // Code aquí
  int n;
  cin >> n;
  vector<vi> grafo (n, vi {});
  vi conected (n, 0);
  int groups = 1;

  int l, r;
  for (int i = 0; i<n-1; i++){
    cin >> l >> r;
    grafo[l-1].PB(r-1);
    grafo[r-1].PB(l-1);
    conected[l-1]++;
    conected[r-1]++;
  }
  vi index = {0};
  for (int i = 1; i<conected.size(); i++){
    if (conected[index[0]] == conected[i]){
        index.PB(i);
    }
    if (conected[index[0]] < conected[i]){
        index = {i};
    }
  }
  if (index.size()>2){
    cout << 2*conected[index[0]]-1 << endl;
    return 0;
  }


  groups += conected[index[0]]-1;
  conected[index[0]] = -1;
  for (int i = 0; i<grafo[index[0]].size(); i++){
    conected[grafo[index[0]][i]]--;
  }
  index = {0};
  for (int i = 1; i<conected.size(); i++){
    if (conected[index[0]] < conected[i]){
        index = {i};
    }
    if (conected[index[0]] == conected[i]){
        index.PB(i);
    }
  }
  groups += conected[index[0]]-1;
  cout << groups << endl;

  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr); 
  int T;
  cin >> T; // Número de casos
  while (T--) {
    solve();
  }
  return 0;
}

//Eliminar comentario si el proyecto está terminado (Dinámica empezó el 21/06/2024)