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

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

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

    if (n == 4) {
        cout << 2 << 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;
}

Submission (verdict: Accepted):
#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(degrees.find(adj[node1].size()));
        for (auto e : adj[node1]) {
            degrees.erase(degrees.find(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(degrees.find(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;
}