Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define vinput(v) for(int i=0;i<n;i++){cin>>v[i];}
#define loop(i,m,n) for(int i=m;i<n;i++)
#define vi vector<int>
#define vll vector<long long int>
#define pb push_back
#define ppb pop_back
#define MOD 1000000007
#define MOD1 998244353
#define nl endl 
#define mp make_pair
#define allr(x) (x).rbegin(),(x).rend()
#define ff first
#define sx(x) ((int)(x).size())
#define ss second
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define big1 998244353
#define big2 1000000007
#define all(v) v.begin(),v.end()
typedef long long ll;



void solve() {
    ll n;
    cin >> n;

    vector<vector<int> > g(n+1);

    loop(i,0,n-1)
    {
        int a,b;
        cin>>a>>b;
        g[a].pb(b);
        g[b].pb(a);
    }
    if(n==2)
    {
        cout<<0<<endl;
        return;
    }

    multiset<int> s;
    int ans = 0;;
    loop(i,1,n+1) s.insert(int(g[i].size()));

    loop(i,1,n+1)
    {
        int curr = g[i].size();
        if(s.find(curr)!=s.end()){
             s.erase(int(g[i].size()));
        }
        for(auto j : g[i])
        {
           if(s.find(int(g[j].size()))!=s.end()){
             s.erase(int(g[j].size()));
        }
            s.insert(int(g[j].size())-1);
        }
        curr += *s.rbegin();
        ans = max(ans,curr);

        for(auto j : g[i])
        {
           if(s.find(int(g[j].size()-1))!=s.end()){
             s.erase(int(g[j].size()-1));
           }
            s.insert(int(g[j].size()));
        }

        s.insert(int(g[i].size()));
    }
    cout<<ans-1<<nl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
#endif
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


Submission (verdict: Accepted):
 #include <algorithm>
 #include <iostream>
 #include <bits/stdc++.h>
 using namespace std;
 #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
 #define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
//---MACROS--//
//                                                  -| JAI MAA SARASWATI |-
 #define vinput(v) for(int i=0;i<n;i++){cin>>v[i];}
 #define loop(i,m,n) for(int i=m;i<n;i++)
 #define vi vector<int>
 #define vll vector<long long int>
 #define pb push_back
 #define ppb pop_back
 #define MOD 1000000007
 #define MOD1 998244353
 #define nl "endl" 
 #define mp make_pair
 #define allr(x) (x).rbegin(),(x).rend()
 #define F first
 #define sx(x) ((int)(x).size())
 #define S second
 #define yes cout<<"YES"<<endl
 #define no cout<<"NO"<<endl
 #define big1 998244353
 #define big2 1000000007
 #define all(v) v.begin(),v.end()
 #define test cout<<'B A N K A I'<<endl
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
 int fact(int n) {
     if (n == 0 || n == 1)
     return 1;
     else
     return n * fact(n - 1);
 }
 int gcd(int a, int b)
 {
        return b == 0 ? a : gcd(b, a % b);
 }
 int nCr(int n, int r)
 {
        if(n<=0 || r<0) return 0;
        else if(r>n) return 0;
        return fact(n) / (fact(r) * fact(n - r));
 }
 bool prime(int n) {
        if (n < 2) return false;
        for (int x = 2; x*x <= n; x++) {
                if (n%x == 0) return false;
        }
        return true;
 }
 vector<int> factors(int n) {
        vector<int> f;
        for (int x = 2; x*x <= n; x++) {
        while (n%x == 0) {
                f.push_back(x);
                n /= x;
                }
        }
        if (n > 1) f.push_back(n);
        return f;
 }
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
 void solve()
 {
     ll n;
    cin >> n;

    vector<vector<int> > g(n+1);

    loop(i,0,n-1)
    {
        int a,b;
        cin>>a>>b;
        g[a].pb(b);
        g[b].pb(a);
    }
    if(n==2)
    {
        cout<<0<<endl;
        return;
    }

    multiset<int> s;
    int ans = 0;;
    loop(i,1,n+1) s.insert(int(g[i].size()));
    debug(s);
    loop(i,1,n+1)
    {
        debug(i);
        int curr = g[i].size();
        if(s.find(curr)!=s.end()){
             s.erase(s.find(int(g[i].size())));
        }
        debug(curr);
        debug(s);
        cerr<<"updating childs"<<endl;
        for(auto j : g[i])
        {
           debug(j);
           if(s.find(int(g[j].size()))!=s.end()){
             s.erase(s.find(int(g[j].size())));
        }
            debug(s);
            s.insert(int(g[j].size())-1);
            debug(s);
        }
        curr += *s.rbegin();
        debug(curr);
        ans = max(ans,curr);
        debug(ans);
        cerr<<"updating childs again"<<endl;
        for(auto j : g[i])
        {
           debug(j);
           if(s.find(int(g[j].size()-1))!=s.end()){
             s.erase(s.find(int(g[j].size()-1)));
           }
           debug(s);
            s.insert(int(g[j].size()));
            debug(s);
        }

        s.insert(int(g[i].size()));
        debug(s);
    }
    cout<<ans-1<<endl;
 }
int main(){
        //std::cout<<std::setprecision(10)<<std::fixed;
        ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("Error.txt", "w", stderr);
#endif
        int t = 1;
        cin>>t;
        while(t--){
                solve();
        }
        return 0;
}