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

Previous submission (verdict: Wrong answer on test 2):
#include "bits/stdc++.h"
typedef long long int ll;
#define int  long long int
#define pb(x)  push_back(x)  
#define endl  '\n'
#define IMAX  LLONG_MAX
#define IMIN  LLONG_MIN
int mod=998244353;
using namespace std;
ll max(ll a,ll b)
{
    if(a>b)
    return a;
    return b;
}
ll min(ll a,ll b)
{
    if(a<b)
    return a;
    return b;
}
ll power(ll n,ll k)
{
    if(k==0)
        return 1;
    if(k==1)
        return n;
    ll res=power(n,k/2);
    if(k%2==0)
        return res*res;
    return res*res*n;
}
ll powermod(ll n,ll k,ll mod)
{
    if(k==0)
        return 1;
    if(k==1)
        return n%mod;
    ll res=powermod(n,k/2,mod);
    if(k%2==0)
        return (res*res)%mod;
    return ((res*res)%mod*n)%mod;
}
ll gcd(ll a,ll b)
{
    while(b!=0)
    {
    int temp=a;
    a=b;
    b=temp%b;
    }
    return a;
}
int find(vector<int> &par,int i)
{
    if(par[i]==i)
    return i;
    return par[i]=find(par,par[i]);
}
void _union(vector<int> &rank,vector<int> &par,int i,int j)
{
     i=find(par,i);
     j=find(par,j);
    if(i==j)
    return;
    if(rank[i]>rank[j])
    {
        par[j]=i;
        return;
    }
    if(rank[i]<rank[j])
    {
        par[i]=j;
        return;
    }
    par[i]=j;
    rank[j]++;


}
void update(vector<int> &segtree,int idx,int l,int r,int i,int v)
{
    if(l==r)
    {
        segtree[idx]=v;
        return;
    }
    int mid=(l+r)/2;
    if(i<=mid)
    update(segtree,2*idx+1,l,mid,i,v);
    else
    update(segtree,2*idx+2,mid+1,r,i,v);
    segtree[idx]=gcd(segtree[2*idx+1],segtree[2*idx+2]);
 
}
int find(vector<int> &segtree,int idx,int l,int r,int a,int b)
{
    if(l>b||r<a)
    return 0;
    if(a<=l&&r<=b)
    return segtree[idx];
    int mid=(l+r)/2;
    return gcd(find(segtree,2*idx+1,l,mid,a,b),find(segtree,2*idx+2,mid+1,r,a,b));
}
pair<int,int> dfs(vector<int> adj[],int idx,int par,int &ans)
{
    if(par!=-1)
    {
        ans=max(ans,adj[par].size()+adj[idx].size()-2);
    }
    vector<pair<int,int>> arr;
    int a=adj[idx].size();
    if(par!=-1)
    a--;
    int b=0;
    vector<int> f;
    for(auto i:adj[idx])
    {
        if(i!=par)
        {
            pair<int,int> temp=(dfs(adj,i,idx,ans));
            b=max(b,max(temp.first+1,temp.second));
            arr.push_back(temp);
            f.push_back(temp.second);
            ans=max(ans,temp.second+adj[idx].size());
            
        }
    }
    sort(arr.rbegin(),arr.rend());
    sort(f.begin(),f.end());
    if(arr.size()>=2)
    {
        ans=max(ans,arr[0].first+arr[1].first+1);
        ans=max(ans,arr[0].second+arr[1].second-1);
        int max1=f[0];
        int max2=f[1];
        for(auto p:arr)
        {
            int res=p.first;
            if(max1!=p.second)
            ans=max(ans,res+max1);
            else
            ans=max(ans,res+max2);

        }


    }

    return {a,b};
}


signed main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE

freopen("input.txt", "r", stdin);

freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE

int t;
cin>>t;
while(t--)
{
   int n;
   cin>>n;
   vector<int> adj[n];
   for(int i=-0;i<n-1;i++)
   {
    int a,b;
    cin>>a>>b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
   }
   if(n==2)
   {
    cout<<0<<endl;
    continue;
   }
   int ans=0;
   dfs(adj,0,-1,ans);
   cout<<ans-1<<endl;
   
   

}
}

Submission (verdict: Wrong answer on test 3):
#include "bits/stdc++.h"
typedef long long int ll;
#define int  long long int
#define pb(x)  push_back(x)  
#define endl  '\n'
#define IMAX  LLONG_MAX
#define IMIN  LLONG_MIN
int mod=998244353;
using namespace std;
ll max(ll a,ll b)
{
    if(a>b)
    return a;
    return b;
}
ll min(ll a,ll b)
{
    if(a<b)
    return a;
    return b;
}
ll power(ll n,ll k)
{
    if(k==0)
        return 1;
    if(k==1)
        return n;
    ll res=power(n,k/2);
    if(k%2==0)
        return res*res;
    return res*res*n;
}
ll powermod(ll n,ll k,ll mod)
{
    if(k==0)
        return 1;
    if(k==1)
        return n%mod;
    ll res=powermod(n,k/2,mod);
    if(k%2==0)
        return (res*res)%mod;
    return ((res*res)%mod*n)%mod;
}
ll gcd(ll a,ll b)
{
    while(b!=0)
    {
    int temp=a;
    a=b;
    b=temp%b;
    }
    return a;
}
int find(vector<int> &par,int i)
{
    if(par[i]==i)
    return i;
    return par[i]=find(par,par[i]);
}
void _union(vector<int> &rank,vector<int> &par,int i,int j)
{
     i=find(par,i);
     j=find(par,j);
    if(i==j)
    return;
    if(rank[i]>rank[j])
    {
        par[j]=i;
        return;
    }
    if(rank[i]<rank[j])
    {
        par[i]=j;
        return;
    }
    par[i]=j;
    rank[j]++;


}
void update(vector<int> &segtree,int idx,int l,int r,int i,int v)
{
    if(l==r)
    {
        segtree[idx]=v;
        return;
    }
    int mid=(l+r)/2;
    if(i<=mid)
    update(segtree,2*idx+1,l,mid,i,v);
    else
    update(segtree,2*idx+2,mid+1,r,i,v);
    segtree[idx]=gcd(segtree[2*idx+1],segtree[2*idx+2]);
 
}
int find(vector<int> &segtree,int idx,int l,int r,int a,int b)
{
    if(l>b||r<a)
    return 0;
    if(a<=l&&r<=b)
    return segtree[idx];
    int mid=(l+r)/2;
    return gcd(find(segtree,2*idx+1,l,mid,a,b),find(segtree,2*idx+2,mid+1,r,a,b));
}
pair<int,int> dfs(vector<int> adj[],int idx,int par,int &ans)
{
    if(par!=-1)
    {
        ans=max(ans,adj[par].size()+adj[idx].size()-2);
    }
    vector<pair<int,int>> arr;
    int a=adj[idx].size();
    if(par!=-1)
    a--;
    int b=0;
    vector<int> f;
    for(auto i:adj[idx])
    {
        if(i!=par)
        {
            pair<int,int> temp=(dfs(adj,i,idx,ans));
            b=max(b,max(temp.first+1,temp.second));
            arr.push_back(temp);
            f.push_back(temp.second);
            ans=max(ans,temp.second+adj[idx].size()-1);
            
        }
    }
    sort(arr.rbegin(),arr.rend());
    sort(f.rbegin(),f.rend());
    if(arr.size()>=2)
    {
        ans=max(ans,arr[0].first+arr[1].first+1);
        ans=max(ans,arr[0].second+arr[1].second-1);
        int max1=f[0];
        int max2=f[1];
        for(auto p:arr)
        {
            int res=p.first;
            if(max1!=p.second)
            ans=max(ans,res+max1);
            else
            ans=max(ans,res+max2);

        }


    }

    return {a,b};
}


signed main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE

freopen("input.txt", "r", stdin);

freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE

int t;
cin>>t;
while(t--)
{
   int n;
   cin>>n;
   vector<int> adj[n];
   for(int i=-0;i<n-1;i++)
   {
    int a,b;
    cin>>a>>b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
   }
   if(n==2)
   {
    cout<<0<<endl;
    continue;
   }
   int ans=0;
   dfs(adj,0,-1,ans);
   cout<<ans<<endl;
   
   

}
}