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


Submission (verdict: Wrong answer on test 2):
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
//              find_by_order(k) -> iterator to the kth largest element - zero indexed
//              order_of_key(k)  -> no. of items in that are strictly smaller than k
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll,ll>> vpll;
typedef vector<vpll> vvpll;
typedef vector<pair<ld , ld>> vplld;
typedef pair<ll,ll> pll;
typedef vector<pair<ll,pair<ll,ll>>> vppll;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;
#define fi first
#define se second
#define pb push_back
#define yes  cout << "YES" << endl
#define no  cout << "NO" << endl
#define mod1 998244353
#define mod 1000000007
#define print(a) for(auto x:a) cout<<x<<" ";cout<<endl;
#define printarr(nums,n) for(int i = 0 ; i < n  ;i ++) cout << nums[i] << " "; cout << endl;
#define inputarr(nums,n) for(int i=0;i<n;i++) cin>>nums[i];
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()


long long power(long long a,long long b){
    long long res=1;
    while(b){
        if(b%2==1){
            b--;
            res*=a;
        }
        else{
            a*=a;
            b/=2;
        }
    }
    return res;
}

long long mpower(long long a,long long b,long long p){
    long long res=1;
    while(b){
        if(b%2==1){
            b--;
            res=(res*a)%p;
        }
        else{
            a=(a*a)%p;
            b/=2;
        }
    }
    return res;
}

ll modinv(ll a,ll m){
    ll x=mpower(a,m-2,m);
    return x;
}
/*-----------------------------------------------------------------------------------------*/
void solve(){
    ll n;
    cin>>n;

    vector<set<ll>> adj(n+1);

    vector<int> degree(n+1);

    vector<bool> removed(n+1,false);

    for(int i=0;i<n-1;i++){
        ll x,y;
        cin>>x>>y;
        adj[x].insert(y);
        adj[y].insert(x);
    }

    if(n<=2){
        cout<<0<<endl;
        return;
    }

    for(int i=1;i<=n;i++){
        for(auto ele : adj[i]){
            degree[ele]++;
        }
    }

    ll maxIndex=0,maxi=0;

    for(int i=1;i<=n;i++){
        if(degree[i]>maxi){
            maxIndex=i;
            maxi=degree[i];
        }
    }

    removed[maxIndex]=true;

    degree[maxIndex]=-1;

    for(auto ele : adj[maxIndex]){
        adj[ele].erase(maxIndex);
        degree[ele]--;
    }


    maxIndex=0,maxi=-1;

    for(int i=1;i<=n;i++){
        if(degree[i]>maxi){
            maxIndex=i;
            maxi=degree[i];
        }
    }

    for(auto ele : adj[maxIndex]){
        adj[ele].erase(maxIndex);
        degree[ele]--;
    }

    removed[maxIndex]=true;


    ll ans=0;
    vector<bool> vis(n+1,false);

    for(int i=1;i<=n;i++){
        if(!vis[i]&&!removed[i]){
            vis[i]=true;
            ans++;
            queue<ll> q;
            q.push(i);
            while(!q.empty()){
                auto node=q.front();
                q.pop();
                for(auto ele : adj[node]){
                    if(!vis[ele]){
                        q.push(ele);
                        vis[ele]=true;
                    }
                }
            }
        }
    }

//    print(removed);

    cout<<ans<<endl;

}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t--) 
    solve();
    return 0;
}