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] = node;
    dp[node] = 1;
    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) {
        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);
        }
        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];
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();
}