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

Solution 1 (runtime: 843 ms):
//#include <bits/stdc++.h>
//__builtin_popcountll(long long number); 二进制数数字1的个数
//__builtin_popcount(int number);二进制数数字1的个数
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
#include <queue>
#include <cstring>
using namespace std;
const int inf = 0x3f;
#define int long long
#define lowbit(x) ((x)&(-x))
#define endl '\n'
typedef pair<int, int> PII;
const int N = 200010;

vector<int> arr[N];

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	int num = 1;
	int T, n;
	int a, b;
	cin >> T;
	while (T--) {

		map<int, int> dev;
		map<int, int> maxson;
		cin >> n;
		for (int i = 1; i <= n; i++)
			arr[i].clear();
		for (int i = 1; i <= n - 1; i++) {

			cin >> a >> b;
			dev[a]++;
			dev[b]++;
			arr[a].push_back(b);
			arr[b].push_back(a);
		}
		int num = 0;
		int maxl = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j < arr[i].size(); j++) {
				maxson[i] = max(dev[arr[i][j]], maxson[i]);
			}
		}
		for (int i = 1; i <= n; i++) {
			if (dev[i] > maxl) {
				maxl = dev[i];
				num = i;
			} else if (dev[i] == maxl) {
				if (maxson[i] < maxson[num])
					num = i;
			}
		}
		int maxnums = 0;
		for (int i = 1; i <= n; i++) {
			if (dev[i] == maxl) {
				maxnums++;
			}
		}
		if (maxnums > 2) {
			cout << maxl*2 - 1 << endl;
			continue;
		}
		int ans = 1;
		ans += maxl - 1;
		dev[num] = 0;
		for (int i = 0; i < arr[num].size(); i++) {
			dev[arr[num][i]]--;
		}
		maxl = 0;
		for (int i = 1; i <= n; i++) {
			if (dev[i] > maxl && i != num) {
				maxl = dev[i];
			}
		}
		ans += maxl - 1;
		cout << ans << endl;
	}
	return 0;
}

















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

typedef long long ll;
const ll mod=1e9+7;
const ll MX=2e5+5;
inline void norm(ll &a) {a%=mod; (a<0) && (a+=mod) ;}                            //positive mod value
inline ll modAdd(ll a,ll b) {a%=mod, b%=mod; norm(a),norm(b); return (a+b)%mod;} //modular addition
inline ll modSub(ll a,ll b) {a%=mod, b%=mod; norm(a),norm(b); return (a-b)%mod;} //modular subtraction
inline ll modMul(ll a,ll b) {a%=mod, b%=mod; norm(a),norm(b); return (a*b)%mod;} //modular multiplication
inline ll bigMod(ll b,ll p)  {ll r=1; while(p) {if(p & 1LL) r=modMul(r,b) ;b=modMul(b,b) ; p>>=1LL ; } return r; }
inline ll modInverse(ll a) {return bigMod(a,mod-2); }
inline ll modDiv(ll a ,ll b) { return modMul(a,modInverse(b)) ;}

void solve()
{
    ll n; 
    cin >> n; 
    vector<vector<ll>> adj(n+1, vector<ll> ()); 

    vector<ll> deg(n+1, 0); 
    for(int i=1;i<n; i++)
    {
        ll u, v; 
        cin >> u >> v; 
        adj[u].push_back(v); 
        adj[v].push_back(u);
        deg[u]++, deg[v]++; 
    }

    for(int i=1;i<=n; i++) sort(adj[i].begin(), adj[i].end());
    ll ans = 0; 
    for(int i=1;i<=n; i++)
    {
        for(auto child: adj[i])
        {
            ans = max(ans, deg[i] + deg[child] - 2); 
        }
    }

    vector<pair<ll,ll>> vp; 
    for(int i=1;i<=n; i++)
    {
        vp.push_back({deg[i], i}); 
    }

    sort(vp.rbegin(), vp.rend());

    for(int i=1;i<=n; i++)
    {
        for(int j=i+1;j<=n; j++)
        {
            ll u = vp[i-1].second, v = vp[j-1].second;
            if(binary_search(adj[u].begin(), adj[u].end(), v)) continue;
            ans = max(ans, vp[i-1].first + vp[j-1].first-1);
            break;
        }
    }

    cout << ans << endl; 
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll tc = 1;
    cin >> tc;
    for (ll t = 1; t <= tc; t++)
    {
        solve();
    }
    return 0;
}