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

Solution 1 (runtime: 406 ms):
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        vector<set<ll>> adj(n);
        vector<ll> deg(n);
        for(ll i = 0; i < n - 1; i++) {
            ll u, v;
            cin >> u >> v;
            u--;
            v--;
            adj[u].insert(v);
            adj[v].insert(u);
            deg[u]++;
            deg[v]++;
        }
        vector<pair<ll, ll>> e;
        for(ll i = 0; i < n; i++) {
            e.push_back({deg[i], i});
        }
        sort(e.rbegin(), e.rend());
        ll ans = 0;
        for(ll i = 0; i < n; i++) {
            for(ll j = i + 1; j < n; j++) {
                ll u = e[i].second;
                ll v = e[j].second;
                if(adj[u].count(v)) {
                    continue;
                }
                ans = max(ans, deg[u] + deg[v] - 1);
                break;
            }
        }
        for(ll i = 0; i < n; i++) {
            for(ll j: adj[i]) {
                ans = max(ans, deg[i] + deg[j] - 2);
            }
        }
        cout << ans << '\n';
    }
    return 0;
}

Solution 2 (runtime: 531 ms):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
typedef pair<int, int> pii;

int n, degree[N];
int p[N], eid;
int dp[N][3][2];

void init()
{
    for (int i = 1; i <= n; i++)
    {
        p[i] = -1, degree[i] = 0;
        dp[i][2][1] = dp[i][2][0] = dp[i][1][1] = dp[i][1][0] = 0;
    }
    eid = 0;
}
struct edge
{
    int v, next;
} E[N << 1];
void add(int u, int v)
{
    E[eid].v = v;
    E[eid].next = p[u];
    p[u] = eid++;
}

// dp[u][t] 在以u为根的子树中删了t次，所获得的最大结果

void dfs(int u, int fa)
{
    dp[u][1][1] = degree[u];
    int t1 = 0, t2 = 0;
    for (int i = p[u]; i != -1; i = E[i].next)
    {
        int v = E[i].v;
        if (v == fa)
        {
            continue;
        }
        dfs(v, u);
        dp[u][1][0] = max(dp[u][1][0], max(dp[v][1][0], dp[v][1][1]));
        dp[u][2][1] = max(dp[u][2][1], max(dp[v][1][1] + degree[u] - 2, dp[v][1][0] + degree[u] - 1));
        dp[u][2][0] = max(dp[u][2][0], max(dp[v][2][1], dp[v][2][0]));
    }
    t1 = dp[u][1][0];
    int t = 0;
    for (int i = p[u]; i != -1; i = E[i].next)
    {
        int v = E[i].v;
        if (v == fa)
        {
            continue;
        }
        if (max(dp[v][1][0], dp[v][1][1]) == t1)
        {
            t++;
        }
        if (max(dp[v][1][0], dp[v][1][1]) < t1 && max(dp[v][1][0], dp[v][1][1]) > t2)
        {
            t2 = max(dp[v][1][0], dp[v][1][1]);
        }
    }
    if(t > 1)
    {
        dp[u][2][0] = max(dp[u][2][0], t1 * 2 - 1);
    }
    else
    {
        dp[u][2][0] = max(dp[u][2][0], t1 + t2 - 1);
    }
}


void solve()
{
    cin >> n;
    init();
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        add(u, v), add(v, u);
        degree[u]++, degree[v]++;
    }
    int ans = -1;
    dfs(1, -1);
    for (int i = 1; i <= n; i++)
    {
        ans = max(ans, max(dp[i][2][0], dp[i][2][1]));
    }
    cout << ans << endl;
}

signed main()
{
    int T;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}