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


Submission (verdict: Runtime error on test 2):
#include <iostream>
#include <chrono>
#include <bits/stdc++.h>
#include <vector>
#include <limits.h>
#include <cmath>
#include <algorithm>
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define newl cout << "\n";
#define vi vector<int>
#define vpl vector<pair<ll int, ll int>>
#define vpi vector<pair<int, int>>
#define vl vector<ll int>
#define pl pair<ll int, ll int>
#define ml map<ll int, ll int>
#define show(v) cout<<v<<"\n";
#define all(v) v.begin(),v.end()
#define print(v) for(auto it : v){cout << it << ' ';}newl
#define fors(n) for (ll int i = 0; i < n; i++)
#define loop(i,st,en) for (ll int i = st; i < en; i++)
#define loopdec(i,st,en) for(ll int i=st;i>en;i--)
#define inv(v,n) fors(n) cin>>v[i];
#define ina(a,n) fors(n) cin>>a[i];
#define setBits(x) __builtin_popcount(x)
#define setBitsll(x) __builtin_popcountll(x)
#define py show("YES");
#define pn show("NO");
using namespace std;
const int MOD = 998244353;

//policy data structure
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp>  
#include <functional>
using namespace __gnu_pbds;  
  
typedef tree<ll, null_type, less<ll>, rb_tree_tag,  
            tree_order_statistics_node_update>  
    ordered_set;
 
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,  
            tree_order_statistics_node_update>  
    ordered_multiset;

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

class DSU{
public:
    int parent[10001];
    int size[10001];
    void make(int v){
        parent[v] = v;
        size[v] = 1;
        return;
    }
    int findParent(int v){
        if(parent[v]==v){
            return v;
        }
        //Path compression
        return parent[v] = findParent(parent[v]);
    }
    void Union(int a, int b){
        a = findParent(a);
        b = findParent(b);
        if(a!=b){
            //union by size;
            if(size[a]<size[b]){
                swap(a,b);
            } 
            parent[b] = a;
            size[a] += size[b];
        }
    }
};

ll int binsearch(ll int arr[],ll int l,ll int r,ll int x)
{
    while (l <= r) {
        ll int m = l + (r - l) / 2;

        if (arr[m] == x)
            return m;

        if (arr[m] < x)
            l = m + 1;

        else
            r = m - 1;
    }
    return -1;
}

int getBit(ll int num, ll int pos){
    return num & (1<<pos);
}

int setBit(ll int num, ll int pos){
    num |= (1<<pos);
    return num;
}

ll int add(ll int x, ll int y) {
  x += y;
  if (x >= MOD) x -= MOD;
  return x;
} 
 
ll int mul(ll int x, ll int y) {
    x %= MOD;
    y %= MOD;
  return x * 1LL * y % MOD;
}

ll int pow(ll int a, ll int b){
    if(b==0){
        return 1;
    }
    else{
        if(b%2==0){
            return mul(pow(a,b/2),pow(a,b/2));
        }
        else{
            return mul(a,mul(pow(a,b/2),pow(a,b/2)));
        }
    }
}

ll int negmod(ll int a,ll int m){
    return ((a%m + m) % m);
}

long long power(long long a, long long b) {
    long long result = 1;
    while(b) {
        if (b & 1) 
            result = mul(result, a);
        a = mul(a,a);
        b >>= 1;
    }
    return result;
}

vector<bool> sieve(ll int n) {
    vector<bool> isPrime(n + 1, true);
    isPrime[0] = isPrime[1] = false;
 
    ll int sqrtN = sqrt(n);
    for (ll int i = 2; i <= sqrtN; ++i) {
        if (isPrime[i]) {
            for (ll int j = i * i; j <= n; j += i) {
                isPrime[j] = false;
            }
        }
    }
 
    return isPrime;
}

ll int gcd (ll int a,ll int b) {
    if (b == 0)
        return a;
    else
        return gcd (b, a % b);
}

//giving minimum prime divisor for a particular index i
vector<ll int> precalc(ll int N) {
    vector<ll int> v(N);
	for(ll int i=0;i<N;i++){
        v[i] = i;
    }
	for (ll int p = 2; p < N; p++) {
		if (v[p] != p)
			continue;
		for (ll int d = 2 * p; d < N; d += p)
			v[d] = min(v[d], p);
	}
    return v;
}

ll int digsum(ll int a) { 
    ll int sum = 0;
    while(a){
        sum += a%10;
        a /= 10;
    } 
    return sum;
}

ll int calculate(ll int p, ll int q) 
{ 
    ll int mod = 1e9 + 7, expo; 
    expo = mod - 2;  
    while (expo) { 
 
        if (expo & 1) { 
            p = (p * q) % mod; 
        } 
        q = (q * q) % mod; 
 
        expo >>= 1; 
    } 
    return p; 
}

int ask(int x, int y) {
	printf("? %d %d\n", x, y);
	fflush(stdout);
	scanf("%d", &x);
	return x;
}

void give(int x, int y) {
	printf("! %d %d\n", x, y);
	fflush(stdout);
}

void dfs(ll int node, ll int parent, vector<vector<ll int>> &edges, vector<ll int> &par, vector<ll int> &numofnodes){
    par[node] = parent;
    for(auto &child: edges[node]){
        if(child==parent) continue;
        dfs(child, node, edges, par, numofnodes);
        numofnodes[node]++;
    }
}

void solve(){
    ll int n;
    cin>>n;
    vector<vector<ll int>> edges(n+1, vector<ll int>());
    fors(n-1){
        ll int a;
        ll int b;
        cin>>a>>b;
        edges[a].pb(b);
        edges[b].pb(a);
    }
    if(n==2){
        show(0);
        return;
    }
    vector<ll int> par(n+1, 0);
    vector<ll int> numofnodes(n+1, 0);
    dfs(1, -1, edges, par, numofnodes);
    // print(par);
    // print(numofnodes);
    map<ll int, ll int> mp;
    for(ll int i=1;i<=n;i++) mp[i] = numofnodes[i];
    set<pair<ll int, ll int>> s;
    ll int mx = (*(max_element(numofnodes.begin(), numofnodes.end())));
    set<ll int> s1;
    for(ll int i=1;i<=n;i++){
        if(numofnodes[i]==mx){
            s1.insert(i);
            continue;
        }
        s.insert({numofnodes[i], i});
    }
    ll int ans = mx;
    ll int a1 = 0;
    for(auto &i: s1){
        ll int parent = par[i];
        auto it = s.end();
        if(parent!=-1){
            auto it = s.find({numofnodes[parent], parent});
            s.erase(it);
            s.insert({numofnodes[parent]-1, parent});
        }
        it = s.end();
        it--;
        ll int c = 0;
        while(c<2){
            if(par[(*(it)).second]!=-1){
                if((*(it)).first==0){
                    a1 = max(a1, 0LL);
                }else{
                    a1 = max(a1, (*(it)).first+1);
                }
            }else{
                a1 = max(a1, (*(it)).first);
            }
            if(it==s.begin()) break;
            it--;
        }
        if(parent!=-1){
            it = s.find({numofnodes[parent]-1, parent});
            s.erase(it);
            s.insert({numofnodes[parent], parent});
        }
    }
    ans += a1;
    cout<<ans<<"\n";
}

int main() {
    fastio();
    auto start1 = chrono::high_resolution_clock::now();
    ll int t;
    cin>>t;
    while(--t>=0){
        solve();
    }
    auto stop1 = chrono::high_resolution_clock::now();
    auto duration = chrono::duration_cast<chrono::milliseconds>(stop1 - start1);
    // cerr << "Time: " << duration . count() / 1000 << " ms" << endl;
}