Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
/*
    author: Alphabet
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int M = 1e9 + 7;
vector<vector<int>> graph;
int degree[200100];
int visited[200100];
void dfs(int level){
    if(visited[level]) return ;
    visited[level] = 1;
    for(auto v:graph[level]){
        degree[level]++;
        dfs(v);
    }
}
void solve(){
    int n;
    cin >> n;
    graph.resize(n+1);
    for(int i=0; i<n-1; i++){
        int a,b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    memset(degree, 0 , sizeof(degree));
    memset(visited, 0, sizeof(degree));

    for(int i=1; i<=n; i++){
        if(visited[i] == 0){
            dfs(i);
        }
    }

    vector<pair<int, int>> deg;
    for(int i=1; i<=n; i++){
        deg.push_back({-degree[i] , i});
    }
    sort(deg.begin(), deg.end());

    
    int fin = -1;
    int ans = -deg[0].first;
    vector<int> arr;
    for(int i=1; i<=n; i++){
        if(deg[i].first == deg[1].first){
            arr.push_back(deg[i].second);
        }
    }

    set<int> st;
    for(auto v: graph[deg[0].second]){
        st.insert(v);
    }
    for(auto v: arr){
        if(st.find(v) == st.end()){
            fin = max(fin, ans - deg[1].first - 1);
        }
    }
    
    fin = max(fin, ans - deg[1].first - 2);
    cout << fin <<'\n';
    graph.clear();
}
int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

Submission (verdict: Time limit exceeded on test 5):
/*
    author: Alphabet
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int M = 1e9 + 7;
vector<vector<int>> graph;
int degree[200100];
int visited[200100];
void dfs(int level){
    if(visited[level]) return ;
    visited[level] = 1;
    for(auto v:graph[level]){
        degree[level]++;
        dfs(v);
    }
}
void solve(){
    int n;
    cin >> n;
    graph.resize(n+1);
    set<pair<int,int>> edges;
    for(int i=0; i<n-1; i++){
        int a,b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
        edges.insert({a,b});
        edges.insert({b,a});
    }

    memset(degree, 0 , sizeof(degree));
    memset(visited, 0, sizeof(visited));

    for(int i=1; i<=n; i++){
        if(visited[i] == 0){
            dfs(i);
        }
    }

    vector<pair<int, int>> deg;
    for(int i=1; i<=n; i++){
        deg.push_back({degree[i] , i});
    }
    sort(deg.begin(), deg.end(), greater<pair<int,int>>());
    int ans = 0;
    for(int i=0; i<n-1; i++){
        for(int j=i+1; j<n; j++){
            if(edges.find({deg[i].second, deg[j].second}) == edges.end()){
                ans = max(ans , deg[i].first + deg[j].first - 1);
            }
        }
    }

    for(auto v: edges){
        ans = max(ans , degree[v.first] + degree[v.second] - 2);
    }

    cout << ans <<'\n';
    edges.clear();
    graph.clear();
    return;
}
int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}