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

Previous submission (verdict: Runtime error on test 1):
#include<bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define mod 998244353
#define PII pair<int,int>
#define PLL pair<long long,long long>
#define PCC pair<char,char>
const int N = 200005;
using namespace std;
typedef long long ll;
typedef long double ld;
const int dirs_4[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
const int dirs_8[8][2] = { {0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
ll quick_pow(ll a, ll b) {
    ll s = 0;
    while (b) {
        if (b & 1)s^=a;
        b >>= 1;
        a = (1&a)^(1&a);
    }
    return s;
}
ll gcd(ll x, ll y) {
    return y == 0 ? x : gcd(y, x % y);
}
ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}
ll lowbit(ll x) {
    return x & -x;
}
ll get_dist(int x1,int y1,int x2,int y2){
    return pow(x1-x2,2)+pow(y1-y2,2);
}
int u[N],v[N],f[N],g[N];
int Find1(int x){
    return x==f[x]?x:f[x]=Find1(f[x]);
}
int Find2(int x){
    return x==g[x]?x:g[x]=Find2(g[x]);
}
void solve()
{
    int n;
    cin>>n;
    vector<vector<int>>g(n+1);
    vector<int>deg(n+1);
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        deg[u]++,deg[v]++;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    int ans=0;
    multiset<int>st;
    for(int i=1;i<=n;i++){
        st.emplace(deg[i]);
    }
    for(int i=1;i<=n;i++){
        for(auto&j:g[i]){
            ans=max(ans,deg[i]+deg[j]-2);
            st.erase(st.find(deg[j]));
        }
        st.erase(deg[i]);
        if(!st.empty()) 
            ans=max(ans,deg[i]+*st.rbegin()-1);
        st.emplace(deg[i]);
        for(auto&j:g[i]){
            st.emplace(deg[j]);
        }
    }
    cout<<ans<<endl;

}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    t=1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}



Submission (verdict: Accepted):
#include<bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define mod 998244353
#define PII pair<int,int>
#define PLL pair<long long,long long>
#define PCC pair<char,char>
const int N = 200005;
using namespace std;
typedef long long ll;
typedef long double ld;
const int dirs_4[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
const int dirs_8[8][2] = { {0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
ll quick_pow(ll a, ll b) {
    ll s = 0;
    while (b) {
        if (b & 1)s^=a;
        b >>= 1;
        a = (1&a)^(1&a);
    }
    return s;
}
ll gcd(ll x, ll y) {
    return y == 0 ? x : gcd(y, x % y);
}
ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}
ll lowbit(ll x) {
    return x & -x;
}
ll get_dist(int x1,int y1,int x2,int y2){
    return pow(x1-x2,2)+pow(y1-y2,2);
}
int u[N],v[N],f[N],g[N];
int Find1(int x){
    return x==f[x]?x:f[x]=Find1(f[x]);
}
int Find2(int x){
    return x==g[x]?x:g[x]=Find2(g[x]);
}
void solve()
{
    int n;
    cin>>n;
    vector<vector<int>>g(n+1);
    vector<int>deg(n+1);
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        deg[u]++,deg[v]++;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    int ans=0;
    multiset<int>st;
    for(int i=1;i<=n;i++){
        st.emplace(deg[i]);
    }
    for(int i=1;i<=n;i++){
        for(auto&j:g[i]){
            ans=max(ans,deg[i]+deg[j]-2);
            st.erase(st.find(deg[j]));
        }
        st.erase(st.find(deg[i]));
        if(!st.empty()) 
            ans=max(ans,deg[i]+*st.rbegin()-1);
        st.emplace(deg[i]);
        for(auto&j:g[i]){
            st.emplace(deg[j]);
        }
    }
    cout<<ans<<endl;

}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    t=1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}