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;
ll ans;

void dfs(ll node, ll prev_val, bool are_connected) {
  // UPDATE
  if (are_connected == true) {
    ans = lmax(ans, prev_val + graph[node].size() - 2);
  } else {
    ans = lmax(ans, prev_val + graph[node].size() - 1);
  }

  // cout << "new answer : " << graph[node].size() << endl;

  // CALCULATE
  used[node] = true;
  for (ll v : graph[node]) {
    if (!used[v]) {
      bool iscon = false;
      ll pass_value = prev_val;
      if (graph[node].size() < prev_val) {
        iscon = true;
        pass_value = graph[node].size();
      }
      dfs(v, pass_value, iscon);
    }
  }
}

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);
    loop(i, n - 1) {
      ll u, v;
      cin >> u >> v;
      graph[u - 1].push_back(v - 1);
      graph[v - 1].push_back(u - 1);
    }
    ans = -INF;

    used[0] = true;
    for (ll v : graph[0]) {
      dfs(v, graph[0].size(), true);
    }

    show(ans);

    // RELOAD
    graph.clear();
    used.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;
ll ans;

void dfs(ll node, ll prev_val, bool are_connected) {
  // UPDATE
  if (are_connected == true) {
    ans = lmax(ans, prev_val + graph[node].size() - 2);
  } else {
    ans = lmax(ans, prev_val + graph[node].size() - 1);
  }

  // cout << "new answer : " << graph[node].size() << endl;

  // CALCULATE
  used[node] = true;
  for (ll v : graph[node]) {
    if (!used[v]) {
      bool iscon = false;
      ll pass_value = prev_val;
      if (graph[node].size() > prev_val) {
        iscon = true;
        pass_value = graph[node].size();
      }
      dfs(v, pass_value, iscon);
    }
  }
}

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);
    loop(i, n - 1) {
      ll u, v;
      cin >> u >> v;
      graph[u - 1].push_back(v - 1);
      graph[v - 1].push_back(u - 1);
    }
    ans = -INF;

    used[0] = true;
    for (ll v : graph[0]) {
      dfs(v, graph[0].size(), true);
    }

    show(ans);

    // RELOAD
    graph.clear();
    used.clear();
  }
  return 0;
}