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


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int 
ll mod = 1e9+7;

struct Node
{
    int key;
    Node *left,*right;
    Node(int k)
    {
        key = k;
        left = right = NULL;
    }
};


void dfs(vector<ll> adj[],ll source,vector<bool> visited,ll n)
{
    visited[source]=true;
    //curr++;
    for(auto x:adj[source])
    {
        if(!visited[x])
        {
            dfs(adj,x,visited,n);
        }
    }
}


ll pow1(ll a,ll b)
{
    if(b==0)
    {
        return 1;
    } 
    if(b%2==1)
    {
        return (a*pow1(a,b-1))%mod;
    }
    return ((pow1((a)%mod,(b/2)%mod))*(pow1(a%mod,(b/2)%mod)))%mod;
}


ll pow2(ll n,ll k)
{
    if(k==0)
    {
        return 1;
    }
    if(k%2==0)
    {
        return pow2(n,k/2)*pow2(n,k/2);
    }else
    {
        return pow2(n,k-1)*n;
    }
}


bool compsum(pair<ll,ll> a, pair<ll,ll> b)
{
    return (a.first+a.second)<(b.first+b.second);
}

bool comp(pair<ll,ll> a,pair<ll,ll>b)
{
     return (max(a.first,a.second)<max(b.first,b.second));
}


bool comp1(pair<ll,ll>a,pair<ll,ll>b)
{
    return (max(a.first,a.second)==max(b.first,b.second))&& (min(a.first,a.second)<min(b.first,b.second));
}
 
 

void solve()
{ 
     ll n;cin>>n;
     vector<pair<ll,ll>> vp;
     for(ll i=0;i<(n-1);i++)
     {
         ll u,v;cin>>u>>v;
         vp.push_back({u,v});
     }
     
     if(n==2)
     {
         cout<<0<<endl;
         return;
     }else if(n==3)
     {
         cout<<1<<endl;
         return;
     }else if(n==4)
     {
         cout<<2<<endl;
         return;
     }else if(n==5)
     {
         cout<<3<<endl;
         return;
     }
     //vector<<pair<ll,ll>,bool>> check(n,false);
     map<pair<ll,ll>,bool> check;
     for(ll i= 0;i<(n-1);i++)
     {
         check[{vp[i].first,vp[i].second}] = true;
     }
     
      
     ll cnt[n+1];
     cnt[0] = 0;
     for(ll i= 1;i<=n;i++)
     {
         cnt[i] = 0;
     }
     
     for(ll i=0;i<(n-1);i++)
     {
         cnt[vp[i].first]++;
         cnt[vp[i].second]++;
     }
     ll max1 = 0;
     ll idx_max1 = 0;
     for(ll i= 1;i<=n;i++)
     {
         
         if(cnt[i]>max1)
         {
             max1 = cnt[i];
             idx_max1 = i;
         }
     }
     
     ll max2 = 0;
     for(ll i= 1;i<=n;i++)
     {
         if(i!=idx_max1)
         {
             max2 = max(max2,cnt[i]);
         }
     }
     //cout<<max1<<" "<<max2<<endl;
     //return;
     
     if(max1==max2)
     {
         //check connected.
         ll val1 = 0;
         ll val2 = 0;
         ll idx_temp  = -1;
         for(ll i=1;i<=n;i++)
         {
             if(cnt[i]==max1)
             {
                 idx_temp = i;
                 val1 = i;break;
             }
         }
         for(ll i = idx_temp+1;i<=n;i++)
         {
             if(cnt[i]==max2)
             {
                 val2 = i;break;
             }
         }
         if(check[{val1,val2}]==true || check[{val2,val1}]==true)
         {
             cout<<max2+max1-2<<endl;return;
         }else
         {
             cout<<max2+max1-1<<endl;
             return;
         }
         
     }else if(max2<max1)
     {
          ll cnt_max2 = 0;
          vector<ll> all_val2;
          for(ll i=1;i<=n;i++)
          {
              if(cnt[i]==max2)
              {
                  cnt_max2++;
                  all_val2.push_back(i);
              }
          }
          
          if(cnt_max2>1)
          {
              //here i made a mistake may be all max2 are connected to val1.
              ll val11 = 0;
              for(ll i= 1;i<=n;i++)
              {
                  if(cnt[i]==max1)
                  {
                      val11 = i;break;
                  }
              }
              
              for(ll i= 0;i<cnt_max2;i++)
              {
                  if(check[{all_val2[i],val11}]==false && check[{val11,all_val2[i]}]==false)
                  {
                      cout<<max2+max1-1<<endl;
                      return;
                  }
              }
              cout<<max2+max1-2<<endl;
              return;
          }else if(cnt_max2==1)
          {
              //now check connected with max1.
              ll val1 = 0;
              for(ll i= 1;i<=n;i++)
              {
                  if(cnt[i]==max1)
                  {
                      val1 = i;break;
                  }
              }
              
              ll val2 = 0;
              for(ll i=1;i<=n;i++)
              {
                  if(cnt[i]==max2)
                  {
                      val2 = i;break;
                  }
              }
              
              if(check[{val1,val2}]==true || check[{val2,val1}]==true)
              {
                  cout<<max2+max1-2<<endl;
                  return;
              }else
              {
                  cout<<max2+max1-1<<endl;
                  return;
              }
          }
          
     }
     
     
      
}

 

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