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

Previous submission (verdict: Time limit exceeded on test 9):
/**
 *  author: xing huan liu kong 
 *  created: 2025-02-13 10:46:57
 *  
 *  即使这个图论(world)大的无边无际
 *  我也会深度优先搜索(dfs)找到你..
**/
#include <bits/stdc++.h>
#define ll long long
#define endl '\n'  
#define point(x) fixed<<setprecision(x)
using namespace std;

#define int long long
typedef pair<int, int> PII;
typedef array<int, 3> arr;

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

    vector<PII> p(n, {0, 0});
    vector<vector<int>> e(n);

    for (int i = 0; i < n - 1; i ++ ) {
        int u, v;
        cin >> u >> v;
        u -- , v -- ;
        p[u].first ++ , p[v].first ++ ;
        p[i].second = i;

        e[u].push_back(v);
        e[v].push_back(u);
    }
    p[n - 1].second = n - 1;

    sort(p.rbegin(), p.rend());

    vector<int> f(n, 0);
    for (int i = 0; i < n; i ++ ) {
        f[p[i].second] = i;
    }

    int res = -1;

    for (int i = 0; i < n; i ++ ) {
        int ans = 1;

        for (auto j : e[i]) {
            p[f[j]].first -- , ans ++ ;
        }
        ans -- , p[f[i]].first = 0;

        // cerr << ans << endl;
        // cerr << "idx " << p[f[i]].second << endl;
        
        for (int j = 0; j < n; j ++ ) {
            res = max(res, ans + p[j].first - 1);

            // cerr << ans + p[i].first - 1 << " " << p[i].second << endl;

            if (j + 1 < n && (p[j + 1].first >= p[j].first || p[j + 1].first == 0)) continue;
            else break;
        }
        
        for (auto j : e[i]) {
            p[f[j]].first ++ , p[f[i]].first ++ ;
        }


    }

    cout << res << endl;

}

signed main(void)
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    
    int t = 1;
    cin >> t;

    while(t -- ){
        solve();
    }

    return 0;
}

Submission (verdict: Time limit exceeded on test 9):
/**
 *  author: xing huan liu kong 
 *  created: 2025-02-13 10:46:57
 *  
 *  即使这个图论(world)大的无边无际
 *  我也会深度优先搜索(dfs)找到你..
**/
#include <bits/stdc++.h>
#define ll long long
#define endl '\n'  
#define point(x) fixed<<setprecision(x)
using namespace std;

#define int long long
typedef pair<int, int> PII;
typedef array<int, 3> arr;

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

    vector<PII> p(n, {0, 0});
    vector<vector<int>> e(n);

    for (int i = 0; i < n - 1; i ++ ) {
        int u, v;
        cin >> u >> v;
        u -- , v -- ;
        p[u].first ++ , p[v].first ++ ;
        p[i].second = i;

        e[u].push_back(v);
        e[v].push_back(u);
    }
    p[n - 1].second = n - 1;

    sort(p.rbegin(), p.rend());

    vector<int> f(n, 0);
    for (int i = 0; i < n; i ++ ) {
        f[p[i].second] = i;
    }

    int res = -1;

    for (int i = 0; i < n; i ++ ) {
        int ans = 1;

        for (auto j : e[i]) {
            p[f[j]].first -- , ans ++ ;
        }
        ans -- , p[f[i]].first = 0;
        for (int j = 0; j < n; j ++ ) {
            if (res > ans + p[j].first - 1) {
                break;
            }

            res = max(res, ans + p[j].first - 1);
            // if (j + 1 < n && (p[j + 1].first >= p[j].first || p[j + 1].first == 0)) continue;
            // else break;
        }
        
        for (auto j : e[i]) {
            p[f[j]].first ++ , p[f[i]].first ++ ;
        }


    }

    cout << res << endl;

}

signed main(void)
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    
    int t = 1;
    cin >> t;

    while(t -- ){
        solve();
    }

    return 0;
}