Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <cstring>
#include <iomanip>
#include <climits>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <random>
#include <numeric>
  
using namespace std;
  
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vector<pair<ll, ll>>> vvpll;
typedef pair<ll, pair<ll, ll>> SuperPair;
  
const ll INF = 1e18;
  
#define _MATH_DEFINES_DEFINED
  
#define MP make_pair
#define pb push_back
#define printl cout << "\n";
#define dprintl cout << endl;
#define loop(x, n) for (ll x = 0; x < n; x++)
#define range(x, l, r) for (ll x = l; x < r; x++)
#define show(a) cout << (a) << "\n";
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define con cout << "NO\n"
#define coy cout << "YES\n";
#define max_el max_element
#define min_el min_element
#define stf shrink_to_fit
  
template<typename T>
void input(vector<T> &mas) {
  loop(i, mas.size()) {
    cin >> mas[i];
  }
}
  
template<typename T>
void output(vector<T> &mas) {
  loop(i, mas.size()) {
    cout << mas[i] << " ";
  }
  printl;
}
  
template<typename T>
void waste_read(ll n) {
  T ignore;
  for (ll i = 0; i < n; i++) cin >> ignore;
}
  
ll to_number(char c) {
  return (ll)c - (ll)'a' + 1;
}
  
char to_char(ll x) {
  return (char)(x + (ll)'a' - 1);
}
  
ll labs(ll n) {
  return (n < 0) ? -n : n;
}
  
ll lmin(ll a, ll b) {
  return (a < b) ? a : b;
}
  
ll lmax(ll a, ll b) {
  return (a > b) ? a : b;
}
  
ll lmod(ll a, ll b) {
  return ((a % b) + b) % b;
}

vvll graph;
vll used, used_max, max_values;
ll ans;

ll dfs_max(ll node) {
  used_max[node] = true;
  ll value = -INF;
  for (ll child : graph[node]) {
    if (!used_max[child]) {
      ll subvalue = graph[child].size();
      value = lmax(value, lmax(dfs_max(child), subvalue));
    }
  }
  max_values[node] = value;
  return value;
}

void dfs(ll node) {
  used[node] = true;

  if (graph[node].size() == 0) return;

  // cout << "node = " << node << endl;

  // SUBGROUP
  for (ll child : graph[node]) {
    if (!used[child]) {
      ans = lmax(ans, max_values[child] + graph[node].size() - 1);
    }
  }
  // cout << "[INFO] ans = " << ans << endl;

  // DIFFERENT GROUPS
  if (graph[node].size() >= 2) {
    vll values;
    for (ll child : graph[node]) {
      if (!used[child]) {
        values.push_back(lmax(
          graph[child].size(),
          max_values[child]
        ));
      }
    }
    sort(values.rbegin(), values.rend());
    ans = lmax(ans, values[0] + values[1] - 1);
  }

  // cout << "[SPECIAL INFO] ans = " << ans << endl;

  for (ll child : graph[node]) {
    if (!used[child]) {
      dfs(child);
    }
  }
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll t;
  cin >> t;
  while (t--) {
    ll n;
    cin >> n;
    graph.resize(n);
    used.resize(n);
    used_max.resize(n);
    max_values.resize(n);
    vpll edges;
    loop(i, n - 1) {
      ll u, v;
      cin >> u >> v;
      graph[u - 1].push_back(v - 1);
      graph[v - 1].push_back(u - 1);
      edges.push_back(make_pair(u - 1, v - 1));
    }

    // cout << "data is now stored!" << endl;
    dfs_max(0);
    // cout << "max_values = "; output<ll>(max_values);
    // cout << "max calculated." << endl;

    ans = -INF;
    for (pll edge : edges) {
      ans = lmax(ans, graph[edge.first].size() + graph[edge.second].size() - 2);
    }

    // cout << "current_answer = " << ans << endl;

    dfs(0);

    show(ans);

    graph.clear();
    used.clear();
    used_max.clear();
  }
  return 0;
}
























Submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <cstring>
#include <iomanip>
#include <climits>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <random>
#include <numeric>
  
using namespace std;
  
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vector<pair<ll, ll>>> vvpll;
typedef pair<ll, pair<ll, ll>> SuperPair;
  
const ll INF = 1e18;
  
#define _MATH_DEFINES_DEFINED
  
#define MP make_pair
#define pb push_back
#define printl cout << "\n";
#define dprintl cout << endl;
#define loop(x, n) for (ll x = 0; x < n; x++)
#define range(x, l, r) for (ll x = l; x < r; x++)
#define show(a) cout << (a) << "\n";
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define con cout << "NO\n"
#define coy cout << "YES\n";
#define max_el max_element
#define min_el min_element
#define stf shrink_to_fit
  
template<typename T>
void input(vector<T> &mas) {
  loop(i, mas.size()) {
    cin >> mas[i];
  }
}
  
template<typename T>
void output(vector<T> &mas) {
  loop(i, mas.size()) {
    cout << mas[i] << " ";
  }
  printl;
}
  
template<typename T>
void waste_read(ll n) {
  T ignore;
  for (ll i = 0; i < n; i++) cin >> ignore;
}
  
ll to_number(char c) {
  return (ll)c - (ll)'a' + 1;
}
  
char to_char(ll x) {
  return (char)(x + (ll)'a' - 1);
}
  
ll labs(ll n) {
  return (n < 0) ? -n : n;
}
  
ll lmin(ll a, ll b) {
  return (a < b) ? a : b;
}
  
ll lmax(ll a, ll b) {
  return (a > b) ? a : b;
}
  
ll lmod(ll a, ll b) {
  return ((a % b) + b) % b;
}

vvll graph;
vll used, used_max, max_values;
ll ans;

ll dfs_max(ll node) {
  used_max[node] = true;
  ll value = -INF;
  for (ll child : graph[node]) {
    if (!used_max[child]) {
      ll subvalue = graph[child].size();
      value = lmax(value, lmax(dfs_max(child), subvalue));
    }
  }
  max_values[node] = value;
  return value;
}

void dfs(ll node) {
  used[node] = true;

  if (graph[node].size() == 0) return;

  // cout << "node = " << node << endl;

  // SUBGROUP
  for (ll child : graph[node]) {
    if (!used[child]) {
      ans = lmax(ans, max_values[child] + graph[node].size() - 1);
    }
  }
  // cout << "[INFO] ans = " << ans << endl;

  // DIFFERENT GROUPS
  if (graph[node].size() >= 2) {
    vll values;
    for (ll child : graph[node]) {
      if (!used[child]) {
        values.push_back(lmax(
          graph[child].size(),
          max_values[child]
        ));
      }
    }
    sort(values.rbegin(), values.rend());
    ans = lmax(ans, values[0] + values[1] - 1);
  }

  // cout << "[SPECIAL INFO] ans = " << ans << endl;

  for (ll child : graph[node]) {
    if (!used[child]) {
      dfs(child);
    }
  }
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll t;
  cin >> t;
  while (t--) {
    ll n;
    cin >> n;
    graph.resize(n);
    used.resize(n);
    used_max.resize(n);
    max_values.resize(n);
    vpll edges;
    loop(i, n - 1) {
      ll u, v;
      cin >> u >> v;
      graph[u - 1].push_back(v - 1);
      graph[v - 1].push_back(u - 1);
      edges.push_back(make_pair(u - 1, v - 1));
    }

    // cout << "data is now stored!" << endl;
    dfs_max(0);
    // cout << "max_values = "; output<ll>(max_values);
    // cout << "max calculated." << endl;

    ans = -INF;
    for (pll edge : edges) {
      ans = lmax(ans, graph[edge.first].size() + graph[edge.second].size() - 2);
    }

    // cout << "current_answer = " << ans << endl;

    dfs(0);

    show(ans);

    graph.clear();
    used.clear();
    used_max.clear();
    max_values.clear();
  }
  return 0;
}