Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include "bits/stdc++.h"
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define dbg(x...)
#endif
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
const int mod = 1000000007;
// clang-format off
template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }
template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\n'; return cout; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
// #define _GLIBCXX_DEBUG
// clang-format on

void solve()
{
    ll t, m, n, a, b;
    string h;
    cin >> n;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++)
    {
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    int node, maxx = -1;
    for (int i = 0; i < n; i++)
    {
        if ((int)g[i].size() > maxx)
        {
            maxx = g[i].size();
            node = i;
        }
    }

    vector<vector<int>> g1(n);
    for (int i = 0; i < n; i++)
    {
        if (i == node)
            continue;
        for (auto &j : g[i])
            if (j != node)
                g1[i].push_back(j);
    }
    swap(g, g1);
    dbg(g);

    node, maxx = -1;
    for (int i = 0; i < n; i++)
    {
        if ((int)g[i].size() > maxx)
        {
            maxx = g[i].size();
            node = i;
        }
    }
    g1 = vector<vector<int>>(n);
    for (int i = 0; i < n; i++)
    {
        if (i == node)
            continue;
        for (auto &j : g[i])
            if (j != node)
                g1[i].push_back(j);
    }
    swap(g, g1);
    dbg(g);
    vector<bool> vis(n, false);
    function<void(int)> dfs = [&](int u)
    {
        vis[u] = true;
        for (int v : g[u])
        {
            if (!vis[v])
                dfs(v);
        }
    };
    int ans = 0;
    for (int i = 0; i < n; ++i)
    {
        if (vis[i])
            continue;
        ans++;
        dfs(i);
    }
    cout << ans - 2 << endl;
}
int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Submission (verdict: Wrong answer on test 2):
#include "bits/stdc++.h"
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define dbg(x...)
#endif
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
const int mod = 1000000007;
// clang-format off
template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }
template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\n'; return cout; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
// #define _GLIBCXX_DEBUG
// clang-format on

void solve()
{
    ll t, m, n, a, b;
    string h;
    cin >> n;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++)
    {
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<int> v1(n), v2(n, -1e9);
    vector<pair<int, int>> v;
    for (int i = 0; i < n; i++)
    {
        int maxx = 0;
        sort(g[i].begin(), g[i].end());
        for (auto &x : g[i])
            maxx = max(maxx, (ll)g[x].size());
        v1[i] = maxx;
        v.push_back({(ll)g[i].size(), i});
    }
    sort(v.rbegin(), v.rend());
    int cnt = 0;
    int i = 0;
    while (cnt < n && i < n)
    {
        for (int j = 0; j < n; j++)
        {
            if (i != j && !binary_search(g[j].begin(), g[j].end(), i))
            {
                if (v2[j] == -1e9)
                {
                    cnt++;
                    v2[j] = (ll)g[i].size();
                }
            }
        }
        i++;
    }
    dbg(v1, v2);
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        int tp = g[i].size();
        ans = max(ans, tp + v1[i] - 2);
        ans = max(ans, tp + v2[i] - 1);
    }
    cout << ans << endl;
}
int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}