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];
int p[N];

void dfs(int node, int par) {
    p[node] = par;
    dp[node] = -inf;
    for(int ch : adj[node]) if(ch != par) {
        dfs(ch, node);
        dp[node] = max(dp[node], dp[ch]);
        dp[node] = max(dp[node], (int) adj[ch].size());
    }
}

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);
    }

    dfs(0, 0);

    //F(i, 0, n) cout << dp[i] << " ";
    //cout << endl;

    int ans = 0;

    F(i, 0, n) {
        if((int) adj[i].size() - (i != 0) < 1) continue;
        int bestChild = 0;
        for(int ch : adj[i]) if(ch != p[i]) {
            if(adj[ch].size() > 1) bestChild = max(bestChild, dp[ch]);
            bestChild = max(bestChild, ((int) adj[ch].size()) - 1);
        }
        //cout << i sp bestChild sp (int) adj[i].size() - (i != 0) - 1 + bestChild << endl;
        ans = max(ans, (int) adj[i].size() - (i != 0) - 1 + bestChild);
    }

    cout << ans << 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: 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-1) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    dfs(0, 0);

    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();
}