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

Previous 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();
   
} 

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);
    vector<pair<int,int>> edges;
    for(int i = 1; i <= n - 1; i++){
        int u, v;
        cin >> u >> v;
        edges.push_back({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));
    // for(auto p : vec){
    //     cout << p.first << " : " << p.second << endl;
    // }
    int ans = 0;
    for(int i = 0 + 1; i < n; i++){
        int v = vec[0].second;
        int u = vec[i].second;
        if(graph[v].count(u)){
            ans = max(vec[0].first + vec[i].first - 2, ans);
        }else{
            ans = max(vec[0].first + vec[i].first - 1, ans);
            break;
        }
    }
    
    cout << ans << 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();
   
}