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

Previous submission (verdict: Wrong answer on test 3):
#include<bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<pair<ll,ll>, null_type,less<pair<ll,ll>>, rb_tree_tag,tree_order_statistics_node_update>



#define ll long long

#define scn(v) for(auto &i:v)cin>>i
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define vl vector<ll>
#define vi vector<int>
#define vvi vector<vi>
#define vvl vector<vl>
#define vb vector<bool>
#define vvb vector<vb>
#define en cout<<'\n'
#define vb vector<bool>
#define vpl vector<pair<ll,ll>>
#define vi vector<int>
#define X first
#define Y second

const int MOD = 998244353;
ll mul(ll a, ll b) {
    return (((a % MOD + MOD) * (b % MOD + MOD)) % MOD);
}
ll fastPow(ll x, ll n, ll m) {
    if (n == 0) {
        return 1;
    }
    ll po = fastPow(x, n / 2, m);
    po =mul(po,po) % m;
    if (n & 1) {
        po =mul(po,x) %m;
    }
    return po % m;
}

ll modInv(ll x, ll m) {
    return fastPow(x, m - 2, m) % m;
}

ll setBit(ll num, ll idx, ll val = 1) {
    return (val) ? (num | (1 << idx)) : (num & ~(1 << idx));
}
int getBit(ll num, int idx) { return ((num >> idx) & 1) == 1; }

void burnx(){
     ll n;
     cin>>n;
     vvl adj(n+1);
     vl deg(n+1);
     vpl dgi(n);
     vector<set<ll>> st(n+1);
     for(int i=1;i<n;i++){
         ll u,v;
         cin>>u>>v;
         adj[u].push_back(v);
         adj[v].push_back(u);
         st[u].insert(v);
         st[v].insert(u);
         deg[u]++;
         deg[v]++;
     }

     for(int i=0;i<n;i++){
         dgi[i]={deg[i],i};
     }
     sort(rall(dgi));

     ll ans=0;
     ll cnt=1;
//
//     for(int z=0;z<2;z++){
//         ll mx=-1;
//         ll mxi=-1;
//         for(int i=1;i<=n;i++){
//             if(deg[i]>mx){
//                 mxi=i;
//                 mx=deg[i];
//             }
//         }
//         cnt+=deg[mxi]-1;
//         deg[mxi]=-1;
//         for(auto v:adj[mxi]){
//             deg[v]--;
//         }
//     }


        for (int x = 0; x < n; ++x) {
            ll lst=0;
            for (int y = x + 1; y < n; ++y) {
                ll temp = dgi[x].X + dgi[y].X - (st[dgi[x].Y].find(dgi[y].Y) != st[dgi[x].Y].end() ? 2 : 1);
    
                ans = max(ans, temp);
            }
        }

    cout<<ans;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cin.tie(NULL);
//  freopen ("input.txt","r",stdin);
//   freopen ("output.txt","w",stdout);
    //preFact();
    int t = 1;
    cin >> t;

    for(int txt=1;txt<=t;txt++){
        burnx();
        en;
    }

    return 0;
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<pair<ll,ll>, null_type,less<pair<ll,ll>>, rb_tree_tag,tree_order_statistics_node_update>



#define ll long long

#define scn(v) for(auto &i:v)cin>>i
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define vl vector<ll>
#define vi vector<int>
#define vvi vector<vi>
#define vvl vector<vl>
#define vb vector<bool>
#define vvb vector<vb>
#define en cout<<'\n'
#define vb vector<bool>
#define vpl vector<pair<ll,ll>>
#define vi vector<int>
#define X first
#define Y second

const int MOD = 998244353;
ll mul(ll a, ll b) {
    return (((a % MOD + MOD) * (b % MOD + MOD)) % MOD);
}
ll fastPow(ll x, ll n, ll m) {
    if (n == 0) {
        return 1;
    }
    ll po = fastPow(x, n / 2, m);
    po =mul(po,po) % m;
    if (n & 1) {
        po =mul(po,x) %m;
    }
    return po % m;
}

ll modInv(ll x, ll m) {
    return fastPow(x, m - 2, m) % m;
}

ll setBit(ll num, ll idx, ll val = 1) {
    return (val) ? (num | (1 << idx)) : (num & ~(1 << idx));
}
int getBit(ll num, int idx) { return ((num >> idx) & 1) == 1; }

void burnx(){
    int n;
    cin >> n;
    // Graph in 1-indexed form; for a tree we have n-1 edges.
    vector<vector<int>> graph(n + 1);
    vector<int> deg(n + 1, 0);
    for (int i = 1; i <= n - 1; i++){
        int u, v;
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
        deg[u]++;
        deg[v]++;
    }

    // Build neighbor sets for quick lookup.
    // (In C++20 you could use std::unordered_set::contains or similar.)
    vector<unordered_set<int>> neighbors(n + 1);
    for (int i = 1; i <= n; i++){
        for (int nb : graph[i])
            neighbors[i].insert(nb);
    }

    // Build a vector of vertices (1-indexed) sorted by degree descending.
    vector<int> sortedVerts(n);
    iota(sortedVerts.begin(), sortedVerts.end(), 1);
    sort(sortedVerts.begin(), sortedVerts.end(), [&](int a, int b){
        return deg[a] > deg[b];
    });

    int bestNonAdjacent = 0;
    // For each vertex x, choose the best partner y (by global degree order)
    // such that y is not x and not a neighbor of x.
    for (int x = 1; x <= n; x++){
        for (int cand : sortedVerts) {
            if (cand == x) continue;               // skip itself
            if (neighbors[x].find(cand) != neighbors[x].end()) continue; // skip neighbors
            // cand is the highest-degree vertex that is not adjacent to x.
            bestNonAdjacent = max(bestNonAdjacent, deg[x] + deg[cand] - 1);
            break;  // no need to look further for x
        }
    }

    int bestAdjacent = 0;
    // Iterate over every edge (each only once) to check adjacent pairs.
    for (int x = 1; x <= n; x++){
        for (int nb : graph[x]){
            if (x < nb){ // each edge counted once
                bestAdjacent = max(bestAdjacent, deg[x] + deg[nb] - 2);
            }
        }
    }

    int ans = max(bestNonAdjacent, bestAdjacent);
    cout << ans;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cin.tie(NULL);
//  freopen ("input.txt","r",stdin);
//   freopen ("output.txt","w",stdout);
    //preFact();
    int t = 1;
    cin >> t;

    for(int txt=1;txt<=t;txt++){
        burnx();
        en;
    }

    return 0;
}