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


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
// #include <iostream>
#define ll long long
const ll mod = 1e9 + 7;
// vector<ll> v;

#define iofast()                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);

using namespace std;

ll sod(ll n)
{
    ll m;
    ll sum = 0;
    while (n > 0)
    {
        m = n % 10;
        sum += m;
        n /= 10;
    }
    return sum;
}

int smallestDivisor(int n)
{
    // if divisible by 2
    if (n % 2 == 0)
        return 2;

    // iterate from 3 to sqrt(n)
    for (int i = 3; i * i <= n; i += 2)
    {
        if (n % i == 0)
            return i;
    }

    return n;
}

int fact(int n)
{
    if (n == 0 || n == 1)
        return 1;
    return (n * fact(n - 1)) % mod;
}

bool check_prime(int n)
{
    for (int i = 2; i < min(n, 50000); i++)
        if (n % i == 0)
            return 0;
    return 1;
}

int highestPowerof2(int n)
{
    int p = (int)log2(n);
    return (int)pow(2, p);
}

// const ll mod1 = 998244353;

void primeFactors(int n)
{
    // Print the number of 2s that divide n
    vector<ll> v;
    while (n % 2 == 0)
    {
        // cout << 2 << " ";
        v.push_back(2);
        n = n / 2;
    }

    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
            v.push_back(i);
            // cout << i << " ";
            n = n / i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        v.push_back(n);
    // cout << n << " ";
}

bool isPrime(int n)
{

    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}

vector<ll> printDivisors(int n)
{
    vector<ll> v;

    for (int i = 1; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {

            if (n / i == i)
                v.push_back(i);

            else
                v.push_back(i);
            v.push_back(n / i);
        }
    }
    return v;
}

void printSubsequence(int m, string &l1, string &r1, set<string> &s1, string input, string output)
{

    if (input.empty())
    {
        if (output.size() == m)
        {
            ll flag1 = 0;
            if (flag1 == 0)
            {
                s1.insert(output);
            }
        }
        return;
    }

    printSubsequence(m, l1, r1, s1, input.substr(1), output + input[0]);

    printSubsequence(m, l1, r1, s1, input.substr(1), output);
}

bool sortbyCond(const pair<int, int> &a,
                const pair<int, int> &b)
{
    if (a.first != b.first)
        return (a.first < b.first);
    else
        return (a.second > b.second);
}

int power(long long x, unsigned int y, int p)
{
    int res = 1; // Initialize result

    x = x % p; // Update x if it is more than or
               // equal to p

    if (x == 0)
        return 0; // In case x is divisible by p;

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;

        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}

bool cmp(pair<ll, ll> &a, pair<ll, ll> &b)
{
    return a.first * b.second > a.second * b.first;
}

bool cmp1(pair<ll, ll> &a, pair<ll, ll> &b)
{
    return a.first * b.second < a.second * b.first;
}

long long binpow(long long a, long long b)
{
    long long res = 1;
    while (b > 0)
    {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

long long division(long long a, long long b) // where a is dividend and b is divisor
{
    long long sign = 1;
    if ((a < 0) ^ (b < 0)) // - ^ - = +,+ ^ - = - , - ^ + = - , + ^ + = +
    {
        sign = -1;
    }
    long long m = abs(a);
    long long n = abs(b);
    long long count = 0; // for storing the quotient
    long long temp = 0;
    for (int j = 31; j >= 0; --j)
    {

        if (temp + (n << j) <= m)
        {
            temp += n << j;
            count |= 1L << j;
        }
    }
    if (sign == -1) // when sign is negative
    {
        count = -count;
    }
    return count;
}

ll maxSubArraySum(vector<ll> &a, ll size)
{
    ll max_so_far = LONG_LONG_MIN, max_ending_here = 0;

    for (ll i = 0; i < size; i++)
    {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;

        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}

ll decToBinary(int n)
{
    int binaryNum[32];
    int i = 0;
    while (n > 0)
    {
        binaryNum[i] = n % 2;
        n = n / 2;
        i++;
    }
    string s = "";
    for (int j = i - 1; j >= 0; j--)
        s += binaryNum[j] + '0';

    return stol(s);
}

string convert_31_bit_binary_to_string(ll number)
{
    number &= 0x7FFFFFFF;
    bitset<64> binary(number);
    return binary.to_string();
}

bool isPalindrome(string s)
{
    ll n = s.length();
    for (ll i = 0; i < n / 2; i++)
    {
        if (s[i] != s[n - i - 1])
            return false;
    }

    return true;
}

ll ask(ll x, ll y)
{
    cout << "? " << x << " " << y << endl;
    ll x1;
    cin >> x1;
    return x1;
}

vector<ll> generatePrime(ll n)
{
    vector<ll> prime;
    // prime.push_back(1);

    vector<ll> v(n + 1);

    for (ll i = 2; i <= n; i++)
    {
        if (v[i] == 0)
        {
            prime.push_back(i);
            for (ll j = i * i; j <= n; j += i)
            {
                v[j] = 1;
            }
        }
    }

    return prime;
}

ll grefact(ll n, ll k)
{
    ll flag = 0;
    for (ll i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            if ((i % 2 == 1 && i >= k) || ((n / i) % 2 == 1 && n / i >= k))
                return 1;
        }
    }

    return 0;
}

void dfs(ll node, map<ll, set<ll>> &mp, set<ll> &st)
{
    st.insert(node);

    for (auto &it : mp[node])
    {
        if (st.find(it) == st.end())
        {
            dfs(it, mp, st);
        }
    }
}

int main()
{
    iofast();
    ll t = 1;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
        map<ll, set<ll>> mp;
        for (ll i = 0; i < n - 1; i++)
        {
            ll x, y;
            cin >> x >> y;
            mp[x].insert(y);
            mp[y].insert(x);
        }

        ll mx = 0, tp = 0;

        for (ll i = 1; i <= n; i++)
        {
            if (mp.find(i) != mp.end())
            {
                if (mx < mp[i].size())
                {
                    mx = mp[i].size();
                    tp = i;
                }
            }
        }

        // cout << mx << " " << tp << endl;

        for (ll i = 1; i <= n; i++)
        {
            if (mp.find(i) != mp.end())
            {
                if (mp[i].find(tp) != mp[i].end())
                {
                    mp[i].erase(tp);
                }
            }
        }

        mp.erase(tp);

        mx = 0, tp = 0;

        for (ll i = 1; i <= n; i++)
        {
            if (mp.find(i) != mp.end())
            {
                if (mx <= mp[i].size())
                {
                    mx = mp[i].size();
                    tp = i;
                }
            }
        }

        // cout << mx << " " << tp << endl;

        for (ll i = 1; i <= n; i++)
        {
            if (mp.find(i) != mp.end())
            {
                if (mp[i].find(tp) != mp[i].end())
                {
                    mp[i].erase(tp);
                }
            }
        }
        // cout << mx << " " << tp << endl;

        mp.erase(tp);

        ll ct = 0;
        set<ll> st;

        for (auto &it : mp)
        {
            if (st.find(it.first) == st.end())
            {
                ct++;
                dfs(it.first, mp, st);
            }
        }

        cout << ct << endl;
    }
    clock_t tStart = clock();
    cerr << fixed << setprecision(10) << "\nTime Taken: " << (double)(clock() - tStart) / CLOCKS_PER_SEC << endl;

    return 0;
}