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


Submission (verdict: Wrong answer on test 2):

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FASTIO() \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
using namespace std;

//<------------------------------------------------------------------------------------------------------------------------------------------------------------------------------>
//                                                            DEBUG TEMPLATE
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

// ordered set
// find_by_order, order_of_key
// order_of_key (k) : Number of items strictly smaller than k.
// find_by_order(k) : K-th element in a set (counting from zero)
using namespace __gnu_pbds;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ;
template<class T> using ordered_multiset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
template<class key, class value, class cmp = std::less<key>> using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
struct pair_hash {
    template <class T1, class T2>
    size_t operator()(const std::pair<T1, T2>& p) const {
        auto h1 = std::hash<T1>{}(p.first);
        auto h2 = std::hash<T2>{}(p.second);
        return h1 ^ (h2 << 1);
    }
};
class DSU{
    public:
    vector<int> Rank, parent;
    void make(int n) {
        Rank.resize(n+1,0);
        parent.resize(n+1);
        for (int i = 0; i <= n; i++) {
            parent[i] = i;
        }
    }
    int get(int node) {
        if (node == parent[node])
            return node;
        return parent[node] = get(parent[node]);
    }
    void unite(int u, int v) {
        int ulp_u = get(u);
        int ulp_v = get(v);
        if (ulp_u == ulp_v) return;
        if (Rank[ulp_u] < Rank[ulp_v]) {
            parent[ulp_u] = ulp_v;
        }
        else if (Rank[ulp_v] < Rank[ulp_u]) {
            parent[ulp_v] = ulp_u;
        }
        else {
            parent[ulp_v] = ulp_u;
            Rank[ulp_u]++;
        }
    }
};
//<----------------------------------------------------------------------------------------------------------------------------------------------------------------------------->

void solve(){
    int n; cin >> n;
    vector<int>indegree(n + 1, 0);
    set<vector<int>>edges;
    for(int i = 1; i <= n - 1; ++i){
        int u, v;
        cin >> u >> v;
        edges.insert({u, v});
        indegree[u] += 1;
        indegree[v] += 1;
    }
    int maxnode = -1;
    int maxin = *max_element(indegree.begin(), indegree.end());
    maxnode = find(indegree.begin(), indegree.end(), maxin) - indegree.begin();
    for(auto it = edges.begin(); it != edges.end();){
        auto edge = *it;
        int u = edge[0], v = edge[1];
        if(u == maxnode || v == maxnode){
            indegree[u] -= 1;
            indegree[v] -= 1;
            it = edges.erase(it);
        }
        else{
            ++it;
        }

    }
    if(edges.empty()){
        cout << n - 2 << endl;
        return;
    }
    int maxnode2 = -1;
    int maxin2 = *max_element(indegree.begin(), indegree.end());
    maxnode2 = find(indegree.begin(), indegree.end(), maxin2) - indegree.begin();
    for(auto it = edges.begin(); it != edges.end();){
        auto edge = *it;
        int u = edge[0], v = edge[1];
        if(u == maxnode2 || v == maxnode2){
            indegree[u] -= 1;
            indegree[v] -= 1;
            it = edges.erase(it);
        }
        else{
            ++it;
        }

    }
    if(edges.empty()){
        cout << n - 2 << endl;
        return;
    }
    DSU D;
    D.make(n+1);
    for(auto &edge: edges){
        int u = edge[0], v = edge[1];
        D.unite(u, v);
    }
    unordered_set<int>parent;
    for(int i = 1; i <= n; ++i){
        if(i == maxnode2 || i == maxnode) continue;
        int p = D.get(i);
        parent.insert(p);
    }
    cout << parent.size() << endl;






}
int main() {
    FASTIO();
    int t; t = 1;
    cin >> t;
    while(t--){
        solve();
    }

}