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

Solution 1 (runtime: 561 ms):
#include <bits/stdc++.h>
using namespace std;
void dfs(int node , vector<vector<int>>& adj , vector<int>& vis , int del1 , int del2){
    vis[node]=1;
    for(auto it : adj[node]){
        if(!vis[it] && it!=del1 && it!=del2){
            dfs(it,adj,vis,del1,del2);
        }
    }
}
void solve() {
    int n;
    cin>>n;
    // vector<vector<int>> adj(n+1);
    set<pair<int,int>> adj;
    vector<int> deg(n+1,0);
    for(int i=0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        adj.insert({a,b});
        adj.insert({b,a});
        deg[b]++;
        deg[a]++;
    }
    int ans = 0;
    vector<pair<int,int>> arr;
    for(int i=1;i<=n;i++){
        arr.push_back({deg[i],i});
    }
    sort(arr.begin(),arr.end(),greater<pair<int,int>>());
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(adj.find({arr[i].second , arr[j].second})==adj.end()){
                ans = max(ans , arr[j].first + arr[i].first - 1);
                break;
            }
        }
    }
    for(auto it : adj){
        ans = max(ans , deg[it.first] +deg[it.second] - 2);
    }
    cout<<ans<<endl;
    
}

int main()
{
    ios_base::sync_with_stdio(false);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cin.tie(NULL);
 
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
 
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}

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

using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define range(i, a, b, s) for (int i = a; (s > 0 && i < (b)) || (s < 0 && i > (b)); i += s)
#define trav(a, x) for (auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;

template <typename T>
void print_arr(vector<T> a) {
	trav(x, a) cout << to_string(x) << " ";
	cout << "\n";
}

int T;
int n;
vector<set<int>> g;

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

	cin >> T;
	while (T--) {
		cin >> n;
		g = vector<set<int>>(n);
		rep(i, 0, n-1) {
			int u, v;
			cin >> u >> v;
			u--; v--;
			g[u].insert(v);
			g[v].insert(u);
		}

		if (n == 2) {
			cout << 0 << '\n';
			continue;
		}

		vpi pairs(n);
		rep(i, 0, n) {
			pairs[i] = pii(sz(g[i]), i);
		}

		sort(all(pairs));
		// 1. 3 of the highest value (find two not connected)
		// 2. 2 of the highest value (take both)
		// 3. 1 of the highest value (check if there's a value 
		//    among the next highest that doesn't overlap)

		if (pairs[n-1].first == pairs[n-2].first && pairs[n-1].first == pairs[n-3].first) {
			cout << pairs[n-1].first * 2 - 1 << '\n';
			continue;
		}

		bool found = false;
		int cnt = pairs[n-2].first;
		rep(i, 0, n) {
			int v = pairs[n-1].second;
			if (i == v) continue;
			if (sz(g[i]) == cnt && g[v].count(i) == 0) {
				found = true;
				break;
			}
		}

		//cout << "last " << pairs[n-1].first << " " << cnt << " " << found << '\n';
		cout << pairs[n-1].first + cnt - (found ? 0 : 1) - 1 << '\n';
	}

	return 0;
}