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

Solution 1 (runtime: 687 ms):
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<deque> 
#include<vector>
#include<cmath> 
#include<set> 
using namespace std;
const int N = 2e5 + 10;
#define int long long 
int head[N], nex[N << 1], to[N << 1], tot;
int d[N];
inline void init(int n)
{
	tot = 0;
	for(int i = 1; i <= n; ++i) head[i] = d[i] = 0;
}
inline void add(int x, int y)
{
	nex[++tot] = head[x];
	to[tot] = y;
	head[x] = tot;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int T;
	cin >> T;
	while(T--)
	{
		int n;
		cin >> n;
		init(n);
		int u, v;
		multiset<int, greater<int>> s;
		//cout << "s.size() = " << s.size() << endl;
		for(int i = 1; i < n; ++i) 
		{
			cin >> u >> v;
			add(u, v);
			add(v, u); 
			++d[u];
			++d[v];
		}
		for(int i = 1; i <= n; ++i) s.insert(d[i]); 
		
		int ans = 0;
		for(int i = 1; i <= n; ++i)
		{
			auto it = s.find(d[i]);
			s.erase(it);
			for(int j = head[i]; j; j = nex[j])
			{
				int v = to[j];
				auto it = s.find(d[v]);
				s.erase(it);
				s.insert(d[v] - 1);
			}
			ans = max(ans, d[i] + (*s.begin()) - 1);
			for(int j = head[i]; j; j = nex[j])
			{
				int v = to[j];
				auto it = s.find(d[v] - 1);
				s.erase(it);
				s.insert(d[v]);
			}
			s.insert(d[i]);
		}
		cout << ans << endl;
	}
	return 0;
}

Solution 2 (runtime: 249 ms):

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

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

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

    ll maxdeg = 0, maxnode = 0;
    for (ll i = 0; i < n; i++){
        if (g[i].size() > maxdeg){
            maxdeg = g[i].size();
            maxnode = i;
        }
    }

    ll maxdeg2 = 0, maxnode2 = 0;
    for (ll i = 0; i < n; i++){
        if (i == maxnode){
            continue;
        }
        bool delconnection = false;
        for (auto val: g[i]){
            if (val == maxnode){
                delconnection = true;
            }
        }

        if (g[i].size() - delconnection > maxdeg2){
            maxdeg2 = g[i].size() - delconnection;
            maxnode2 = i;
        }
    }

    ll ans1 = maxdeg + maxdeg2 - 1;

    maxdeg = 0, maxnode = 0;
    for (ll i = 0; i < n; i++){
        if (g[i].size() >= maxdeg){
            maxdeg = g[i].size();
            maxnode = i;
        }
    }

    maxdeg2 = 0, maxnode2 = 0;
    for (ll i = 0; i < n; i++){
        if (i == maxnode){
            continue;
        }
        bool delconnection = false;
        for (auto val: g[i]){
            if (val == maxnode){
                delconnection = true;
            }
        }

        if (g[i].size() - delconnection >= maxdeg2){
            maxdeg2 = g[i].size() - delconnection;
            maxnode2 = i;
        }
    }

    ll ans2 = maxdeg + maxdeg2 - 1;
    
    
    cout << max(ans1, ans2) << endl;


}

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

    int T;
    cin >> T;
    while(T--){
        solve();
    }
}