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

Solution 1 (runtime: 780 ms):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T>
void read(T& x)
{
	x = 0;
	int f = 0, v = getchar();
	while(v != '-' && (v < '0' || v > '9')) v = getchar();
	if(v == '-') f = 1, v = getchar();
	while(v >= '0' && v <= '9') x = (x << 3) + (x << 1) + (v & 15), v = getchar();
	if(f) x = -x;
}
template <typename T>
void write(T x)
{
	if(x < 0) putchar('-'), x = -x;
	if(x < 10) putchar(x | 48);
	else write(x / 10), putchar(x % 10 | 48);
}
const int maxn = 2e5;
int t, n, u, v;
ll sz[maxn + 5];
ll c[maxn + 5];
vector<int> g[maxn + 5];
int lowbit(int x)
{
	return x & -x;
}
void add(int id, ll v)
{
	for(int i = id; i <= n; i += lowbit(i)) c[i] = max(c[i], v);
}
ll findans(int l, int r)
{
	ll res = -1e18;
	while(l <= r)
	{
		int nxt = r - lowbit(r) + 1;
		if(nxt >= l) res = max(res, c[r]), r -= lowbit(r);
		else res = max(res, sz[r]), -- r;
	}
	return res;
}
ll findans2(int l, int r)
{
	ll res = -1e18, yr = r;
	while(l <= r)
	{
		int nxt = r - lowbit(r) + 1;
		if(nxt >= l && r != yr) res = max(res, c[r]), r -= lowbit(r);
		else res = max(res, sz[r]), -- r;
	}
	return res;
}
void upd(int id, ll v)
{
	if(v >= 0) sz[id] += v, add(id, sz[id]);
	else 
	{
		ll tmp = sz[id];
		sz[id] += v;
		for(int i = id; i <= n; i += lowbit(i))
		{
			if(c[i] == tmp) c[i] = findans2(i - lowbit(i) + 1, i);
		}
	}
}
int main(void)
{
	cin >> t;
	while(t--)
	{
		cin >> n;
		ll maxv = 0, maxi, ans = 0;
		for(int i = 1; i <= n; ++ i) 
		{
			g[i].clear();
			sz[i] = 0;
			c[i] = 0;
		}
		for(int i = 1; i < n; ++ i)
		{
			cin >> u >> v;
			++ sz[u], ++ sz[v];
			g[u].push_back(v);
			g[v].push_back(u);
		}
		for(int i = 1; i <= n; ++ i)
		{
			add(i, sz[i]);
			maxv = max(maxv, sz[i]);
		}
		for(int i = 1; i <= n; ++ i)
		{
			if(sz[i] == maxv)
			{
				for(int j : g[i]) upd(j, -1);
				upd(i, -1e9);
				ans = max(ans, sz[i] + (ll)1e9 + findans(1, n) - 1);
				upd(i, 1e9);
				for(int j : g[i]) upd(j, 1);
			}
		}
		cout << ans << endl;
	}
	return 0;
}
/*
1
8
1 2
1 3
1 4
2 5
2 6
3 7
3 8
*/

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

using namespace std;

bool isInSet(pair<int,int> pr, set<pair<int,int>> &s) {
    return s.find(pr) != s.end();
}

void solve(){
    int n;
    cin>>n;
    set<pair<int, int>> edge_set;
    vector<pair<int,int>> deg(n);
    for(int i=0;i<n;i++) {
        deg[i].second = i;
        deg[i].first = 0;
    }
    for (int i=1;i<n;i++) {
        int u,v;
        cin>>u>>v;
        --u,--v;
        edge_set.insert(make_pair(u,v));
        edge_set.insert(make_pair(v,u));
        deg[u].first++;
        deg[v].first++;
    }
    if (n==2) {
        cout<<"0\n";
        return;
    }
    sort(deg.begin(), deg.end());
    int ans = 0;
    for(int i=n-2;i>=0;i--) {
        int j=n-1;
        ans = max(ans, deg[i].first + deg[j].first - (isInSet(make_pair(deg[i].second, deg[j].second), edge_set) ? 1: 0));
    }
    ans = max(ans, deg[n-2].first + deg[n-3].first - (isInSet(make_pair(deg[n-2].second, deg[n-3].second), edge_set) ? 1: 0));
    cout<<(ans-1)<<'\n';
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}