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

Previous submission (verdict: Wrong answer on test 4):
#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]; 
// int L[200005], R[200005];
void javabu(){ 
    int n; cin>>n; 
    map<int, int> mp;
    fr(i, 0, n) adj[i].clear();
    fr(i, 0, n-1){
        int u, v; cin>>u>>v; --u; --v;
        // L[i] = u; R[i] = v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

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

    set<pr> st; int res = 0;
    for(auto &it : mp){
        st.insert({it.second, it.first});
    }
    int node1 = (*--st.end()).S;
    res += (*--st.end()).F;
    st.erase(--st.end());
    for(auto &n : adj[node1]){
        st.erase({mp[n], n});
        st.insert({--mp[n], n});
    }
    int node2 = (*--st.end()).S;
    res += (*--st.end()).F;

    // DSU dsu(n);
    // fr(i, 0, n){
    //     if(L[i] == node1 || L[i] == node2 || R[i] == node1 || R[i] == node2 ) continue;
    //     dsu.unionBySize(L[i], R[i]);
    // }

    // set<int> res;
    // fr(i, 0, n){
    //     if(i == node1 || i == node2) continue;
    //     res.insert(dsu.findUPar(i));
    // }

    cout<<res-1<<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;}


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;
}