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

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

// Fast I/O
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)

// Input/Output file setup for debugging
#ifndef ONLINE_JUDGE
    #define FILE_IO freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);freopen("error.txt", "w", stderr)
#else
    #define FILE_IO
#endif

// Type Definitions
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()
#define vii vector<int>
#define vll vector<ll>
#define ff first
#define ss second
#define yes cout << "YES" << endl
#define no cout << "NO" << endl

// Constants
const ll MOD = 1e9 + 7;     // Modular arithmetic constant

// Debugging Macro (Prints variable values during local testing)
#ifndef ONLINE_JUDGE
    #define debug(x) cerr << #x << " = " << x << endl
    #define debugv(v) cerr << #v << " = "; for (auto i : v) cerr << i << " "; cerr << endl
#else
    #define debug(x)
    #define debugv(x)
#endif

// Utility Functions
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll modAdd(ll a, ll b, ll m = MOD) { return ((a % m) + (b % m)) % m; }
ll modSub(ll a, ll b, ll m = MOD) { return ((a % m) - (b % m) + m) % m; }
ll modMul(ll a, ll b, ll m = MOD) { return ((a % m) * (b % m)) % m; }

// Main problem-solving function
void solve() {
    int n;
    cin >> n;
    set<pair<int,int>> st;
    vii deg(n+1);
    for(int i = 0; i < n-1; ++i){
        int u,v;
        cin >> u >> v;
        st.insert({u,v});
        st.insert({v,u});
        deg[u]++;
        deg[v]++;
    }
    vector<pair<int,int>> b;
    for(int i = 0; i < n; ++i){
        b.pb({deg[i+1],i+1});
    }
    int ans = 0;
    sort(all(b),greater<pair<int,int>>());
    for(int i = 0; i < n; ++i){
        for(int j = i+1; j < n; ++j){
            if(st.find({b[i].ss,b[j].ss}) == st.end()){
                ans = max(ans,b[j].ff + b[i].ff - 1);
            }
        }
    }
    for(auto &a: st){
        ans = max(ans,deg[a.ff] + deg[a.ss] - 2);
    }
    cout << ans << endl;
}

int main() {
    // File and Fast I/O Setup
    FAST_IO;
    FILE_IO;

    // Number of test cases
    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


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

// Fast I/O
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)

// Input/Output file setup for debugging
#ifndef ONLINE_JUDGE
    #define FILE_IO freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);freopen("error.txt", "w", stderr)
#else
    #define FILE_IO
#endif

// Type Definitions
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()
#define vii vector<int>
#define vll vector<ll>
#define ff first
#define ss second
#define yes cout << "YES" << endl
#define no cout << "NO" << endl

// Constants
const ll MOD = 1e9 + 7;     // Modular arithmetic constant

// Debugging Macro (Prints variable values during local testing)
#ifndef ONLINE_JUDGE
    #define debug(x) cerr << #x << " = " << x << endl
    #define debugv(v) cerr << #v << " = "; for (auto i : v) cerr << i << " "; cerr << endl
#else
    #define debug(x)
    #define debugv(x)
#endif

// Utility Functions
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll modAdd(ll a, ll b, ll m = MOD) { return ((a % m) + (b % m)) % m; }
ll modSub(ll a, ll b, ll m = MOD) { return ((a % m) - (b % m) + m) % m; }
ll modMul(ll a, ll b, ll m = MOD) { return ((a % m) * (b % m)) % m; }

// Main problem-solving function
void solve() {
    int n;
    cin >> n;
    set<pair<int,int>> st;
    vii deg(n+1);
    for(int i = 0; i < n-1; ++i){
        int u,v;
        cin >> u >> v;
        st.insert({u,v});
        st.insert({v,u});
        deg[u]++;
        deg[v]++;
    }
    vector<pair<int,int>> b;
    for(int i = 0; i < n; ++i){
        b.pb({deg[i+1],i+1});
    }
    int ans = 0;
    sort(all(b),greater<pair<int,int>>());
    for(int i = 0; i < n; ++i){
        for(int j = i+1; j < n; ++j){
            if(st.find({b[i].ss,b[j].ss}) == st.end()){
                ans = max(ans,b[j].ff + b[i].ff - 1);
                break;
            }
        }
    }
    for(auto &a: st){
        ans = max(ans,deg[a.ff] + deg[a.ss] - 2);
    }
    cout << ans << endl;
}

int main() {
    // File and Fast I/O Setup
    FAST_IO;
    FILE_IO;

    // Number of test cases
    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}