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


Submission (verdict: Wrong answer on test 2):
/*
g++ -std=gnu++20 cf2028.cpp -o cf2028 && ./cf2028 < input.txt > output.txt
*/

#include <vector>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <algorithm>
#include <deque>
#include <stack>
#include <set>
#include <math.h>
#include <queue>
#include <cmath>
#include <numeric>
#include <complex>
// #include <bits/stdc++.h>


using namespace std;

namespace macros {

    bool printDebug = 0;

    template<typename T>
    void print(T arg) {
        if (!printDebug) return;
        cout << arg;
    }

    template<typename T, typename... Args>
    void print(T first, Args... args) {
        if (!printDebug) return;
        cout << first << " ";
        print(args...);
    }
    
    // # define M_PI 3.14159265358979323846
    #define optimiza_io \
        cin.tie(0);     \
        ios_base::sync_with_stdio(0);
    #define ll long long
    #define ld long double
    #define pb push_back
    #define pi pair<ll, ll>
    #define f first
    #define s second
    #define rep(i, a, b) for (ll i = (a); i < (b); i++)
    #define trav(a, x) for (auto &(a) : (x))
    #define endl "\n"
    #define vi vector<ll>
    #define vvi vector<vi>
    #define vpi vector<pi>
    #define sz(a) ((ll)(a).size())
    #define ldd(a) ((ld)(a))
    #define mp(a, b) make_pair((a), (b))
    #define minn(a, b) (a) = min ((a), (b))
    #define maxx(a, b) (a) = max ((a), (b))
    #define all(x) (x).begin(), (x).end()

    template<typename T>
    void printVec(vector<T> arr) {
        trav(u, arr) {
            print(u, " ");
        }
        print(endl);
    }

}
const ll INF = 1e16;
const ll mod = 1e9+7;
const ll maxn = 2e5+2;

using namespace macros;

// TODO make input macro jaja

void solveInc() {
    // find factors of a[0], keep that multiset (vector)

    // for each i>0, find primes that form a[i-1) / gcd(a[i],a[i-1) (or -1 if not subset)
    // aqui forma nuevo arrays the primes 

    // do inc-exc with those ones, and also only allow mults of a[i] (your m is now m/a[i])
}

void bfs (ll node, vi &visited, vvi &adj) {
    visited[node] = true;
    for (auto u : adj[node]){
        if (visited[u]) continue;
        bfs(u, visited, adj);
    }
}

void solve() {
    ll n;
    cin >> n;
    vpi a(n-1);
    
    map<ll, ll> reps;
    for (auto &[l, r] : a) {
        cin >> l >> r;
        l--;
        r--;
        reps[l]++;
        reps[r]++;
    }
    if (n <= 2) {
        cout << 0 << endl;
        return;
    }

    vi temp1(n, 0);
    rep(i, 0, n) {
        temp1[i] = reps[i];
    }

    ll r1 = max_element(all(temp1)) - temp1.begin();

    reps.clear();
    for (auto &[l, r] : a) {
        if (l == r1 || r == r1) continue;
        reps[l]++;
        reps[r]++;
    }
    rep(i, 0, n) {
        temp1[i] = reps[i];
    }

    ll r2 = max_element(all(temp1)) - temp1.begin();

    vi visited(n+1, 0);
    vvi adj(n);

    for (auto &[l, r] : a) {
        if (l == r1 || r == r1) continue;
        if (l == r2 || r == r2) continue;
        adj[l].pb(r);
        adj[r].pb(l);
    }

    ll ans = 0;
    rep(i, 0, n) {
        if (i == r1 || i == r2) continue;
        if (visited[i]) continue;
        bfs(i, visited, adj);
        ans++;
    }
    
    cout << ans << endl;
}

/*
NEW ME: 
- Read test cases and experiment with them, use your page 2!!!
- Ask questions. What to do to achieve this? What's different from usual slower X strat?
- And ofc, observations

ALWAYS REMEMBER:
- Focus on the target at hand! ---- Making observations? Making formula for X? Write your target down!
- Simplify before coding
- Check all test cases!
- Divide problema dificil en problemas pequeños. POR PARTES!
- OBSERVATIONS: Recuerda el significado de su nombre
- EDGE CASES: Yes
- Calm down

- Sometimes try formulazo. Intenta representar el valor de manera matemática at some point.
    - Luego intenta derivar formular o simplificarla, ya de ahí tal vez ya queda el problema
    - Formulazo o upper limits!
- for debugging, read code first before just printing everything haha
- Even if you find a solution, is the process you made necessary? Is there a way to simplify the process or omit some steps?
- If greedy, really check if you cannot phantom a possible edge case where it is out of your control
- Write down the formulas better to facilitate coding
- primero itera los bits en bitmasking!
- Si quiero hacer greedy, piensa en "cuando es que me conviene hacer x?"

OTHER
- its doable
- link your ideas, dont forget them
- remember key aspects, think of even more, optimal

*/


signed main() {
    optimiza_io

    bool multipleTestCases = 1;
    printDebug = 0;

    int t = 1;
    if (multipleTestCases) {
        cin >> t;
    }

    rep(i, 0, t) {
        print("\n---------------------\n");
        solve();
    }
}