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

Solution 1 (runtime: 608 ms):
#include <bits/stdc++.h>

using namespace std;

template <typename T>
T& smax(T& a, T& b) {
  a = max(a, b);
  return a;
}

template <typename T>
T& smin(T& a, T& b) {
  a = max(a, b);
  return a;
}

void solve() {
  int n;
  cin >> n;
  vector<vector<int>> adj(n);
  vector<int>         deg(n, 0);
  for (int i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
    // update the degrees vector
    deg[a]++;
    deg[b]++;
  }
  int ans = 0;

  std::function<int(int, int)> dfs = [&](int u, int par) {
    vector<int> nodes;
    int         max_child = 0;
    for (auto v : adj[u]) {
      if (v == par) continue;
      if (max_child != 0) {
        ans = max(ans, deg[v] + max_child - 1);
      }
      smax(max_child, deg[v]);
      int val = dfs(v, u);
      ans     = max(ans, val + deg[u] - 1);
      nodes.push_back(max(val, deg[v]));
    }
    sort(nodes.begin(), nodes.end(), greater<int>());
    if (max_child != 0) {
      ans = max(ans, max_child + deg[u] - 2);
    }
    if (nodes.size() > 1) {
      ans = max(ans, nodes[0] + nodes[1] - 1);
    }
    int rv;

    if (nodes.empty()) {
      rv = max_child;
    } else {
      rv = max(max_child, nodes[0]);
    }

    // std::cout << "Node: " << u << " "
    //           << "return_value: " << rv << "\n";
    return rv;
  };

  dfs(0, -1);
  std::cout << ans << "\n";
}

int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  // freopen("txt.in", "r", stdin);
  // freopen("txt.out", "w", stdout);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}

Solution 2 (runtime: 796 ms):
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

#define endl "\n"
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(a) cout<<#a<<' '<<a<<endl;
#define cut cout<<endl <<"-------------------"<<endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef double db;

const int N = 2e5+10;
const ll mod=1e9 + 7, inf = 0x3f3f3f3f;
mt19937 mrand(time(0));     
int rnd(int x) { return mrand() % x;}
ll qmi(ll a,ll b,ll p) {ll res=1;a%=p; assert(b>=0); for(;b;b/=2){if(b&1)res=res*a%p;a=a*a%p;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

void init()
{
    int n;
    cin >> n;
    vector<vector<int>> g(n + 1);
    vector<int> d(n + 1, 0);
    for(int i = 1; i < n; i ++)
    {
        int l, r;
        cin >> l >> r;
        g[l].pb(r), g[r].pb(l);
    }
    multiset<int> s;
    for(int i = 1; i <= n; i ++)
    {
        s.insert({SZ(g[i])});
    }
    int ans = 0;
    for(int i = 1; i <= n; i ++)
    {
        s.erase(s.lower_bound(SZ(g[i])));
        for(auto x : g[i])
        {
            s.erase(s.lower_bound(SZ(g[x])));
            s.insert(SZ(g[x]) - 1);
        }
        ans = max(ans, n - 2 - (n - 1 - SZ(g[i]) - *(--s.end())));
        s.insert(SZ(g[i]));
        for(auto x : g[i])
        {
            s.erase(s.lower_bound(SZ(g[x]) - 1));
            s.insert(SZ(g[x]));
        }
    }
    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin.tie(0);
    
    int _;
    cin>>_;
    // _ = 1; 
    while(_--)
    {
        init();
    }

    return 0;
}