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

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

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<int> adj[n + 1];
        vector<int> cnt(n + 1);
        for (int i = 1; i < n; i++)
        {
            int u, v;
            cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
            cnt[u]++;
            cnt[v]++;
        }
        if (n == 2)
        {
            cout << 0 << endl;
            continue;
        }
        vector<pair<int, int>> arr;
        for (int i = 1; i <= n; i++)
        {
            arr.push_back({cnt[i], i});
        }
        sort(arr.begin(), arr.end());
        // cout << arr[n - 3].second << arr[n - 2].second << arr[n - 1].second << endl;
        //  arr[n-3],arr[n-2],arr[n-1]
        //  search if they are neighbours
        int x = 0, y = 0, z = 0;
        for (int ch : adj[arr[n - 3].second])
        {
            if (ch == arr[n - 2].second)
                x = 1;
            if (ch == arr[n - 1].second)
                y = 1;
        }
        for (int ch : adj[arr[n - 2].second])
        {
            if (ch == arr[n - 1].second)
                z = 1;
        }
        // cout << x << y << z << endl;
        int maxi = 0;
        maxi = max(maxi, cnt[arr[n - 3].second] + cnt[arr[n - 2].second] - 1 - x); // arr[n-3],arr[n-2] (x)
        maxi = max(maxi, cnt[arr[n - 3].second] + cnt[arr[n - 1].second] - 1 - y);
        maxi = max(maxi, cnt[arr[n - 2].second] + cnt[arr[n - 1].second] - 1 - z);

        // final case---single maximum
        int node = arr[n - 1].second;
        vector<int> mp(n + 1);
        for (int ch : adj[node])
        {
            mp[ch] = 1;
        }
        for (int i = 1; i <= n; i++)
        {
            if (i == node)
                continue;
            maxi = max(maxi, cnt[node] + cnt[i] - 1 - mp[i]);
        }

        cout << maxi << endl;
    }
}

Solution 2 (runtime: 515 ms):
#include <iostream>
#include<iomanip>
#include<climits>
// INT_MAX LLONG_MAX
#include <vector>
#include<array>
#include<queue>
#include<stack>
#include<set>
#include<unordered_set>
#include<map>
#include<unordered_map>
#include<cstring>
#include<cmath>
#include <algorithm>
#include <functional>
using namespace std;
#define endl "\n"
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define ffor(i,a,b) for(int i=(a);i<=(b);i++)
#define rfor(i,a,b) for(int i=(a);i>=(b);i--)
#define fios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define yes cout<<"Yes"<<endl;
#define no cout<<"No"<<endl;
#define DEBUG cout<<">>>>"<<endl;
using vi=vector<int>;
template<size_t N>
using ai = array<int, N>;
using pii=pair<int,int>;
typedef long long LL;
const int inf=1e18;
const int CJR=998244353;
const int HY_Spark=1e9+7;


void solve(){
	int n;
	cin>>n;
	vector<vector<int>> G(n+1);
	
	ffor(i,1,n-1){
		int u,v;
		cin>>u>>v;
		G[u].push_back(v);
		G[v].push_back(u);	
	}
	if(n==2){
		cout<<0<<endl;
		return;
	}
	multiset<int> mt;
	ffor(i,1,n){
		mt.insert(G[i].size());
	}
	int ans=0;
	// 相邻 不相邻
	function<void(int, int)> dfs;
	dfs=[&](int u,int fa) -> void {
		if(fa!=-1){
			ans=max(ans,(int)(G[fa].size()+G[u].size()-2));
		}
		for(auto p:G[u]){
			if(p==fa){
				continue;
			}else{
				dfs(p,u);
			}
		}
		mt.erase(mt.find(G[u].size()));
		for(auto p:G[u]){
			mt.erase(mt.find(G[p].size()));
		}
		if(!mt.empty()){
			ans=max(ans,(int)(G[u].size()+*mt.rbegin()-1));
		}
		
		mt.insert(G[u].size());
		for(auto p:G[u]){
			mt.insert(G[p].size());
		}
	};
	dfs(1,-1);
	cout<<ans<<endl;
}


signed main(){
    fios;
    int _=1;
    cin>>_;
    while(_--){
        solve();
    }
}