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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb emplace_back
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
#define trace(x) cerr << #x << " = " << x << '\n'

using namespace std;

template<class T> bool chmax(T& A, T&& B) { return B > A? A = B, true : false; }
template<class T> bool chmin(T& A, T&& B) { return B < A? A = B, true : false; }

const int64_t DESPACITO = 2e18;
const int INF = 2e9, MOD = 1e9+7;
const int MxN = 2e5 + 5;

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define MOD1 998244353
#define INF1 1e18
#define nline "\n"
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
ll oo=998244353;
ll mul(ll x,ll y)
{
    return ((x%oo)*(y%oo))%oo;
}
ll add(ll x,ll y)
{
    return ((x%oo)+(y%oo))%oo;
}
ll sub(ll x,ll y)
{
    return ((x%oo)-(y%oo)+oo)%oo;
}
ll fast_pow(ll x,ll y)
{
    if(y==0)return 1;
    if(y==1)return x;
    ll ret=1;
    ret=fast_pow(x,y/2);
    ret=mul(ret,ret);
    if(y%2==1)ret=mul(ret,x);
    return ret;
}
ll fact[2000200];
void factorial()
{
    fact[0]=1;
    for(int i=1; i<=1000000; i++)
    {
        fact[i]=mul(fact[i-1],i);
    }
}
ll calculate_inverse(ll x)
{
    return fast_pow(x,oo-2);
}
ll n_choose_k(ll n,ll k)
{
    if(k<0)return 0;
    ll ret=1;
    ret=fact[n];
    ret=mul(ret,calculate_inverse(fact[k]));
    ret=mul(ret,calculate_inverse(fact[n-k]));
    return ret;
}
ll gcd(ll x,ll y)
{
    if(x<y)swap(x,y);
    if(y==0)return x;
    return gcd(y,x%y);
}
struct DSU {
    vector<int> parent, size;
    int components;

    DSU(int n) {
        parent.resize(n);
        size.resize(n, 1);
        components = n;
        iota(parent.begin(), parent.end(), 0);
    }

    int find(int x) {
        if (parent[x] != x) {
            parent[x] = find(parent[x]);
        }
        return parent[x];
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (size[x] < size[y]) swap(x, y);
        parent[y] = x;
        size[x] += size[y];
        components--;
    }
};

void solve() {
    int n;
    cin>>n;
    vector<set<int>>g(n);
    vector<int>deg(n,0);
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        --u; 
        --v;
        deg[u]++;
        deg[v]++;
        g[u].insert(v);
        g[v].insert(u);
    }
    multiset<int>ms;
    for(int i=0;i<n;i++){
        ms.insert(deg[i]);
    }
    int ans=0;
    for(int u=0;u<n;u++){
        ms.erase(g[u].size());
        for(auto v:g[u]){
            ms.erase(g[v].size());
            ans=max(ans,(int)g[u].size()+(int)g[v].size()-2);
        }
        if(!ms.empty()){
            ans=max(ans,(int)g[u].size()+*ms.rbegin()-1);
        }
        ms.insert(g[u].size());
        for(auto v:g[u]){
            ms.insert(g[v].size());
        }
    }
    cout<<ans<<endl;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
#endif
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
} 

Submission (verdict: Accepted):
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb emplace_back
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
#define trace(x) cerr << #x << " = " << x << '\n'

using namespace std;

template<class T> bool chmax(T& A, T&& B) { return B > A? A = B, true : false; }
template<class T> bool chmin(T& A, T&& B) { return B < A? A = B, true : false; }

const int64_t DESPACITO = 2e18;
const int INF = 2e9, MOD = 1e9+7;
const int MxN = 2e5 + 5;

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define MOD1 998244353
#define INF1 1e18
#define nline "\n"
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
ll oo=998244353;
ll mul(ll x,ll y)
{
    return ((x%oo)*(y%oo))%oo;
}
ll add(ll x,ll y)
{
    return ((x%oo)+(y%oo))%oo;
}
ll sub(ll x,ll y)
{
    return ((x%oo)-(y%oo)+oo)%oo;
}
ll fast_pow(ll x,ll y)
{
    if(y==0)return 1;
    if(y==1)return x;
    ll ret=1;
    ret=fast_pow(x,y/2);
    ret=mul(ret,ret);
    if(y%2==1)ret=mul(ret,x);
    return ret;
}
ll fact[2000200];
void factorial()
{
    fact[0]=1;
    for(int i=1; i<=1000000; i++)
    {
        fact[i]=mul(fact[i-1],i);
    }
}
ll calculate_inverse(ll x)
{
    return fast_pow(x,oo-2);
}
ll n_choose_k(ll n,ll k)
{
    if(k<0)return 0;
    ll ret=1;
    ret=fact[n];
    ret=mul(ret,calculate_inverse(fact[k]));
    ret=mul(ret,calculate_inverse(fact[n-k]));
    return ret;
}
ll gcd(ll x,ll y)
{
    if(x<y)swap(x,y);
    if(y==0)return x;
    return gcd(y,x%y);
}
struct DSU {
    vector<int> parent, size;
    int components;

    DSU(int n) {
        parent.resize(n);
        size.resize(n, 1);
        components = n;
        iota(parent.begin(), parent.end(), 0);
    }

    int find(int x) {
        if (parent[x] != x) {
            parent[x] = find(parent[x]);
        }
        return parent[x];
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (size[x] < size[y]) swap(x, y);
        parent[y] = x;
        size[x] += size[y];
        components--;
    }
};

void solve() {
    int n;
    cin>>n;
    vector<set<int>>g(n);
    vector<int>deg(n,0);
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        --u; 
        --v;
        deg[u]++;
        deg[v]++;
        g[u].insert(v);
        g[v].insert(u);
    }
    multiset<int>ms;
    for(int i=0;i<n;i++){
        ms.insert(deg[i]);
    }
    int ans=0;
    for(int u=0;u<n;u++){
        ms.erase(ms.find(g[u].size()));
        for(auto v:g[u]){
            ms.erase(ms.find(g[v].size()));
            ans=max(ans,(int)g[u].size()+(int)g[v].size()-2);
        }
        if(!ms.empty()){
            ans=max(ans,(int)g[u].size()+*ms.rbegin()-1);
        }
        ms.insert(g[u].size());
        for(auto v:g[u]){
            ms.insert(g[v].size());
        }
    }
    cout<<ans<<endl;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
#endif
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}