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

Solution 1 (runtime: 687 ms):
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
    int n;
    cin >> n;
    vector<vector<int>> e;
    e.assign((n + 1), vector<int>());
    for (int i = 1; i < n; i++)
    {
        int x, y;
        cin >> x >> y;
        e[x].push_back(y);
        e[y].push_back(x);
    }
    multiset<int> v;
    for (int i = 1; i <= n; i++)
        v.insert(e[i].size());
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        v.extract(e[i].size());
        int sum = 1;
        sum += e[i].size() - 1;
        for (auto it : e[i])
        {
            v.extract(e[it].size());
            v.insert(e[it].size() - 1);
        }
        sum += *v.rbegin() - 1;
        ans = max(ans, sum);
        for (auto it : e[i])
        {
            v.extract(e[it].size() - 1);
            v.insert(e[it].size());
        }
        v.insert(e[i].size());
    }
    cout << ans << "\n";
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

Solution 2 (runtime: 843 ms):
#include <bits/stdc++.h>
#define pb push_back
#define se second
#define fi first
#define all(x) (x).begin(), (x).end()
#define el "\n"
#define bit(mask,i) ((mask>>i)&1)
#define taskname "test"
using namespace std;

typedef long long ll;
typedef pair<ll,ll> pi;

const int MAXN = 2e5+5;
const ll MOD = 998244353;
const ll INF = 1e18;
const double EPSILON = 0.000001;

// Initial function
ll add_mod (ll &a, ll b) {
    if ((a += b) >= MOD) a -= b;
    return a;
}

ll sub_mod (ll &a, ll b) {
    return (a - b + MOD) % MOD;
}

ll mul_mod (ll &a, ll b) {
    return (a = (1LL * a * b % MOD));
}

void maximize (ll &x, ll val)
{
    x = max (x, val);
}

void minimize (ll &x, ll val)
{
    x = min (x, val);
}

void debug (ll x, ll type = 0)
{
    cout << x;
    cout << (type == 0 ? el : " " );
}
// -----------------------

// Variable
ll n, res;
multiset <ll> m;
vector <ll> adj[MAXN];

// -----------------------


// Setup Function
void del (ll x) {
    auto itr = m.find (x);
    m.erase (itr);
}

// -----------------------

// subtask


// -----------------------


int main ()
{
    if (fopen (taskname".inp", "r"))
    {
        freopen (taskname".inp", "r", stdin);
        freopen (taskname".out", "w", stdout);
    }

    ios_base :: sync_with_stdio (false);
    cin.tie (NULL);
    cout.tie (NULL);

    ll t; cin >> t;
    while (t--) {   
        for (int i = 1; i <= n; i ++) adj[i].clear ();
        m.clear ();
        res = 0;

        cin >> n;
        for (int i = 1; i <= n - 1; i ++){
            ll x, y; cin >> x >> y;
            adj[x].pb (y), adj[y].pb (x);
        }


        for (int i = 1; i <= n; i ++)
            m.insert (adj[i].size());

        for (int i = 1; i <= n; i ++) {
            ll ans = adj[i].size();
            del (ans);

            for (auto u : adj[i]) {
                del (adj[u].size());
                m.insert (adj[u].size() - 1);
            }

            ll maxx = *m.rbegin();
            // cout << maxx << el;
            ans += maxx - 1;
            maximize (res, ans);

            m.insert (adj[i].size());
            for (auto u : adj[i]) {
                m.insert (adj[u].size());
                del (adj[u].size() - 1);
            }
        }

        cout << res << el;
    }
    return 0;
}

/*
{\_/}
(>.<)
/>AC
*/