Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 5):
#include<bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
const int maxN = 1e6 + 5;
const int Mod = 1e9 + 7;
const int LG = 21;
int n,mx,mx1,d[maxN];
bool avail[maxN];
vector<int> adj[maxN],p;
priority_queue<pair<int,int>> q;
void input()
{
    cin >> n;
    while (!q.empty()) q.pop();
    p.clear();
    for (int i = 0;i <= n;++i) adj[i].clear();
    for (int i = 1;i < n;++i)
    {
        int u,v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        if (adj[mx].size() < adj[u].size()) mx = u;
        if (adj[mx].size() < adj[v].size()) mx = v;
    }
    for (int i = 1;i <= n;++i)
    {
        if (adj[i].size() == adj[mx].size()) p.push_back(i);
        d[i] = adj[i].size();
        q.push({d[i],i});
    }
}
void solve()
{
    if (n <= 2) {cout << 0 << "\n";return;}
    int res = 0;
    for (int u : p)
    {
        bool flag = false;
        for (int v : adj[u])
        {
            q.push({d[v] - 1,v});
            --d[v];
        }
        while (!q.empty() && (q.top().first != d[q.top().second] || q.top().second == u))
        {
            if (q.top().second == u) flag = true;
            q.pop();
        }
        //cerr << q.size() << " " << u << " " << q.top().second << "\n";
        res = max(res,d[u] + q.top().first - 1);
        for (int v : adj[u])
        {
            q.push({d[v] + 1,v});
            ++d[v];
        }
        if (flag) q.push({d[u],u});
    }
    cout << res << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
//    freopen("1.inp","r",stdin);
//    freopen("1.out","w",stdout);
    int t;
    cin >> t;
    while (t--)
    {
        input();
        solve();
    }
}





Submission (verdict: Wrong answer on test 5):
#include<bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
const int maxN = 1e6 + 5;
const int Mod = 1e9 + 7;
const int LG = 21;
int n,mx,mx1,d[maxN];
bool avail[maxN];
vector<int> adj[maxN],p;
priority_queue<pair<int,int>> q;
void input()
{
    cin >> n;
    while (!q.empty()) q.pop();
    p.clear();
    for (int i = 0;i <= n;++i) adj[i].clear();
    for (int i = 1;i < n;++i)
    {
        int u,v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        if (adj[mx].size() < adj[u].size()) mx = u;
        if (adj[mx].size() < adj[v].size()) mx = v;
    }
    for (int i = 1;i <= n;++i)
    {
        if (adj[i].size() == adj[mx].size()) p.push_back(i);
        d[i] = adj[i].size();
        q.push({d[i],i});
    }
}
void solve()
{
    if (n <= 2) {cout << 0 << "\n";return;}
    int res = -1e9;
    for (int u : p)
    {
        bool flag = false;
        for (int v : adj[u])
        {
            q.push({d[v] - 1,v});
            --d[v];
        }
        while (!q.empty() && (q.top().first != d[q.top().second] || q.top().second == u))
        {
            if (q.top().second == u) flag = true;
            q.pop();
        }
        //cerr << q.size() << " " << u << " " << q.top().second << "\n";
        res = max(res,d[u] + q.top().first - 1);
        for (int v : adj[u])
        {
            q.push({d[v] + 1,v});
            ++d[v];
        }
        if (flag) q.push({d[u],u});
    }
    cout << res << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
//    freopen("1.inp","r",stdin);
//    freopen("1.out","w",stdout);
    int t;
    cin >> t;
    while (t--)
    {
        input();
        solve();
    }
}