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

Code 1:
#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 (x);
}

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

// 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
*/


Code 2:
#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
*/


Answer: 2