Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 4):
using namespace std;
#include <bits/stdc++.h>

#define all(A) A.begin(),A.end()
#define fori(i, k, n) for (int i = k; i < n; i++)
#define ln "\n"


vector<vector<int> > adj;
vector<int> deg;
vector<bool> visited;
int n, U, V;

void removeVertex(int u) {
    deg[u] = 0;
    for (const auto &v: adj[u]) {
        adj[v].erase(remove(all(adj[v]), u), adj[v].end());
        deg[v]--;
    }
    adj[u].clear();
}

void dfs(int u) {
    visited[u] = true;
    for (const auto &v: adj[u])
        if (!visited[v])
            dfs(v);
}

void printGraph() {
    fori(u, 0, n) {
        cout << (u + 1) << "->";
        for (const auto &v: adj[u])
            cout << (v + 1) << ", ";
        cout << ln;
    }
}


void solve() {
    cin >> n;
    adj.assign(n, vector<int>());
    deg.assign(n, 0);
    fori(i, 0, n-1) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
        deg[a]++;
        deg[b]++;
    }
    set<pair<int, int> > tree;
    fori(u, 0, n)
        tree.insert({deg[u], u});
    auto first = prev(tree.end());
    tree.erase(first);
    U = first->second;
    removeVertex(U);
    tree.clear();
    fori(u, 0, n) {
        if (u == U)
            continue;
        tree.insert({deg[u], u});
    }
    V = prev(tree.end())->second;
    removeVertex(V);
    visited.assign(n, false);
    //   cout << "U: " << U + 1 << " V: " << V + 1 << ln;
    int ans = 0;
    fori(u, 0, n) {
        if (u != U && u != V && !visited[u]) {
            //cout << "u: " << u + 1 << ln;
            dfs(u);
            ans++;
        }
    }
    cout << ans << ln;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}


Submission (verdict: Wrong answer on test 4):
using namespace std;
#include <bits/stdc++.h>

#define all(A) A.begin(),A.end()
#define fori(i, k, n) for (int i = k; i < n; i++)
#define ln "\n"


vector<unordered_set<int> > adj;
vector<pair<int, int> > deg;
vector<bool> visited;
int n, ans;

void dfs(int u) {
    visited[u] = true;
    for (const auto &v: adj[u])
        if (!visited[v]) {
            ans = max(ans, deg[u].first + deg[v].first - 2);
            dfs(v);
        }
}

bool vecinos(int u, int v) {
    return adj[u].find(v) != adj[u].end();
}


void solve() {
    cin >> n;
    adj.assign(n, unordered_set<int>());
    deg.assign(n, {0, 0});
    visited.assign(n, false);
    fori(i, 0, n)
        deg[i].second = i;
    fori(i, 0, n-1) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        adj[a].insert(b);
        adj[b].insert(a);
        deg[a].first++;
        deg[b].first++;
    }
    ans = 0;
    dfs(0);
    sort(all(deg), greater<pair<int, int> >());
    fori(i, 0, n) {
        fori(j, i+1, n) {
            if (vecinos(deg[i].second, deg[j].second))
                continue;
            ans = max(ans, deg[i].first + deg[j].first - 1);
            cout << ans << ln;
            return;
        }
    }
    cout << ans << ln;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}