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

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

#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
// #define int long long
#define pi pair<int,int>

void solve(){
	int n;	cin >> n;
	vector< set<int>> graph(n+1);

	for( int i = 0; i < n-1; ++i){
		int a,b;	cin >> a >> b;
		graph[a].insert(b);
		graph[b].insert(a);		
	}	
	set<pi> st;
	for( int i = 1; i <= n; ++i){
		st.insert({ graph[i].size() , i});
	}
	int ans = 0;
	for( int i = 1; i <= n; ++i){
		// what if we pick i as first vertex to be removed
		st.erase( st.find({graph[i].size() ,i}));

		// if i and j are adjacent
		int j = (*st.rbegin()).second;
		int score = (int)graph[i].size() + (int)graph[j].size()-2;
		
		// if i and j are not adjacent
		for( int a : graph[i])
			st.erase( st.find( {graph[a].size() , a}));
		if( st.size()){
			j = (*st.rbegin()).second;			
			score = max( score , (int)graph[i].size() + (int)graph[j].size() - 1);
		}
		
		ans = max( ans , score);
		// insert all the  values
		st.insert( {graph[i].size(),i});
		for( int a : graph[i])
			st.insert( {graph[a].size(),a});
	}
	cout << ans;
	

}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    
    int test = 1;   
    cin >> test;
    while( test--){
        solve();
	    cout << "\n";
    }

    return 0;
}


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

#define ll long long
#define EB emplace_back
#define V(x) vector<x>
#define pii pair<int,int>
#define F first
#define S second
#define MP(x,y) make_pair(x,y)

using namespace std;

bool comp(pii &a, pii &b){
	return a.F < b.F;
}

long long solve(){
	int n;
	cin>>n;
	V(V(int)) a(n+1);
	V(pii) vert(n-1);
	vector<map<int,int>>m(n+1);
	for(int i = 0; i < n-1; i++){
		int u,v;
		cin>>u>>v;
		a[u].EB(v);
		a[v].EB(u);
		m[u][v] = 1;
		m[v][u] = 1;
		vert[i] = MP(u,v);
	}
	int ans = 0;
	V(pii) sz(n+1);
	for(int i = 1; i <= n; i++){
		sz[i].F = a[i].size();
		sz[i].S = i;
	}
	sort(sz.rbegin(), sz.rend(), comp);
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < n; j++){
			int ele = sz[j].S;
			if(ele == i) continue;
			if(m[i][ele]){
				ans = max(ans, (int)(a[i].size() + sz[j].F - 2));
				
			}
			else{
				ans = max(ans, (int)(a[i].size() + sz[j].F - 1));
				break;
			}
		}
	}
	return ans;
	//int x = a[1].size(), ix = 1, y = a[2].size(), iy = 2;
	//for(int i = 3; i <= n; i++) {
		//if(a[i].size() > x){
			//y = x;
			//iy = ix;
			//x = a[i].size();
			//ix = i;
		//}
		//else if(a[i].size() > y){
			//y = a[i].size();
			//iy = i;
		//}
	//}
	//int count = 0;
	//V(int) vis(n+1);
	//vis[ix] = 1;
	//vis[iy] = 1;
	//auto dfs = [&](int start){
		//queue<int>q;
		//q.push(start);
		//vis[start] = 1;
		//while(q.size()){
			//int frnt = q.front();
			//q.pop();
			//for(int adj : a[frnt]){
				//if(vis[adj]) continue;
				//q.push(adj);
				//vis[adj] = 1;
			//}
		//}
	//};
	//for(int i = 1; i <= n; i++){
		//if(vis[i]) continue;
		//count++;
		//dfs(i);
	//}
	//return count;
}

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

	#ifndef ONLINE_JUDGE
    freopen("in", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif
	
	int tt;
	tt = 1;
	
	cin>>tt;
	
	for(int i = 1; i <= tt; i++)
	{
		cout<<solve()<<"\n";
		// solve();
	}    
	
	return 0;
}