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

Solution 1 (runtime: 921 ms):
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        ll n;
        cin >> n;
        vector<vector<ll>> adj(n);
        set<pair<ll, ll>> s;

        for (int i = 0; i < n - 1; i++) { 
            ll a, b;
            cin >> a >> b;
            a--; 
            b--;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }

        if (n == 2) {
            cout << 0 << endl;
            continue;
        }

        for (int i = 0; i < n; i++) {
            s.insert({adj[i].size(), i});
        }

        int maxi = INT_MIN;

        for (int i = 0; i < n; i++) {
            
             s.erase({adj[i].size(),i});
            for (auto x : adj[i]) {
                maxi = max(maxi, (int)(adj[i].size() + adj[x].size() - 1));
                s.erase({adj[x].size(), x});
            }

            if (!s.empty()) {
                auto maxElement = *s.rbegin(); 
                maxi = max(maxi, (int)(adj[i].size() + maxElement.first));
            }
            for (auto x : adj[i]) {
               
                s.insert({adj[x].size(), x});
            }
            s.insert({adj[i].size(),i});
            
           
        }

       cout << maxi - 1 << endl;
    }

    return 0;
}


Solution 2 (runtime: 1842 ms):
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> oset;
#define int long long
#define MOD 1000000007
#define M 998244353
#define vi vector<int> 
#define vvi vector<vector<int>> 
#define pi pair<int,int> 
#define pb push_back
#define all(x) x.begin(),x.end()
#define nline "\n"
#define frn(i,n) for(int i = 0; i < n; i++)
#define yss cout<<"YES"<<endl
#define noo cout<<"NO"<<endl
const int INF = 1e18;
long long binpow(long long a, long long b, long long m)
 {a %= m;long long res = 1;while (b > 0) {if (b & 1) res = res * a % m;a = a * a % m;b >>= 1;}return res;}
/*----------------------------------------------------------------------------------------*/
// For calculating nCr's just calculate factorials till n and then use
// appropriate modular inverses to get nCr's modulo M.
void dfs(vector<vector<int>> &adj, int src,map<int, int> &edges, int par = -1){
  for(auto nbr : adj[src]){
    edges[src]++;
    // edges.erase({src, })
    if(nbr == par) continue;
    dfs(adj, nbr, edges, src);
  }
}
void solve(int tc){
    int n; cin >> n;
    vector<vector<int>> adj(n);
    for(int i = 0; i < n - 1; i++){
      int u, v; cin >> u >> v;
      u--, v--;
      adj[u].pb(v);
      adj[v].pb(u);
    }
    map<int, int> edges;
    set<pair<int, int>> edgeset;
    dfs(adj, 0, edges);
    for(auto x : edges){
      edgeset.insert({-1 * x.second, x.first});
    }
    int mx = 0, vrtx = 0;
    int res = 0;
    for(int i = 0; i < n; i++){
      int curr = adj[i].size();
      int ans = 1;
      ans += (curr - 1);
      for(auto nbr : adj[i]){
        edgeset.erase({-1 * (edges[nbr]), nbr});
        edgeset.insert({-1 * (edges[nbr] - 1), nbr});
      }
      edgeset.erase({-1 * edges[i], i});
      auto it = edgeset.begin();
      curr = -1 * it->first;
      ans += (curr - 1);
      res = max(ans, res);
      edgeset.insert({-1 * edges[i], i});
      for(auto nbr : adj[i]){
        edgeset.erase({-1 * (edges[nbr] - 1), nbr});
        edgeset.insert({-1 * (edges[nbr]), nbr});
      }
    }
    cout << res << endl;
    // cout << fixed << setprecision(10) << ans << endl;
    // Always use this format for printing any double value else you 
    // get wrong answers differing greatly with the actual double 
    // variable while you print the answer.
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); 
 int tt=1;
 cin>>tt;
 for(int i = 1; i <= tt; i++){
    //cout << "Case #" << i << ": ";
    solve(i);       
 }
}