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

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

// अहं कृत्स्नस्य जगतः प्रभवः प्रलयस्तथा ।।6।।

using namespace std;

#define IOS                       \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define endl "\n"
#define int long long
typedef unsigned long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define fi first
#define se second
#define all(arr) arr.begin(), arr.end()
#define MP make_pair
#define pb push_back
#define repeat(i, st, n) for (int i = st; i < n; i++)
const int mod = 1e9 + 7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void print(vi &v)
{
    for (auto it : v)
        cout << it << " ";
    cout << endl;
}
void solve()
{
    int n; cin >> n;
    vector<vector<int>> v(n + 1);
    for(int i = 0; i < n - 1; i++){
        int x, y; cin >> x >> y;
        v[x].pb(y);
        v[y].pb(x);
    }
    vector<int> degree(n + 1, 0);
    for(int i = 1; i <= n; i++) degree[i] = v[i].size();
    int ind  = 0, deg = 0;
    for(int i = 1; i <= n; i++){
        if(degree[i] > deg){
            deg = degree[i];
            ind = i;
        }
    }
    vector<int> vis(n + 1, 0);
    for(auto it : v[ind]) vis[it] = 1;
    vis[ind] = 1, degree[ind] = 0;
    int deg1 = 0, deg2 = 0;
    for(int i = 1; i <= n; i++){
        if(vis[i] == 0){
            if(degree[i] > deg1){
                deg1 = degree[i];
            }
        } else {
            if(degree[i] > deg2){
                deg2 = degree[i];
            }
        }
    }
    cout << max(deg + deg1 - 1, deg + deg2 - 2) << endl;
}

signed main()
{
    IOS

#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

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

Submission (verdict: Time limit exceeded on test 9):
#include <bits/stdc++.h>

// अहं कृत्स्नस्य जगतः प्रभवः प्रलयस्तथा ।।6।।

using namespace std;

#define IOS                       \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define endl "\n"
#define int long long
typedef unsigned long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define fi first
#define se second
#define all(arr) arr.begin(), arr.end()
#define MP make_pair
#define pb push_back
#define repeat(i, st, n) for (int i = st; i < n; i++)
const int mod = 1e9 + 7;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void print(vi &v)
{
    for (auto it : v)
        cout << it << " ";
    cout << endl;
}
int maxi(int ind, vector<vector<int>> &v, int n, int deg, vector<int> &degree){
    vector<int> vis(n + 1, 0);
    for(auto it : v[ind]) vis[it] = 1;  
    vis[ind] = 1, degree[ind] = 0;
    // print(vis);
    int deg1 = 0, deg2 = 0;
    for(int i = 1; i <= n; i++){
        if(vis[i] == 0){
            if(degree[i] > deg1) deg1 = degree[i];
        } else {
            if(degree[i] > deg2) deg2 = degree[i];
        }
    }
    // cout << deg1 << " " << deg2 << endl;
    return max(deg + deg1 - 1, deg + deg2 - 2);
}
void solve()
{
    int n; cin >> n;
    vector<vector<int>> v(n + 1);
    for(int i = 0; i < n - 1; i++){
        int x, y; cin >> x >> y;
        v[x].pb(y);
        v[y].pb(x);
    }
    vector<int> degree(n + 1, 0);
    for(int i = 1; i <= n; i++) degree[i] = v[i].size();
    int ind  = 0, deg = 0;
    for(int i = 1; i <= n; i++){
        if(degree[i] > deg){
            deg = degree[i];
            ind = i;
        }
    }
    vector<pair<int,int>> max_deg;
    for(int i = 1; i <= n; i++){
        if(degree[i] == deg){
            max_deg.pb({i, v[i].size()});
        }
    }
    int ans = 0;
    for(int i = 0; i < max_deg.size(); i++){
        ans = max(ans, maxi(max_deg[i].fi, v, n, max_deg[i].se, degree));
        // cout << maxi(max_deg[i].fi, v, n, max_deg[i].se) << " ";
        // cout << max_deg[i].fi << " " << max_deg[i].se << endl;
    }
    // cout << endl;
    cout << ans << endl;
    // cout << endl;
}

signed main()
{
    IOS

#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

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