Problem: 2063C - Remove Exactly Two
Language: c++23 

Code 1:
#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];
void input()
{
    cin >> n;
    for (int i = 0;i <= n;++i)
    {
        adj[i].clear();
        d[i] = 0;
        avail[i] = true;
    }
    mx = 0;mx1 = 0;
    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;
    }
    avail[mx] = false;
}
void dfs(int u)
{
    avail[u] = false;
    for (int v : adj[u])
    {
        if (!avail[v] || v == mx || v == mx1) continue;
        d[u]++;
        dfs(v);
    }
}
void solve()
{
    mx1 = 0;
    if (n <= 2) {cout << 0 << "\n";return;}
    for (int i = 1;i <= n;++i) if (avail[i]) dfs(i);
    if (mx == 1) mx1 = n;
    else mx1 = 1;
    for (int i = 1;i <= n;++i) if (d[mx1] < d[i]) mx1 = i;
    fill(avail + 1,avail + n + 1,true);
    int res = 0;
    //cerr << mx << " " << mx1 << "aa\n";
    avail[mx] = false;
    avail[mx1] = false;
    for (int i = 1;i <= n;++i)
    {
        if (avail[i])
        {
            dfs(i);
            ++res;
        }
    }
    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();
    }
}





Code 2:
#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();
    mx = 0;
    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();
    }
}





Answer: 2