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

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define lc p<<1
#define rc p<<1|1
//#define int long long
#define mp make_pair
#define vi vector<int>
#define vc vector<char>
#define vpi vector<pair<int,int>>
#define vvi vector<vector<int>>
#define vvc vector<vector<char>>
#define vvpi vector<vector<pair<int,int>>>
typedef long long ll;
typedef tuple<int,int,int> tp;
typedef pair<int,int> PII;
typedef pair<ll,pair<ll,ll>> PIII;
typedef pair<ll,pair<ll,pair<ll,ll>>> pIIII;
const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
void solve() {
    int n;cin >> n;
    vvi g(n + 1, vi());
    vi d(n + 1, 0);
    for(int i = 1; i <= n - 1; i ++) {
        int u, v;cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
        d[u] ++ , d[v] ++;
    }
    
    int ans = 0;
    multiset<int> st;
    for(int i = 1; i <= n; i ++) {
        st.insert(d[i]);
    }

    for(int i = 1; i <= n; i ++) {
        st.erase(d[i]);
        for(auto v : g[i]) {
            st.erase(d[v]);
        }
        if(!st.empty()) {
            ans = max(ans, d[i] + *st.rbegin() - 1);
        }

        st.insert(d[i]);
        for(auto v : g[i]) {
            st.insert(d[v]);
            ans = max(ans, d[i] + d[v] - 2);
        }
    }
    
    cout << ans << "\n";
    
}
signed main() {    
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}
/*
Do smth instead of nothing and stay organized
Don't get stuck on one approach
*/

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define lc p<<1
#define rc p<<1|1
//#define int long long
#define mp make_pair
#define vi vector<int>
#define vc vector<char>
#define vpi vector<pair<int,int>>
#define vvi vector<vector<int>>
#define vvc vector<vector<char>>
#define vvpi vector<vector<pair<int,int>>>
typedef long long ll;
typedef tuple<int,int,int> tp;
typedef pair<int,int> PII;
typedef pair<ll,pair<ll,ll>> PIII;
typedef pair<ll,pair<ll,pair<ll,ll>>> pIIII;
const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
void solve() {
    int n;cin >> n;
    vvi g(n + 1, vi());
    vi d(n + 1, 0);
    for(int i = 1; i <= n - 1; i ++) {
        int u, v;cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
        d[u] ++ , d[v] ++;
    }
    
    int ans = 0;
    multiset<int> st;
    for(int i = 1; i <= n; i ++) {
        st.insert(d[i]);
    }
    // for(auto i : st) {
    //     cerr << "i = " << i << "\n";
    // }
    // st.extract(3);
    // cerr << "++++\n";
    // for(auto i : st) {
    //     cerr << "i = " << i << "\n";
    // }
    for(int i = 1; i <= n; i ++) {
        st.extract(d[i]);
        for(auto v : g[i]) {
            st.extract(d[v]);
        }
        if(!st.empty()) {
            ans = max(ans, d[i] + *st.rbegin() - 1);
        }

        st.insert(d[i]);
        for(auto v : g[i]) {
            st.insert(d[v]);
            ans = max(ans, d[i] + d[v] - 2);
        }
    }
    
    cout << ans << "\n";
    
}
signed main() {    
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}
/*
Do smth instead of nothing and stay organized
Don't get stuck on one approach
*/