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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pii pair<int,int>
#define ll long long int
#define SIZE 200005
#define all(v) v.begin(), v.end()
#define pb push_back
#define MOD 1000000007
#define INF 1000000007
using namespace std;

int n , ma[SIZE] , val[SIZE] , ans;
vector<int> g[SIZE] , child[SIZE];

void init() {
    ans=0;
    for (int i = 1; i <= n; i++) {
        g[i].clear();
        child[i].clear();
        ma[i] = 0;
    }
}

void dfs(int here ,int pa) {
    val[here] = 1;
    for (auto t : g[here]) {
        if (t == pa) continue;
        val[here]++;
        child[here].pb(t);
        dfs(t , here);
    }
}

void solve(int here) {
    for (auto t : child[here]) solve(t);
    vector<int> tmp;
    for (auto t : child[here]) tmp.pb(ma[t]);
    sort(all(tmp));
    reverse(all(tmp));
    if (tmp.size() >= 2) ans = max(ans , tmp[0]+tmp[1]-1);
    for (auto t : child[here]) {
        if (child[t].size() > 0) ans = max(ans , ma[t]+val[here]-1-(here==1));
    }
    ma[here] = max(val[here] , (tmp.size() > 0 ? tmp[0] : -1));
}

void solve() {
    cin >> n;
    for (int i = 1; i <= n-1; i++) {
        int u , v; cin >> u >> v;
        g[u].pb(v); g[v].pb(u);
    }

    if (n == 2) {
        cout << "0\n";
        return;
    }

    dfs(1 , -1);
    val[1]--;
    //for (int i = 1; i <= n; i++) printf("%d " , val[i]);
    //printf("\n");
    solve(1);

    cout << ans << "\n";
}

int main() {
    FastIO;

    int t; cin >> t;
    while(t--) {
        init();
        solve();
    }

    return 0;
}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pii pair<int,int>
#define ll long long int
#define SIZE 200005
#define all(v) v.begin(), v.end()
#define pb push_back
#define MOD 1000000007
#define INF 1000000007
using namespace std;

int n , ma[SIZE] , val[SIZE] , ans;
vector<int> g[SIZE] , child[SIZE];

void init() {
    ans=0;
    for (int i = 1; i <= n; i++) {
        g[i].clear();
        child[i].clear();
        ma[i] = 0;
    }
}

void dfs(int here ,int pa) {
    val[here] = 1;
    for (auto t : g[here]) {
        if (t == pa) continue;
        val[here]++;
        child[here].pb(t);
        dfs(t , here);
    }
}

void solve(int here) {
    for (auto t : child[here]) solve(t);
    vector<int> tmp;
    for (auto t : child[here]) tmp.pb(ma[t]);
    sort(all(tmp));
    reverse(all(tmp));
    if (tmp.size() >= 2) ans = max(ans , tmp[0]+tmp[1]-1);
    for (auto t : child[here]) {
        ans = max(ans , ma[t]+val[here]-1-((here==1) && (child[here].size()==1))-(child[t].size()==0));
    }
    ma[here] = max(val[here] , (tmp.size() > 0 ? tmp[0] : -1));
}

void solve() {
    cin >> n;
    for (int i = 1; i <= n-1; i++) {
        int u , v; cin >> u >> v;
        g[u].pb(v); g[v].pb(u);
    }

    if (n == 2) {
        cout << "0\n";
        return;
    }

    dfs(1 , -1);
    val[1]--;
    //for (int i = 1; i <= n; i++) printf("%d " , val[i]);
    //printf("\n");
    solve(1);

    cout << ans << "\n";
}

int main() {
    FastIO;

    int t; cin >> t;
    while(t--) {
        init();
        solve();
    }

    return 0;
}