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

Previous submission (verdict: Wrong answer on test 2):
#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 = {i};
    }
    if (conected[index[0]] == conected[i]){
        index.PB(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)

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