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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n;
  cin >> n;
  vector<set<int>> e(n, set<int>());
  for (int i = 0; i < n - 1; ++i){
    int x, y;
    cin >> x >> y;
    e[--x].insert(--y);
    e[y].insert(x);
  }
  pt mx = {0, 0};
  for (int i = 0; i < n; ++i){
    mx = max(mx, {e[i].size(), i});
  }
  vector<int> v;
  for (int i = 0; i < n; ++i){
    if (e[i].size() == mx.first){
      v.push_back(i);
    }
  }
  for (int i = 0, f = 0; i < v.size() && !f; ++i){
    for (int j = i + 1; j < v.size(); ++j){
      if (e[i].count(j) == 0){
        mx.second = i; f = 1;
        break;
      }
    }
  }
  int ans = 1;
  ans += (mx.first - 1);
  for (auto v : e[mx.second]){
    e[v].erase(mx.second);
  }
  e[mx.second].clear();
  mx = {0, 0};
  for (int i = 0; i < n; ++i){
    mx = max(mx, {e[i].size(), i});
  }
  ans += (mx.first - 1);
  cout << ans << '\n';
}

void burn(){
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i){
    int x, y;
    cin >> x >> y;
  }
}

void reveal(){
  int n;
  cin >> n;
  cout << n << '\n';
  for (int i = 0; i < n - 1; ++i){
    int x, y;
    cin >> x >> y;
    cout << x << ' ' << y << '\n';
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  cin >> test;
  if (false){
    for (int i = 0; i < 40; ++i){
      burn();
    }
    for (int i = 0; i < test - 40; ++i){
      reveal();
    }
  }else{
  while (test--) {
    solve();
  }
  }
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n;
  cin >> n;
  vector<set<int>> e(n, set<int>());
  for (int i = 0; i < n - 1; ++i){
    int x, y;
    cin >> x >> y;
    e[--x].insert(--y);
    e[y].insert(x);
  }
  pt mx = {0, 0};
  for (int i = 0; i < n; ++i){
    mx = max(mx, {e[i].size(), i});
  }
  vector<int> v;
  for (int i = 0; i < n; ++i){
    if (e[i].size() == mx.first){
      v.push_back(i);
    }
  }
  for (int i = 0, f = 0; i < v.size() && !f; ++i){
    for (int j = i + 1; j < v.size(); ++j){
      if (e[v[i]].count(v[j]) == 0){
        mx.second = v[i]; f = 1;
        break;
      }
    }
  }
  int ans = 1;
  ans += (mx.first - 1);
  for (auto v : e[mx.second]){
    e[v].erase(mx.second);
  }
  e[mx.second].clear();
  mx = {0, 0};
  for (int i = 0; i < n; ++i){
    mx = max(mx, {e[i].size(), i});
  }
  ans += (mx.first - 1);
  cout << ans << '\n';
}

void burn(){
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i){
    int x, y;
    cin >> x >> y;
  }
}

void reveal(){
  int n;
  cin >> n;
  cout << n << '\n';
  for (int i = 0; i < n - 1; ++i){
    int x, y;
    cin >> x >> y;
    cout << x << ' ' << y << '\n';
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  cin >> test;
  if (false){
    for (int i = 0; i < 40; ++i){
      burn();
    }
    for (int i = 0; i < test - 40; ++i){
      reveal();
    }
  }else{
  while (test--) {
    solve();
  }
  }
}