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

    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;
}

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

    if (n == 2) {
        cout << 0 << endl;
        return;
    }

    if (n == 3) {
        cout << 1 << endl;
        return;
    }

    multiset<int> degrees;

    for (int i = 1; i <= n; i++) {
        degrees.insert(adj[i].size());
    }
    int ans = 0;
    for (int node1 = 1; node1 <= n; node1++) {
        degrees.erase(adj[node1].size());
        for (auto e : adj[node1]) {
            degrees.erase(adj[e].size());
            degrees.insert(adj[e].size() - 1);
        }

        ans = max(ans, (int) adj[node1].size() + (int) *degrees.rbegin() - 1);

        degrees.insert(adj[node1].size());
        for (auto e : adj[node1]) {
            degrees.erase(adj[e].size() - 1);
            degrees.insert(adj[e].size());
        }
    }

    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;
}