Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>

#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)

#define endl "\n"
#define fraction()                \
    cout.unsetf(ios::floatfield); \
    cout.precision(10);           \
    cout.setf(ios::fixed, ios::floatfield);
#define mem(a, b) memset(a, b, sizeof(a))

using namespace std;

typedef long long ll;
typedef double dl;
class tree
{
    vector<vector<ll>> adj;
    // map<ll,ll> size;
    ll n;

public:
    tree(ll n) : n(n)
    {
        adj.resize(n + 1);
        // size.resize(n+1);
        input();
    }
    void input()
    {
        for (ll i = 1; i < n; i++)
        {
            ll u, v;
            cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
            // size[u]++;
            // size[v]++;
        }
        solve();
    }

    void solve()
    {

        ll ans = 0;
        vector<pair<ll, ll>> v;
        for (ll i = 1; i < adj.size(); i++)// 1 indexed 1->n
        {
            v.push_back({adj[i].size(), i});
        }
        // cout<<v.size()<< " "<<n<<endl;
        if (v.size() <= 2)
        {
            cout << 0 << endl;
            return;
        }
        sort(v.begin(), v.end());
        reverse(v.begin(), v.end());
        ll ele1 = v[0].second; // ele with max child
        ans = adj[ele1].size();

        map<ll, bool> m_connected_to_ele1;
        for (ll i = 0; i < adj[ele1].size(); i++)
        {
            m_connected_to_ele1[adj[ele1][i]] = true;
        }
        // cout<<ans<<endl;// 3 hoiche
        // ll max_sec_ans = 0;//ninus o hoite pare
        ll max_sec_ans = - INFINITY;
        for (ll i = 1; i < v.size(); i++)
        {
        //     cout<<v[i].first<<" v v "<<v[i].second<<endl; 
        //     cout<<m_connected_to_ele1[v[i].second]<<endl;
            ll temp = v[i].first - 1;
            // if (m_connected_to_ele1[temp])
            /// prob
            if(m_connected_to_ele1[v[i].second])
            {
                temp--;
            }
            // cout<<temp<<" "<<max_sec_ans<<endl;
            max_sec_ans = max(max_sec_ans, temp);

            // ansmax
        }
        cout<<ans+max_sec_ans<<endl;

        // if(size.size()<=2){
        //     cout<<0<<endl;
        //     return;
        // }
        // ll ele1,ele2;
        // auto it=size.rbegin();
        // ele1=it->first;
        // it++;
        // ele2=it->second;

        // cout<<ele1<<" "<<ele2<<endl;

        // ll ans=0;
        // ans+=adj[ele1].size();
        // ans+=adj[ele2].size()-1;
        // for(ll i=0;i<adj[ele1].size();i++){
        //     if(adj[ele1][i]==ele2){
        //         ans--;
        //         break;
        //     }
        // }
        // cout << ans << endl;
    }
};

void solve(ll tc)
{
    ll n;
    cin >> n;
    tree t(n);
}

int main()
{
    fastio;
    ll t = 1;
    cin >> t;

    for (ll i = 1; i <= t; i++)
    {
        solve(i);
    }
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>

#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)

#define endl "\n"
#define fraction()                \
    cout.unsetf(ios::floatfield); \
    cout.precision(10);           \
    cout.setf(ios::fixed, ios::floatfield);
#define mem(a, b) memset(a, b, sizeof(a))

using namespace std;

typedef long long ll;
typedef double dl;
class tree
{
    vector<vector<ll>> adj;
    // map<ll,ll> size;
    ll n;

public:
    tree(ll n) : n(n)
    {
        adj.resize(n + 1);
        // size.resize(n+1);
        input();
    }
    void input()
    {
        for (ll i = 1; i < n; i++)
        {
            ll u, v;
            cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
            // size[u]++;
            // size[v]++;
        }
        solve();
    }

    ll ret(vector<pair<ll,ll>> &v, int ii)
    {
        ll ans = 0;

        ll ele1 = v[ii].second; // ele with max child
        ans = adj[ele1].size();

        map<ll, bool> m_connected_to_ele1;
        for (ll i = 0; i < adj[ele1].size(); i++)
        {
            m_connected_to_ele1[adj[ele1][i]] = true;
        }
        // cout<<ans<<endl;// 3 hoiche
        // ll max_sec_ans = 0;//ninus o hoite pare
        ll max_sec_ans = -INFINITY;
        for (ll i = 0; i < v.size(); i++)
        {
            if (i == ii)
                continue;
            ll temp = v[i].first - 1;
            // if (m_connected_to_ele1[temp])
            /// prob
            if (m_connected_to_ele1[v[i].second])
            {
                temp--;
            }
            // cout<<temp<<" "<<max_sec_ans<<endl;
            max_sec_ans = max(max_sec_ans, temp);

            // ansmax
        }
        ans += max_sec_ans;
        return ans;
    }

    void solve()
    {

        ll ans = 0;
        vector<pair<ll, ll>> v;
        for (ll i = 1; i < adj.size(); i++) // 1 indexed 1->n
        {
            v.push_back({adj[i].size(), i});
        }
        // cout<<v.size()<< " "<<n<<endl;
        if (v.size() <= 2)
        {
            cout << 0 << endl;
            return;
        }
        sort(v.begin(), v.end());
        reverse(v.begin(), v.end());
        ans = max(ans, ret(v, 0));
        ans = max(ans, ret(v, 1));
        ans = max(ans, ret(v, 2));

        cout << ans << endl;

        // if(size.size()<=2){
        //     cout<<0<<endl;
        //     return;
        // }
        // ll ele1,ele2;
        // auto it=size.rbegin();
        // ele1=it->first;
        // it++;
        // ele2=it->second;

        // cout<<ele1<<" "<<ele2<<endl;

        // ll ans=0;
        // ans+=adj[ele1].size();
        // ans+=adj[ele2].size()-1;
        // for(ll i=0;i<adj[ele1].size();i++){
        //     if(adj[ele1][i]==ele2){
        //         ans--;
        //         break;
        //     }
        // }
        // cout << ans << endl;
    }
};

void solve(ll tc)
{
    ll n;
    cin >> n;
    tree t(n);
}

int main()
{
    fastio;
    ll t = 1;
    cin >> t;

    for (ll i = 1; i <= t; i++)
    {
        solve(i);
    }
    return 0;
}