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


Submission (verdict: Wrong answer on test 4):

// Radhe Radhe !!!

/*/<------Author: KALKI_P----->/*/

#include<bits/stdc++.h>

// GNU Policy-Based Data Structures
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 

using namespace std;
using namespace __gnu_pbds;
 
/*/---------------------------Defines----------------------/*/
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)x.size())
 
#define ll long long
#define ld long double
#define endl '\n'
#define pb push_back

/*/<--------------------Modular Arithmetic---------------->/*/
const int mod = 1e9 + 7;
const ll inf = 1e18;
const int maxn = 1e5 + 5;

/*/<--------------------Template-------------------->/*/
// Ordered set functions
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; 

/*/<----------------------------NOTE----------------------->/*/

// Read the problem carefully
// Try to find the idea behind the problem
// Have a rough roadmap for the solution u will be going to implement 
// Once u have the roadmap for solution, implement it 

// If that implementation failed then repeat,

/*/<----------------------------Code Begins----------------------->/*/

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

struct DSU{

    vector<ll> parent, rank, size;

    DSU(ll n){
        rank.resize(n+1,0);parent.resize(n+1);size.resize(n+1);
        for(ll i=0;i<=n;i++)
        {
            parent[i] = i;
            size[i] = 1;
        }
    }

    ll findUPar(ll node){
        if(node == parent[node]) return node;
        return parent[node] = findUPar(parent[node]);
    }

    void UBR(ll u, ll v){
        ll ul_u = findUPar(u);
        ll ul_v = findUPar(v);

        if(ul_u == ul_v) return;
        else if(rank[ul_u] < rank[ul_v]){
            parent[ul_u] = ul_v;
        }
        else if(rank[ul_v] < rank[ul_u]){
            parent[ul_v] = ul_u;
        }
        else{
            parent[ul_u] = ul_v;
            rank[ul_v]++;
        }
    }

    void UBS(ll u, ll v){
        ll ul_u = findUPar(u);
        ll ul_v = findUPar(v);

        if(ul_u == ul_v) return;
        else if(size[ul_u] < size[ul_v]){
            parent[ul_u] = ul_v;
            size[ul_v] += size[ul_u];
        }
        else{
            parent[ul_v] = ul_u;
            size[ul_u] += size[ul_v];
        }
    }
};

void dfs(ll node, vector<ll> &vis, vector<set<ll>> &adj) {
    vis[node] = 1;
    for(auto it : adj[node]) {
        if(!vis[it]) {
            dfs(it, vis, adj);
        }
    }
}
 
void solve () {
    
    ll n; cin >> n;
    vector<set<ll>> adj(n+1);
    for(ll i = 0; i < n - 1; i++) {
        ll u, v; cin >> u >> v;
        adj[u].insert(v);
        adj[v].insert(u);
    }
    vector<pair<ll,ll>> max_adj;
    for(ll i = 1; i <= n; i++) {
        max_adj.push_back({adj[i].size(), i});
    }

    sort(allr(max_adj));

    ll node1 = max_adj[0].second, node2 = max_adj[1].second;
    if(adj[node1].find(node2) != adj[node1].end()) {
        ll maxi = max_adj[1].first;
        for(ll i = 2; i < n; i++) {
            if(max_adj[i].first == maxi) {
                if(adj[node1].find(max_adj[i].second) == adj[node1].end()) {
                    node2 = max_adj[i].second;
                    break;
                }
                else if(adj[node2].find(max_adj[i].second) == adj[node1].end()){
                    node1 = max_adj[i].second;
                    break;
                }
                else continue;
            }
        }
    }

    vector<ll> vis(n+1, 0);
    vis[node1] = 1;
    vis[node2] = 1;

    // cout <<node1 << " : " << node2 << endl;
    
    ll cnt = 0;
    for(ll i = 1; i <= n; i++) {
        if(!vis[i]) {
            cnt++;
            dfs(i, vis, adj);
        }
    }

    cout << cnt << endl;

}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int ti = 1;  
    cin >> ti;

    // cout<<fixed<<setprecision(9);
    while (ti--) {
        solve();
    }
    return 0;
 
}