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

Solution 1 (runtime: 733 ms):
#include <iostream>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
void solve() {
    int n; cin >> n;
    vector<set<int>> g(n);
    for (int i = 1; i < n; i++) {
        int u, v; cin >> u >> v;
        g[--u].insert(--v), g[v].insert(u);
    }
    multiset<int, greater<int>> s;
    for (auto x : g) s.insert(x.size());
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j : g[i]) ans = max(ans, (int)g[i].size() + (int)g[j].size() - 2);
    }
    for (int i = 0; i < n; i++) {
        s.erase(s.find(g[i].size()));
        for (int j : g[i]) s.erase(s.find(g[j].size()));
        if (s.size()) {
            ans = max(ans, (int)g[i].size() + *s.begin() - 1);
        }
        for (int j : g[i]) s.insert(g[j].size());
        s.insert(g[i].size());
    }
    cout << ans << '\n';
}
int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t = 1; cin >> t;
    while (t--) solve();

}




Solution 2 (runtime: 952 ms):
#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<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
#define ll long long
#define vll vector <long long>
#define vc vector <char>
#define vi vector <int>
#define all(v) v.begin(),v.end()
#define repi(i,a,b) for(ll i=a;i<b;i++)
#define repd(i,a,b) for(ll i=a;i>=b;i--)
#define F first
#define S second
#define pb push_back
#define Yes cout<<"YES"<<endl;
#define No cout<<"NO"<<endl;
#define hey cout<<"hey"<<endl;
#define vpi vector<pair<int,int>>
#define vpll vector<pair<long long,long long>>
#define lb(a,x) lower_bound(a.begin(),a.end(),x)
#define ub(a,x) upper_bound(a.begin(),a.end(),x)


vll input(ll n){
    vll v(n);
    repi(i,0,n)     cin>>v[i];
    return v;
}


void dfs(map<ll,set<ll>> &g,ll &node,vll &vis){
    vis[node]=1;
    for(auto it : g[node]){
        if(vis[it]==0){
            dfs(g,it,vis);
        }
    }
}


int main()
{
  // sieve();
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);


int t;
cin>>t;
while(t--)
    {
        ll n;
        cin>>n;
        
        map<ll,set<ll>> g;

        repi(i,0,n-1){
            ll u,v;
            cin>>u>>v;
            g[u].insert(v);
            g[v].insert(u);
        }

        ll ans=0;
        for(auto it:g){
            for(auto ele:it.second){
                ans=max(ans,it.second.size()+g[ele].size()-2LL);
            }
        }

        set<pair<ll,ll>> st;
        for(auto it:g){
            st.insert({it.second.size(),it.first});
        }

        // auto it=++st.rbegin();
        auto it1=st.rbegin();
        while(it1!=st.rend()){
            auto it=it1;
            it++;
            while(it!=st.rend()){
                if(g[it1->second].find(it->second)==g[it1->second].end()){
                    // hey
                    // cout<<"it.s="<<it->second<<" it.f="<<it->first<<endl;
                    ans=max(ans,it1->first-1+it->first);
                    break;
                }
                it++;
            }
            it1++;
        }
        cout<<ans<<endl;
    }
    return 0;
}