Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 796 ms):
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int solve(set<pair<int,int>> edges, map<int,int> m, int v, int mmax)
{
    int sol=mmax;
    for (auto it=edges.begin(); it!=edges.end(); it++)
    {
        if (it->second == v)
            m[it->first]--;
        if (it->first == v)
            m[it->second]--;
    }
    int r=-1;
    m[v]=-1;
    for (auto it=m.begin(); it!=m.end(); it++)
    {
        if (it->second>r)
            r=it->second;
    }
    return sol+r-1;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        set<pair<int,int>> edges;
        map<int,int> m;
        int a,b;
        for (int i=1; i<n; i++)
        {
            cin >> a >> b;
            m[a]++; m[b]++;
            if (a>b)
                swap(a,b);
            edges.insert({a,b});
        }
        int v=-1, mmax=-1;
        int vv=-1, vmax=-1;
        for (auto it=m.begin(); it!=m.end(); it++)
        {
            if (it->second>mmax)
            {
                mmax=it->second;
                v=it->first;
            }
            else if (it->second>vmax)
            {
                vmax=it->second;
                vv=it->first;
            }
        }
        cout << max(solve(edges, m, v, mmax), solve(edges, m, vv, vmax)) << "\n";
        edges.clear();
        m.clear();
    }
    return 0;
}


Solution 2 (runtime: 499 ms):
/**
*    author:  lazyhash(yashkundu)
*    created: 15 Feb, 2025 | 18:13:17
**/
#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <set>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

const int MAXN = (int)2e5+10;

int n;
int u, v;

vector<int> g[MAXN];
 
multiset<int> ms;
int mxcmp;

void dfs(int v, int p = -1) {
    ms.extract(g[v].size());
    for(int u: g[v]) {
        mxcmp = max(mxcmp, (int)g[v].size() + (int)g[u].size() - 2);
        ms.extract(g[u].size());
    }

    if(!ms.empty()) mxcmp = max(mxcmp, (int)g[v].size() + *ms.rbegin() - 1);
    for(int u: g[v]) {
        ms.insert(g[u].size());
    }
    ms.insert(g[v].size());
    for(int u: g[v]) {
        if(u==p) continue;
        dfs(u, v);
    }

}


void solve() {
    cin >> n;
    for(int i=0;i<n;i++) g[i].clear();

    for(int i=0;i<n-1;i++) {
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }


    ms.clear();
    for(int i=0;i<n;i++) ms.insert((int)g[i].size());

    mxcmp = 0;
    dfs(0);

    cout << mxcmp << "\n";



}
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
* - BenQ
*/