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

Previous submission (verdict: Wrong answer on test 2):
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;

const int N=2e5+10;
int d[N];
vector<int>g[N];
int cnt[N];
void solve()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        d[i]=0;
        cnt[i]=0;
        g[i].clear();
    }
    int mx1=0;
    int mx2=0;
    for(int i=1;i<=n-1;i++)
    {
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
        d[u]++;
        d[v]++;
    }
    for(int i=1;i<=n;i++)
    {
        cnt[d[i]]++;
        if(d[i]>mx1)
        {
            mx2=mx1;
            mx1=d[i];
        }
        else if(d[i]>mx2)
        {
            mx2=d[i];
        }
    }
    if(cnt[mx1]==1)
    {
        for(int i=1;i<=n;i++)
        {
            
            if(d[i]==mx1)
            {
                
                for(auto v:g[i])
                {
                    if(d[v]==mx2)cnt[mx2]--;
                }
                
            }
        }
        int res=1;
        if(cnt[mx2])
        {
            res+=mx1-1;
            res+=mx2-1;
        }
        else
        {
            res+=mx1-1;
            res+=mx2-2;
        }
        cout<<res<<endl;
    }
    else
    {
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            
            if(d[i]==mx1)
            {
                int st=0;
                for(auto v:g[i])
                {
                    if(d[v]==mx1)st=1;
                }
                if(!st)flag=1;
                
            }
        }
        int res=1;
        if(flag)
        {
            res+=2*mx1-2;
        }
        else
        {
            res+=2*mx1-3;
        }
        cout<<res<<endl;
    }
}


int main()
{
   
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
}


Submission (verdict: Accepted):
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;

const int N=2e5+10;
int d[N];
vector<int>g[N];
int cnt[N];
void solve()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        d[i]=0;
        cnt[i]=0;
        g[i].clear();
    }
    int mx1=0;
    int mx2=0;
    for(int i=1;i<=n-1;i++)
    {
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
        d[u]++;
        d[v]++;
    }
    for(int i=1;i<=n;i++)
    {
        cnt[d[i]]++;
        if(d[i]>mx1)
        {
            mx2=mx1;
            mx1=d[i];
        }
        else if(d[i]>mx2)
        {
            mx2=d[i];
        }
    }
    if(cnt[mx1]==1)
    {
        for(int i=1;i<=n;i++)
        {
            
            if(d[i]==mx1)
            {
                
                for(auto v:g[i])
                {
                    if(d[v]==mx2)cnt[mx2]--;
                }
                
            }
        }
        int res=1;
        if(cnt[mx2])
        {
            res+=mx1-1;
            res+=mx2-1;
        }
        else
        {
            res+=mx1-1;
            res+=mx2-2;
        }
        cout<<res<<endl;
    }
    else
    {
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            
            if(d[i]==mx1)
            {
                int st=0;
                for(auto v:g[i])
                {
                    if(d[v]==mx1)st++;
                }
                if(st<=cnt[mx1]-2)flag=1;
                
            }
        }
       // cout<<flag<<endl;
        int res=1;
        if(flag)
        {
            res+=2*mx1-2;
        }
        else
        {
            res+=2*mx1-3;
        }
        cout<<res<<endl;
    }
}


int main()
{
   
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
}