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

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


#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
//---- Debugger ---- //
#ifndef ONLINE_JUDGE
#define debarr(a,n) cout<<#a<<" : ";for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define debmat(mat,row,col) cout<<#mat<<" :\n";for(int i=0;i<row;i++) {for(int j=0;j<col;j++) cout<<mat[i][j]<<" ";cout<<endl;}
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}
template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const unordered_set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const unordered_map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const multiset<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T> void dbs(string str, T t) {cout << str << " : " << t << "\n";}
template <class T, class... S> void dbs(string str, T t, S... s) {int idx = str.find(','); cout << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...);}
template <class T> void prc(T a, T b) {cout << "["; for (T i = a; i != b; ++i) {if (i != a) cout << ", "; cout << *i;} cout << "]\n";}
//----------------- //
#else
#define pr(...){}
#define debarr(a,n){}
#define debmat(mat,row,col){}
#endif

#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr);
#define nl cout<<'\n'
#define ppb pop_back
#define pb push_back
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define fr for(int i=0  ; i < n ; i++ )
#define all(a) a.begin(), a.end()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define sortt(ar) sort(ar.begin(),ar.end())
#define int long long
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define spi set<pair<int,int>>
#define pi vector<pair<int,int>>
#define vvi vector<vector<int>>
#define printans(ans) for(auto it:ans)cout<<it<<" ";
#define endl '\n' 
void solve()
{
  int n;
  cin>>n;
  vi g[n+1];
  rep(i,0,n-1)
  {
      int x,y;cin>>x>>y;
      g[x].pb(y);
      g[y].pb(x);
  }
  multiset<pair<int,int>> se;
  for(int i=1;i<=n;i++)
  {
    se.insert({g[i].size(),i});
  }
  auto op = se.rbegin();
  int sum=0;
  if(op->first == 1);
  else
    {
      sum = op->first ;
      int k = op->second;
      se.erase(se.find({op->first , op->second}));
      for(auto child : g[k])
      {
          se.erase(se.find({g[child].size(),child}));
          se.insert({g[child].size()-1 , child});
      }
      auto op = se.rbegin();
      if(op->first==0)
        sum--;
      else if(op->first>1)
          sum+=op->first-1;
    }

  cout<<sum<<endl;

    
}
signed main()
{
   fast
       int t; cin>>t ;   for(int i=0;i<t;i++)
      solve();
 

 }

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


#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
//---- Debugger ---- //
#ifndef ONLINE_JUDGE
#define debarr(a,n) cout<<#a<<" : ";for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define debmat(mat,row,col) cout<<#mat<<" :\n";for(int i=0;i<row;i++) {for(int j=0;j<col;j++) cout<<mat[i][j]<<" ";cout<<endl;}
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}
template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const unordered_set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const unordered_map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const multiset<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T> void dbs(string str, T t) {cout << str << " : " << t << "\n";}
template <class T, class... S> void dbs(string str, T t, S... s) {int idx = str.find(','); cout << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...);}
template <class T> void prc(T a, T b) {cout << "["; for (T i = a; i != b; ++i) {if (i != a) cout << ", "; cout << *i;} cout << "]\n";}
//----------------- //
#else
#define pr(...){}
#define debarr(a,n){}
#define debmat(mat,row,col){}
#endif

#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr);
#define nl cout<<'\n'
#define ppb pop_back
#define pb push_back
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define fr for(int i=0  ; i < n ; i++ )
#define all(a) a.begin(), a.end()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define sortt(ar) sort(ar.begin(),ar.end())
#define int long long
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define spi set<pair<int,int>>
#define pi vector<pair<int,int>>
#define vvi vector<vector<int>>
#define printans(ans) for(auto it:ans)cout<<it<<" ";
#define endl '\n' 
void solve()
{
  int n;
  cin>>n;
  vi g[n+1];
  rep(i,0,n-1)
  {
      int x,y;cin>>x>>y;
      g[x].pb(y);
      g[y].pb(x);
  }
  multiset<pair<int,int>> se;
  for(int i=1;i<=n;i++)
  {
    se.insert({g[i].size(),i});
  }
  int mx=INT_MIN;
  for(int i=1;i<=n;i++)
  {
    int sum = g[i].size();
      se.erase(se.find({sum , i}));
      for(auto child : g[i])
      {
          se.erase(se.find({g[child].size(),child}));
          se.insert({g[child].size()-1 , child});
      }
      auto op = se.rbegin();
      mx = max(mx  , sum + op->first -1);
      for(auto child: g[i])
      {
        se.erase(se.find({g[child].size()-1 , child}));
        se.insert({g[child].size() , child});
      }
      se.insert({g[i].size() , i});
    }
  cout<<mx<<endl;
  }
signed main()
{
   fast
       int t; cin>>t ;   for(int i=0;i<t;i++)
      solve();
 

 }