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

Previous submission (verdict: Time limit exceeded on test 5):
#include<bits/stdc++.h>
#define int long long int
#define endl "\n"
#define pb push_back
#define vi vector<int>
#define vv vector<vi>
#define pr pair<int, int>
#define vp vector<pair<int, int>> 
#define all(v) v.begin(), v.end()
#define fr(i, x, n) for(int i=x;i<n;i++)
#define Fr(i, x, n) for(int i=x;i>=n;i--)
#define F first
#define S second
#define speedpenchu cin.tie(0); cout.tie(0);ios_base::sync_with_stdio(0);
using namespace std;
const int MOD = 1000000007;
const int INF = 1E18;

int gcd(int a, int b){if(b == 0) return a;return gcd(b, a%b);}
int add(int a, int b){int m = MOD;return ((a%m)+(b%m)+m)%m;}
int sub(int a, int b){int m = MOD;return ((b%m)-(a%m)+m)%m;}
int mul(int a, int b){int m = MOD;return ((a%m)*(b%m))%m;}


vi adj[200005]; 
void javabu(){ 
    int n; cin>>n; 
    map<int, int> mp;
    set<pr> st;
    fr(i, 0, n) adj[i].clear();
    fr(i, 0, n-1){
        int u, v; cin>>u>>v; --u; --v;
        if(u > v) swap(u, v);
        st.insert({u, v});
        adj[u].pb(v);
        adj[v].pb(u);
    }

    fr(i, 0, n){
        for(auto &node: adj[i]){
            mp[node]++;
        }
    }

    int res = 0, ans = 0;
    fr(i, 0, n){
        for(auto &n : adj[i]){
            res = max(res, mp[i]+mp[n]-2);
        }
    }
    fr(i, 0, n){
        fr(j, i+1, n){
            if(st.find({i, j}) != st.end()) continue;
            res = max(res, mp[i]+mp[j]-1);
        }
    }
    cout<<res<<endl;
    
}


signed main() {
    speedpenchu
    int t;
    cin >> t;
    while (t--) {
        javabu();
    }
    return 0;
}

Submission (verdict: Time limit exceeded on test 5):
#include<bits/stdc++.h>
#define int long long int
#define endl "\n"
#define pb push_back
#define vi vector<int>
#define vv vector<vi>
#define pr pair<int, int>
#define vp vector<pair<int, int>> 
#define all(v) v.begin(), v.end()
#define fr(i, x, n) for(int i=x;i<n;i++)
#define Fr(i, x, n) for(int i=x;i>=n;i--)
#define F first
#define S second
#define speedpenchu cin.tie(0); cout.tie(0);ios_base::sync_with_stdio(0);
using namespace std;
const int MOD = 1000000007;
const int INF = 1E18;

int gcd(int a, int b){if(b == 0) return a;return gcd(b, a%b);}
int add(int a, int b){int m = MOD;return ((a%m)+(b%m)+m)%m;}
int sub(int a, int b){int m = MOD;return ((b%m)-(a%m)+m)%m;}
int mul(int a, int b){int m = MOD;return ((a%m)*(b%m))%m;}

 
void javabu(){ 
    int n; cin>>n; 
    map<int, int> mp;
    set<pr> st;
    fr(i, 0, n-1){
        int u, v; cin>>u>>v; --u; --v;
        st.insert({u, v});
        st.insert({v, u});
        mp[u]++;
        mp[v]++;
    }

    vp indeg;
    for(auto &it : mp){
        indeg.pb({it.S, it.F});
    }
    sort(all(indeg), greater<>());

    int res = 0;
    fr(i, 0, n){
        fr(j, i+1, n){
            if(st.find({indeg[i].S, indeg[j].S}) != st.end()){
                res = max(res, indeg[i].F + indeg[j].F -2);
                break;
            }
        }
    }
    fr(i, 0, n){
        fr(j, i+1, n){
            if(st.find({indeg[i].S, indeg[j].S}) == st.end()){
                res = max(res, indeg[i].F + indeg[j].F -1);
                break;
            }
        }
    }
    cout<<res<<endl;
    
}


signed main() {
    speedpenchu
    int t;
    cin >> t;
    while (t--) {
        javabu();
    }
    return 0;
}