Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
 
typedef long long int ll;
typedef unsigned long long int ull;
 
//Templates
 //defining template when all elements are distinct 
// template <class T> 
// using indexed_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
 //defining template when duplicate elements are also used
// template <class T> 
// using indexed_set = tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
 
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define per(i,a,b) for(int i = (a); i > (b); i--)
#define repl(i,a,b) for(ll i = (a); i < (b); i++)
#define perl(i,a,b) for(ll i = (a); i > (b); i--)
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
 
const ll Mod=998244353;
const ll mod=1000000007;
const ll MAX_n=1010;
const ll p_size=4*1e6+100;
 
ll power(ll x, ll y, ll m){
    ll res = 1;
    while (y > 0){
        if (y & (ll)1){
            res = (res * x)%m;
        }    
        y = (y >> (ll)1)%m;
        x = (x * x)%m;
    }
    return res%m;
}
 
ll gcd(ll a, ll b){
    if (b == 0ll){
        return a;
    }
    return gcd(b, a % b);
}
 
ll lcm(ll a, ll b){
    ll g=gcd(a,b);
    return (a/g)*b;
}
 
void SieveOfEratosthenes(int n,bool prime[]){
    memset(prime, true, n);
 
    for (int p = 2; p * p <= n-1; p++){
        if (prime[p] == true){
            for (int i = p * p; i <= n-1; i += p){
                prime[i] = false;
            }
        }
    }
 
}
 
// ll modInverse(ll a, ll b){           // (a/b) % p = ((a%p) * (pow(b,p-2) % p))%p
//     ll x=power(a,b)%Mod;
//     ll y=power(x,Mod-2)%Mod;
//     return y;
// }
 
 
// bool sortby(const pair<int,int> &a,const pair<int,int> &b){
//     if(a.first==b.first){
//         return a.second < b.second;
//     } 
//     return a.first < b.first;
    
// }
 
 
// class CompareDist
// {
// public:
//     bool operator()(pair<int,int> n1,pair<int,int> n2) {
//         if(n1.first==n2.first){
//             return n1.second>n2.second;
//         }
//         return n1.first<n2.first;
//     }
// };
 
// bool sortby(pair<int,int> a, pair<int,int> b){
//     if(a.first==b.first){
//         return a.second < b.second;
//     }
//     return a.first > b.first;
// }
 
// class compare{
//     bool operator()(pair<int,int> &a, pair<int,int> &b){
//         return a.second<b.second;
//     }
// }
 
// ll fact[MAX_n];
// ll invfact[MAX_n];
 
// ll nCr(ll n,ll r, ll m){
//     ll ans=fact[n];
//     ans*=invfact[r];
//     ans%=m;
//     ans*=invfact[n-r];
//     ans%=m;
//     return ans;
// }
 

void reduce_deg(vector<int> &dp, set<int> &s, int val, int n){
    if(val!=0 && val!=n && dp[val-1]>0){
        dp[val-1]--;
        if(dp[val-1]==0) s.erase(val-1);
        if(dp[val-1]==1) s.insert(val-1);
    }
    if(val!=n-1 && val!=(2*n)-1 && dp[val+1]>0){
        dp[val+1]--;
        if(dp[val+1]==0) s.erase(val+1);
        if(dp[val+1]==1) s.insert(val+1);
    }
    if(val<n && dp[val+n]>0){
        dp[val+n]--;
        if(dp[val+n]==0) s.erase(val+n);
        if(dp[val+n]==1) s.insert(val+n);
    }
    if(val>=n && dp[val-n]>0){
        dp[val-n]--;
        if(dp[val-n]==0) s.erase(val-n);
        if(dp[val-n]==1) s.insert(val-n);
    }
}
 
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
 
    
    // manipulated_seive(xn+10);
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
 
    // bool prime[MAX_n];
    // SieveOfEratosthenes(MAX_n,prime);
    // prime[1]=false;
    // indexed_set s;
    // s.insert(2);
    // auto x = s.find_by_order(index);
    // int y=s.order_of_key(value);
    int t; cin>>t;
    while(t-->0){
        int n;
        cin>>n;
        vector<int> arr[n];
        rep(i,0,n-1){
            int a,b; cin>>a>>b;
            arr[--a].pb(--b);
            arr[b].pb(a);
        }
        if(n==2){cout<<"0\n"; continue;}

        vector<pair<int,int>> v;
        rep(i,0,n){
            v.pb({arr[i].size(), i});
        }
        sort(v.begin(), v.end());
        reverse(v.begin(), v.end());
        // rep(i,0,n){
        //     cout<<v[i].first<<" "<<i<<"\n";
        // }
        int ans = 0;
        if(v[0].first==v[1].first){
            if(v[1].first == v[2].first){
                ans = 2*v[0].first - 1;
            }else{
                bool ok=true;
                rep(i,0,v[0].first){
                    if(arr[v[0].second][i] == v[1].second){
                        ok=false; break;
                    }
                }
                if(ok){
                    ans = 2*v[0].first - 1;
                }else{
                    ans = 2*v[0].first - 2;
                }
            }
        }else{
            bool ok=true;
            rep(i,0,v[0].first){
                if(arr[v[0].second][i] == v[1].second){
                    ok=false; break;
                }
            }
            if(ok){
                ans = v[0].first + v[1].first - 1;
            }else{
                bool ok2 = true;
                rep(i,0,v[0].first){
                    if(arr[v[0].second][i] == v[2].second){
                        ok2=false; break;
                    }
                }
                if(ok2){
                    ans = max(v[0].first + v[1].first - 2, v[0].first + v[2].first - 1);
                }else{
                    ans = max(v[0].first + v[1].first - 2, v[1].first + v[2].first - 1);
                }
            }
        }
        cout<<ans<<"\n";

        
        

    }
 
    
    return 0;
}