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 fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;




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

    vector<vector<int>> adj(n + 1, vector<int>());
    FOR(i, 1, n - 1){
        int u,v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }

    vector<int> f(n + 1, -1e9);

    int answer = 0;

    auto dfs = [&](auto&& dfs, int x, int p) -> void{
        for(int v : adj[x])if(v != p){
            dfs(dfs, v, x);

            f[x] = max(f[x], max(sz(adj[v]) - 1, f[v]));
            answer = max(answer, sz(adj[x]) + sz(adj[v]) - 2);
            answer = max(answer, sz(adj[x]) + f[v]);
        }
    };

    dfs(dfs, 1, -1);

    cout << answer << "\n";
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

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


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;




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

    vector<vector<int>> adj(n + 1, vector<int>());
    FOR(i, 1, n - 1){
        int u,v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }

    vector<int> f(n + 1, -1e9);

    int answer = 0;

    auto dfs = [&](auto&& dfs, int x, int p) -> void{
        int Mx = -1e9, Mx2 = -1e9;
        for(int v : adj[x])if(v != p){
            dfs(dfs, v, x);

            f[x] = max(f[x], max(sz(adj[v]) - 1, f[v]));

            answer = max(answer, sz(adj[x]) + sz(adj[v]) - 2);
            answer = max(answer, sz(adj[x]) + f[v]);
            answer = max(answer, Mx + f[v] + 1);
            answer = max(answer, Mx + sz(adj[v]));
            answer = max(answer, Mx2 + sz(adj[v]) - 1);

            Mx = max(Mx, f[v]);
            Mx2 = max(Mx2, sz(adj[v]));
        }
        return;
    };

    dfs(dfs, 1, -1);

    cout << answer << "\n";
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

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