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

Solution 1 (runtime: 578 ms):
#include<bits/stdc++.h>
#define int long long
#define pb push_back
using namespace std;

int const N = 100005;

signed main()
{
    int t = 1;
    cin>>t;
    while(t--)
    {
        int n ; cin >> n;
        vector<int>v[n+2];
        int b[n+2] = {0};
        int ans = 0;
        
        for(int i = 1 ; i < n ; i++)
        {
            int x, y;
            cin >> x >> y;
            v[x].pb(y);
            v[y].pb(x);
            b[x] += 1;
            b[y] += 1;
        }
        
        // vector<pair<int, int>>a;
        for(int i = 1 ; i <= n ; i++)
        {
            int sz = v[i].size();
            // a.pb({sz, i});
        }
        
        int mx = 0;
        // int node = 0;
        for(int i =1 ; i <= n ; i++) {
            if(b[i] >= mx)  {
                mx = b[i];
                // node = i;
            }
        }
        vector<int>a;
        for(int i =1 ; i <= n ; i++) {
            if(b[i] == mx) a.pb(i);
        }
        
        // for(int i = 0 ; i < a.size(); i++) cout << a[i] << " "; cout << "\n";
        
        int cnt = 50;
        for(int j = 0 ; j < a.size(); j ++) {
        
            int node = a[j];
            // cout << node << " -------\n";
            for(int i = 0 ; i< v[node].size(); i++) {
                b[v[node][i]] -= 1; 
            }
            b[node] = -1;
            
            // cout << "---: ";
            // for(int i = 1 ; i <= n ; i++) cout << b[i] << " "; cout << "\n";
            
            // cout << node << " " << mx << "\n";
        
            // ans += mx;
            int local_mx = 0;
            for(int i = 1 ; i <= n ; i++) {
                if(b[i] > local_mx)  {
                    local_mx = b[i];
                    // node = i;
                }
            }
            
            for(int i = 0 ; i< v[node].size(); i++) {
                b[v[node][i]] += 1; 
            }
            b[node] = mx;
            
            // cout << "--- ";
            // for(int i = 1 ; i <= n ; i++) cout << b[i] << " "; cout << "\n";
            
            ans = max(ans, mx + local_mx);
            // cout << mx << " " << local_mx << endl;
            cnt -= 1;
            if(cnt < 0) break;
        }
        
        // cout << node << " " << mx << "\n";
        
        // ans += mx;
        
        
        ans -= 1;
        cout << ans << "\n";
        
    }
}





Solution 2 (runtime: 749 ms):
#include <bits/stdc++.h>
using namespace std;
void solve()
{
    int n;
    cin >> n;
    vector<int> deg(n + 1, 0);
    vector<int> Adj[n + 1];
    set<pair<int, int>> st;
    vector<pair<int, int>> v;
    for (int i = 0; i < n - 1; i++)
    {
        int x, y;
        cin >> x >> y;
        deg[x]++;
        deg[y]++;
        st.insert({min(x, y), max(x, y)});
        Adj[x].push_back(y);
        Adj[y].push_back(x);
    }
    for (int i = 1; i <= n; i++)
    {
        v.push_back({deg[i], i});
    }
    sort(v.rbegin(), v.rend());
    int i = 0;
    int mx = 0;
    while (i < min(3, n) && v[i].first == v[0].first)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (st.find({min(v[i].second, v[j].second), max(v[i].second, v[j].second)}) != st.end())
            {
                mx = max(mx, v[i].first + v[j].first - 2);
            }
            else
                mx = max(mx, v[i].first + v[j].first - 1);
        }
        i++;
    }
    cout << mx << endl;
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}