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]) {
        ans = max(ans , ma[t]+val[here]-1-((here==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;
}

Submission (verdict: Accepted):
#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 , ma1[SIZE] , deg[SIZE] , ans , ma2[SIZE];
vector<int> g[SIZE] , child[SIZE];

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

void dfs(int here ,int pa) {
    multiset<int> st1 , st2;
    for (auto t : g[here]) {
        if (t == pa) continue;
        ans = max(ans , deg[here]+deg[t]-2);
        dfs(t , here);
        ma2[here] = max(ma2[here] , ma1[t]);
        st1.insert(-ma1[t]);
        st2.insert(ma2[t]);
    }
    if (st2.size() > 0) ans = max(ans , deg[here]+(*st2.rbegin())-1);
    if (st1.size() >= 2) {
        int ret=0;
        auto it = st1.begin();
        ret += -(*it);
        it++;
        ret += -(*it); ret--;
        ans = max(ans , ret);
    }
    ma1[here] = max(deg[here] , ma2[here]);
}

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);
        deg[u]++; deg[v]++;
    }

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

    dfs(1 , -1);

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

int main() {
    FastIO;

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

    return 0;
}