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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
//                                       Sk Najir
#ifndef ONLINE_JUDGE
#include "db.hpp"
#else
#define dbg(x)
#define dbg2(x, y)
#define dbg3(x, y, z)
#endif
#define IOS                      \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);

// pre_define
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

// shortcut type
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;

// Constants
const int MOD = 1e9 + 7;

int f(int i, int pr, bool c, int p, vii &adj)
{
    int mx = max(0, (int)adj[i].size() - p)-c; // Ensure mx is non-negative
    int mr = 0, ml = 0;

    for (auto &j : adj[i])
    {
        if (j == pr)
            continue;
        mr = max(mr, f(j, i, c, 0, adj));
        if (c)
        {
            ml = max(ml, f(j, i, !c, 1, adj));
        }
    }
    return max(mr, mx + ml);
}

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

    if (n == 1) // Edge case for a single node
    {
        cout << "0\n";
        return;
    }

    vii adj(n);
    int x, y;
    for (int i = 1; i < n; i++) // Fix loop to run n-1 times
    {
        cin >> x >> y;
        x--, y--;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    cout << f(0, -1, 1, 0, adj) << endl;
}

int main()
{
    IOS;
    int tc = 1;
     cin >> tc;
    for (int t = 1; t <= tc; t++)
        solve();
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
//                                       Sk Najir
#ifndef ONLINE_JUDGE
#include "db.hpp"
#else
#define dbg(x)
#define dbg2(x, y)
#define dbg3(x, y, z)
#endif
#define IOS                      \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);

// pre_define
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

// shortcut type
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;

// Constants
const int MOD = 1e9 + 7;

int f(int i, int pr, bool c, int p, vii &adj)
{
    int mx = max(0, (int)adj[i].size() - p) - c;
    int mr = 0, ml = 0;

    for (auto &j : adj[i])
    {
        if (j == pr)
            continue;
        mr = max(mr, f(j, i, c, 0, adj));
        if (c)
        {
            ml = max(ml, f(j, i, !c, 1, adj));
        }
    }
    return max(mr, mx + ml);
}

void solve()
{
    int n;
    cin >> n;
    int x, y;
    vii adj(n + 1);
    for (int i = 1; i < n; i++)
    {
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    multiset<int> st;
    for (int i = 1; i <= n; i++)
    {
        st.insert(adj[i].size());
    }
    int mx = 0;
    for (int i = 1; i <= n; i++)
    {
        st.erase(st.find(adj[i].size()));
        x = adj[i].size();
        for (auto &j : adj[i])
        {
            st.erase(st.find(adj[j].size()));
            st.insert(adj[j].size() - 1);
        }
        if (!st.empty())
            mx = max(mx, x + (*st.rbegin()) - 1);
        for (auto &j : adj[i])
        {
            st.erase(st.find(adj[j].size() - 1));
            st.insert(adj[j].size());
        }
        st.insert(adj[i].size());
    }
    cout << mx << endl;
}

int main()
{
    IOS;
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++)
        solve();
}