Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
#define F(i, s, e) for(int i = s; i < e; i++)
#define int long long
#define vi vector<int>
#define pb push_back
#define sp <<' '<<
#define endl '\n'
#define pii pair<int, int>
#define fi first
#define se second

const int inf = 1e15;
const int N = 1e5 + 5;

vi adj[N];
int dp[N][2][2];
int p[N];

void dfs(int node, int par) {
    p[node] = par;
    int chsize = adj[node].size() - (node != 0);
    dp[node][0][1] = chsize;
    pii best1 = {-inf, -inf}, best2 = {-inf, -inf};
    for(int ch : adj[node]) if(ch != par) {
        dfs(ch, node);
        dp[node][0][0] = max(dp[node][0][0], max(dp[ch][0][0], dp[ch][0][1] + 1));

        dp[node][1][1] = max(dp[node][1][1], chsize - 1 + max(dp[ch][0][0], dp[ch][0][1]));

        pii me = max(pii{dp[ch][0][0], 0}, pii{dp[ch][0][1], 1});
        if(best1.fi < 0) {
            best1 = me;
        } else {
            if(best2.fi < 0) best2 = me;
            else {
                if(me > best2) {
                    best2 = me;
                    if(best2 > best1) swap(best1, best2);
                }
            }
        }
    }
    if(best1.se == 1 && best2.se == 1) {
        dp[node][1][0] = best1.fi + best2.fi + 1;
    } else {
        dp[node][1][0] = best1.fi + best2.fi;
    }
}

void solve() {
    int n;
    cin >> n;

    F(i, 0, n) adj[i].clear();
    F(i, 0, n) F(j, 0, 2) F(k, 0, 2) dp[i][j][k] = -inf;

    F(i, 0, n-1) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    dfs(0, 0);

    //F(i, 0, n) {
    //    F(j, 0, 2) F(k, 0, 2) cout << dp[i][j][k] << " ";
    //    cout << endl;
    //}

    int ans = max(dp[0][1][0], dp[0][1][1]);

    cout << max(ans, 0ll) << endl;
}

int32_t main() {
    cin.tie(0); ios_base::sync_with_stdio(0);
    #ifdef Local

        freopen("io.in", "r", stdin);
        freopen("io.out", "w", stdout);
    #endif
    int t = 1;
    cin >> t;
    while(t--) solve();
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
#define F(i, s, e) for(int i = s; i < e; i++)
#define vi vector<int>
#define pb push_back
#define int int_fast64_t
#define sp <<' '<<
#define pii pair<int, int>
#define fi first
#define se second
#define line cout<<"---------------------------------\n";

const int inf = 1e15;
const int N = 2e5+5;
const int mod = 998244353;

vi adj[N];

void solve() {
    int n;
    cin >> n;
    F(i, 0, n) adj[i].clear();
    F(i, 0, n-1) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    multiset<int, greater<int>> cnt;

    F(i, 0, n) cnt.insert(adj[i].size());

    int ans = 0;
    F(i, 0, n) {
        cnt.erase(cnt.find(adj[i].size()));
        for(int nei : adj[i]) cnt.erase(cnt.find(adj[nei].size())), cnt.insert(adj[nei].size() - 1);
        ans = max(ans, (int) adj[i].size() + *cnt.begin() - 1);
        for(int nei : adj[i]) cnt.erase(cnt.find(adj[nei].size() - 1)), cnt.insert(adj[nei].size());
        cnt.insert(adj[i].size());
    }

    cout << ans << endl;
}

int32_t main() {
    #ifdef Local
        freopen("io.in", "r", stdin);
        freopen("io.out", "w", stdout);
    #endif

    int t = 1;
    cin >> t;
    while(t--) solve();
}