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

Previous submission (verdict: Time limit exceeded on test 1):
#include <bits/stdc++.h>
using namespace std;

#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) a / gcd(a, b) * b
#define N cout << "No\n"
#define Y cout << "Yes\n"
#define endl '\n'
#define all(a) a.begin(),a.end()
#define int long long
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ll long long

void solve() {
    int n;
    cin >> n;
    vector <vector <int>> a(n+1);
    for (int i=1; i<n; i++) {
        int u, v;
        cin >> u >> v;
        a[u].push_back(v);
        a[v].push_back(u);
    }
    if (n == 2) {
        cout << 0 << endl;
    } else if (n == 3){
        cout << 1 << endl;
    } else if (n == 4) {
        cout << 2 << endl;
    } else {
        int ans = 1;
        vector <pair <int, int>> num(n+1);
        for (int i=1; i<=n; i++) {
            num[i].first = a[i].size();
            num[i].second = i;
        }
        sort(all(num));
        int p = n;
        while (num[p].first == num[n].first) {
            int res = 1;
            res += num[p].first - 1;
            vector <pair <int, int>> kk(n+1);
            for (int i=1; i<=n; i++) {
                if (i == num[p].second) {
                    continue;
                }
                kk[i].first = a[i].size();
                kk[i].second = i;
            }
            for (auto x : a[num[p].second]) {
                kk[x].first--;
            }
            sort(all(kk));
            res += kk[n].first - 1;
            ans = max(ans, res);
        }
        cout << ans << endl;
    }
}

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


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

#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) a / gcd(a, b) * b
#define N cout << "No\n"
#define Y cout << "Yes\n"
#define endl '\n'
#define all(a) a.begin(),a.end()
#define int long long
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ll long long

void solve() {
    int n;
    cin >> n;
    vector <vector <int>> a(n+1);
    for (int i=1; i<n; i++) {
        int u, v;
        cin >> u >> v;
        a[u].push_back(v);
        a[v].push_back(u);
    }
    if (n == 2) {
        cout << 0 << endl;
    } else if (n == 3){
        cout << 1 << endl;
    } else if (n == 4) {
        cout << 2 << endl;
    } else {
        int ans = 1;
        vector <pair <int, int>> num(n+1);
        for (int i=1; i<=n; i++) {
            num[i].first = a[i].size();
            num[i].second = i;
        }
        sort(all(num));
        int p = n;
        while (num[p].first == num[n].first) {
            int res = 1;
            res += num[p].first - 1;
            vector <pair <int, int>> kk(n+1);
            for (int i=1; i<=n; i++) {
                if (i == num[p].second) {
                    continue;
                }
                kk[i].first = a[i].size();
                kk[i].second = i;
            }
            for (auto x : a[num[p].second]) {
                kk[x].first--;
            }
            sort(all(kk));
            res += kk[n].first - 1;
            ans = max(ans, res);
            p--;
        }
        cout << ans << endl;
    }
}

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