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

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


#define ll long long int
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define F first
#define S second
#define sz(x) ((int)(x).size())
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

const int MOD = 1e9 + 7;
const ll INF = 1e18;


template<typename T>
void debug(vector<T> v) { for(auto i: v) cout << i << " "; cout << endl; }

template<typename T1, typename T2>
void debug(vector<pair<T1, T2>> v) { for(auto i: v) cout << "(" << i.F << "," << i.S << ") "; cout << endl; }


int main() {
    fast_io;

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<vector<int>>neighbours(n,vector<int>());
        vector<int>deg(n,0);
        for(int i=0;i<n-1;i++){
            int a,b;
            cin>>a>>b;
            neighbours[a-1].push_back(b-1);
            neighbours[b-1].push_back(a-1);
            deg[a-1]++;
            deg[b-1]++;
        }
        if(n <= 2){
            cout<<0<<endl;
            continue;
        }
        int conn = 1;
        set<pair<int,int>>s;
        for(int i=0;i<n;i++){
            s.insert({deg[i],i});
        }
        for(int i=0;i<n;i++){
            int tconn = 1;
            pair<int,int>p = {deg[i] , i};
            s.erase(p);
            for(auto e : neighbours[i]){
                s.erase({deg[e],e});
                s.insert({deg[e]-1,e});
            }
            int deg_max = s.rbegin()->first;
       
            conn = max(conn , -1 + deg[i] + deg_max);
            s.insert(p);
            for(auto e : neighbours[i]){
                s.erase({deg[e]-1,e});
                s.insert({deg[e],e});
            }

        }

        cout<<conn<<endl;
        
    }

    return 0;
}

Solution 2 (runtime: 531 ms):
// Problem: C. Remove Exactly Two
// Contest: Codeforces - Codeforces Round 1000 (Div. 2)
// URL: https://codeforces.com/contest/2063/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(), x.end()
#define CLR(i, a) memset(i, a, sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int)a.size())
#define LOCAL

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef array<ll, 3> arr;
typedef double DB;
typedef pair<DB, DB> pdd;
constexpr ll MAXN = static_cast<ll>(2e5) + 10, INF = static_cast<ll>(5e18) + 3;

ll N, M, T;
vector<ll> g[MAXN];
ll edges[MAXN];

inline void solve() {
  cin >> N;
  map<ll, ll> cnt;
  // 初始化
  FOR(i, 1, N) { g[i].clear(); }
  FOR(i, 1, N - 1) {
    ll u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  if (N == 2) {
    cout << 0 << '\n';
    return;
  }
  ll mx = 0, mxN = 1;
  FOR(i, 1, N) {
    edges[i] = SZ(g[i]);
    cnt[edges[i]] = cnt.find(edges[i]) == cnt.end() ? 1 : cnt[edges[i]] + 1;
  }
  ll ans = 0;
  map<ll, ll> backup = cnt;
  FOR(i, 1, N) {
    ll lans = edges[i];
    // #ifdef LOCAL
    // cerr << i << ' ' << edges[i] << '\n';
    // #endif
    cnt[edges[i]] -= 1;
    if (cnt[edges[i]] == 0) {
      cnt.erase(edges[i]);
    }
    FOR(j, 0, SZ(g[i]) - 1) {
      ll node = g[i][j];
      cnt[edges[node]] -= 1;
      cnt[edges[node] - 1] = cnt.find(edges[node] - 1) == cnt.end() ? 1 : cnt[edges[node] - 1] + 1;
      if (cnt[edges[node]] == 0) {
        cnt.erase(edges[node]);
      }
    }
    lans -= 1;  // 先不管我们这个要做第二个操作的联通块
    if (cnt.rbegin()->fi > 1) {
      // #ifdef LOCAL
      // cerr << lans << '\n';
      // #endif
      lans += cnt.rbegin()->fi;
    } else if (cnt.rbegin()->fi == 1) {
      // #ifdef LOCAL
      // cerr<<"In if:" << i << ' ' << lans << '\n';
      // #endif
      lans += cnt.rbegin()->fi - 1;
    }

    // #ifdef LOCAL
    // cerr << i << ' ' << lans << '\n';
    // #endif
    ans = max(ans, lans);
    cnt = backup;
  }
  cout << ans << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> T;
  while (T--) {
    solve();
#ifdef LOCAL
    cerr << '\n';
#endif
  }
  return 0;
}
/*

*/