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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
#include "../debug.h"
#else
#define debug(...)
#endif


using namespace std; 

typedef long long ll;

ll MOD = 998244353; 
const int N = 3e5 + 10;
ll INF = 1e6;
vector<int> length(N);
vector<int> linker(N);


void preprocess() {

}

void solve(int test_case) {
    debug(test_case);
    int n; cin >> n;
    vector<set<int>> adj(n + 1);
    for (int i = 0; i < n - 1; i++) {
        int u, v; cin >> u >> v;
        adj[u].insert(v);
        adj[v].insert(u);
    }

    int best = 0;
    int node = 1;
    for (int i = 1; i <= n; i++) {
        if (adj[i].size() > best) {
            best = adj[i].size();
            node = i;
        }
    }
    int ans = best;
    debug(best, node);

    for (auto e : adj[node]) {
        adj[e].erase(node);
    }

    adj[node].clear();

    best = 0;
    node = 1;
    for (int i = 1; i <= n; i++) {
        if (adj[i].size() > best) {
            best = adj[i].size();
            node = i;
        }
    }

    debug(best, node);

    ans += best - 1;

    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); 
    cout.tie(0);
    int t = 1;
    cin >> t;
    preprocess();
    for (int i = 1; i <= t; i++) {
        solve(i);
    }
    string exit_status = "Successful";
    return 0;
}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
#include "../debug.h"
#else
#define debug(...)
#endif


using namespace std; 

typedef long long ll;

ll MOD = 998244353; 
const int N = 3e5 + 10;
ll INF = 1e6;
vector<int> length(N);
vector<int> linker(N);


void preprocess() {

}

void solve(int test_case) {
    debug(test_case);
    int n; cin >> n;
    vector<set<int>> adj(n + 1);
    for (int i = 0; i < n - 1; i++) {
        int u, v; cin >> u >> v;
        adj[u].insert(v);
        adj[v].insert(u);
    }

    debug(adj);

    int best = 0;
    int node = 1;
    // first cut
    for (int i = 1; i <= n; i++) {
        if (adj[i].size() > best) {
            best = adj[i].size();
            node = i;
        }
    }
    int ans = best;
    debug(best, node);

    // remove edges
    for (auto e : adj[node]) {
        adj[e].erase(node);
    }
    adj[node].clear();


    debug(adj);

    // second cut
    best = 0;
    node = 1;
    for (int i = 1; i <= n; i++) {
        if (adj[i].size() > best) {
            best = adj[i].size();
            node = i;
        }
    }

    debug(best, node);
    //remove edge
    for (auto e : adj[node]) {
        adj[e].erase(node);
    }

    adj[node].clear();

    debug(adj);


    vector<bool> visited(n + 1, false);
    auto dfs = [&](int node, int parent, auto&& dfs) -> void {
        visited[node] = true;
        for (auto neigh : adj[node]) {
            if (neigh == parent) continue;

            dfs(neigh, node, dfs);
        }
    };
    ans = 0;
    for (int i = 1; i <= n; i++) {
        if (visited[i]) continue;
        ans++;
        dfs(i, -1, dfs);
        debug(i);
    }

    cout << ans - 2 << endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); 
    cout.tie(0);
    int t = 1;
    cin >> t;
    preprocess();
    for (int i = 1; i <= t; i++) {
        solve(i);
    }
    string exit_status = "Successful";
    return 0;
}