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;
#define endl '\n'
#define Yes cout<<"Yes"<<endl;
#define No cout<<"No"<<endl;
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define all(x) x.begin(),x.end()
#define All(x) x.begin()+1,x.end()
#define int long long 
typedef unsigned long long ull;
typedef long long ll;
typedef __int128 i128;
typedef pair<int,int> pii;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};

const int N=5e5+100;
vector<int>a[N];

void solve(){
    int n;
    cin>>n;
    vector<int>in(n+1);
    for(int i=1;i<=n;i++)a[i].clear();
    for(int i=1;i<n;i++){
        int u,v;
        
        cin>>u>>v;
        a[u].push_back(v);
        a[v].push_back(u);
        in[u]++,in[v]++;
    }
    int id=0;
    int mx=0;
    vector<array<int,4>>pos(n+1);
    for(int i=1;i<=n;i++){
        int x=0;
        int num=0;
        for(auto v:a[i]){
            int y=a[v].size();
            if(y>x){
                x=v;
                num=1;
            }
            else if(y==x){
                num++;
            }
        }
        pos[i]={(int)a[i].size(),x,num,i};
    }
    sort(All(pos),[&](array<int,4>x,array<int,4>y){
        if(x[0]!=y[0])return x[0]>y[0];
        if(x[1]!=y[1])return x[1]<y[1];
        if(x[2]!=y[2])return x[2]<y[2];
        return true;
    });
    // for(int i=1;i<=n;i++){
    //     for(int j=0;j<4;j++)cout<<pos[i][j]<<" ";cout<<endl;
    // }
    id=pos[1][3];
    mx=pos[1][0];
    int ans=mx;
    for(auto v:a[id]){
        in[v]--;
    }
    int res=-1e8;
    for(int i=1;i<=n;i++){
        if(i==id)continue;
        res=max(res,in[i]-1);
    }
    cout<<res+ans<<endl;
}

signed main(){
    ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)solve();
    return 0;
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define Yes cout<<"Yes"<<endl;
#define No cout<<"No"<<endl;
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define all(x) x.begin(),x.end()
#define All(x) x.begin()+1,x.end()
#define int long long 
typedef unsigned long long ull;
typedef long long ll;
typedef __int128 i128;
typedef pair<int,int> pii;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};

const int N=5e5+100;
vector<int>a[N];

void solve(){
    int n;
    cin>>n;
    vector<int>in(n+1);
    for(int i=1;i<=n;i++)a[i].clear();
    
    for(int i=1;i<n;i++){
        int u,v;
        
        cin>>u>>v;
        a[u].push_back(v);
        a[v].push_back(u);
    }
    map<int,int>mp;
    for(int i=1;i<=n;i++){
        mp[a[i].size()]++;
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        mp[a[i].size()]--;
        if(mp[a[i].size()]==0)mp.erase(a[i].size());
        for(auto v:a[i]){
            mp[a[v].size()]--;
            mp[a[v].size()-1]++;
            if(mp[a[v].size()]==0)mp.erase(a[v].size());
        }
        
        auto [u,v]=*mp.rbegin();
        int now=a[i].size()+u-1;
        ans=max(ans,now);
        
        mp[a[i].size()]++;
        for(auto v:a[i]){
            mp[a[v].size()]++;
            mp[a[v].size()-1]--;
            if(mp[a[v].size()-1]==0)mp.erase(a[v].size()-1);
        }
    }
    cout<<ans<<endl;
}

signed main(){
    ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)solve();
    return 0;
}