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


Submission (verdict: Wrong answer on test 4):

// || =================================================================== ||
// ||                                                                     ||
// ||                   Reach Expert by May 2025                          ||
// ||                                                                     ||
// || =================================================================== ||
#include <bits/stdc++.h>
#define int long long
#define endl "\n";
#define all(a) a.begin(), a.end()     
using namespace std;
long long mod_exp(long long base, long long exp, long long mod) {
    long long result = 1;
    while (exp > 0) {
        if (exp % 2 == 1) {
            result = (result * base) % mod;     
        }
        base = (base * base) % mod; 
        exp /= 2; 
    }
    return result;
}
void dfs(int cur, vector<set<int>> &graph, vector<int> & vis){
    if(vis[cur]) return;
    vis[cur] = 1;
    for(auto v : graph[cur]){
        if(!vis[v]){
            dfs(v, graph, vis);
        }
    }
}
void solve() {
    int n;
    cin >> n;
    vector<set<int>> graph(n + 1);
    for(int i = 1; i <= n - 1; i++){
        int u, v;
        cin >> u >> v;
        graph[u].insert(v);
        graph[v].insert(u);
    }   
    vector<pair<int,int>> vec;
    for(int i = 1; i <= n; i++){
        vec.push_back({(int)graph[i].size(), i});
    }
    sort(all(vec));
    reverse(all(vec));
    // removing first
    for(auto v : graph[vec[0].second]){
        // edge : u -> v
        int u = vec[0].second;
        graph[v].erase(u);
    }
    int second = vec[1].second;
    int maxAns = 0;
    // for(auto p : vec){
    //     cout << p.second << " " << p.first << endl;
    // }
    // cout << "===>" << endl;
    for(int i = 1; i < n; i++){
        int first = vec[0].second;
        int tmp = vec[i].second;
        int cur;
        if(graph[first].count(tmp)){
            cur = vec[i].first - 2;
            if(maxAns <= cur){
                maxAns = cur;
                second = tmp;
            }
        }else{
            cur = vec[i].first - 1;
            if(maxAns <= cur){
                maxAns = cur;
                second = tmp;
            }
        }
        // cout << cur << endl;
    }
    // cout << vec[0].second << endl;
    // cout << second << " $ " << endl;
    // cout << "===>" << endl;
    graph[vec[0].second].clear();
    //removing the second
    for(auto v : graph[second]){
        int u = second;
        graph[v].erase(u);
    }
    graph[second].clear();
    
    vector<int> vis(n + 1, 0);
    vis[vec[0].second] = 1;
    vis[second] = 1;
    // cout << vec[0].second << " " << second << endl;
    // cout << maxAns << endl;
    int cnt = 0;
    for(int i = 1; i <= n ; i++){
        if(vis[i] == 0){
            cnt++;
            dfs(i, graph, vis);
        }
    }
    cout << cnt << endl;
}


signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE 
        freopen("input.txt", "r", stdin);  
        freopen("output.txt", "w", stdout); 
    #endif  
    int t;
    cin >> t;
    while(t--)
        solve();
   
}