Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 9):
#include<bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define in_ freopen("input.txt", "r", stdin);
#define all(a) a.begin(), a.end()
#define cout2(a) for(int i = 0; i < a.size(); ++ i) cout << a[i] << " "; cout << "\n";
#define cout1(a, s, e) for(int i = s; i < e; ++ i) cout << a[i] << " "; cout << "\n";
#define debug1(x) cout << x << "!!!\n";
#define debug2(x) cout << x << "###\n";

typedef long long ll;
const int N = 200005;
typedef pair<int,int> PII;
 
#define x1 first
#define y1 second

void deal() {
	
	int n;
	cin >> n;
	
	vector<int> deg(n), a;
	vector<PII> e(n);
	
	for(int i = 0; i < n - 1; ++ i) {
		int u, v;
		cin >> u >> v;
		
		u --, v --;
	    deg[u] ++, deg[v] ++;
	    
	    e.push_back({u, v});
	}
	
	int ans = 0;
//	for(auto it : e) {
//		ans = max(ans, it.x1 + it.y1 - 2);
//	}
//	
	int vmax = *max_element(deg.begin(), deg.end());
	
	for(int i = 0; i < n; ++ i) if(deg[i] == vmax) a.push_back(i);
	 
	for(int j = 0; j < a.size(); ++j)  {
	    vmax = a[j];
	    
		vector<int> ok(n);
		ok[vmax] = 1;
		
		for(auto it : e) {
			if(it.x1 == vmax) ok[it.y1] = 1;
			if(it.y1 == vmax) ok[it.x1] = 1;
		}
		
		for(int i = 0; i < n; ++ i) {
			if(i == vmax) continue;
			if(ok[i]) ans = max(ans, deg[vmax] + deg[i] - 2);
			else ans = max(ans, deg[vmax] + deg[i] - 1);
		}
    } 

    cout << ans << "\n";
   	
} 

int main() {
	
	#ifndef ONLINE_JUDGE
	in_;
//	freopen("out.txt", "w", stdout); 
	#endif
	
	IOS;
	
	int t;
	cin >> t;
	while(t--)
	 deal();

	return 0;
}  

/*
       /\_/\
      (= ._.)
       />  \>  !!!!!!     
       author :: fly
	    
*/ 

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define in_ freopen("input.txt", "r", stdin);
#define all(a) a.begin(), a.end()
#define cout2(a) for(int i = 0; i < a.size(); ++ i) cout << a[i] << " "; cout << "\n";
#define cout1(a, s, e) for(int i = s; i < e; ++ i) cout << a[i] << " "; cout << "\n";
#define debug1(x) cout << x << "!!!\n";
#define debug2(x) cout << x << "###\n";

typedef long long ll;
const int N = 200005;
typedef pair<int,int> PII;
 
#define x1 first
#define y1 second

void deal() {
	
	int n;
	cin >> n;
	
	vector<int> deg(n), a;
	vector<PII> e(n);
	
	for(int i = 0; i < n - 1; ++ i) {
		int u, v;
		cin >> u >> v;
		
		u --, v --;
	    deg[u] ++, deg[v] ++;
	    
	    e.push_back({u, v});
	}
	
	int ans = 0;
//	for(auto it : e) {
//		ans = max(ans, it.x1 + it.y1 - 2);
//	}
//	
	int vmax = *max_element(deg.begin(), deg.end());
	
	for(int i = 0; i < n; ++ i) if(deg[i] == vmax) a.push_back(i);
	
	if(a.size() >= 3) {
		cout << 2 * vmax - 1 << "\n";
		return;
	}
	 
	for(int j = 0; j < a.size(); ++j)  {
	    vmax = a[j];
	    
		vector<int> ok(n);
		ok[vmax] = 1;
		
		for(auto it : e) {
			if(it.x1 == vmax) ok[it.y1] = 1;
			if(it.y1 == vmax) ok[it.x1] = 1;
		}
		
		for(int i = 0; i < n; ++ i) {
			if(i == vmax) continue;
			if(ok[i]) ans = max(ans, deg[vmax] + deg[i] - 2);
			else ans = max(ans, deg[vmax] + deg[i] - 1);
		}
    } 

    cout << ans << "\n";
   	
} 

int main() {
	
	#ifndef ONLINE_JUDGE
	in_;
//	freopen("out.txt", "w", stdout); 
	#endif
	
	IOS;
	
	int t;
	cin >> t;
	while(t--)
	 deal();

	return 0;
}  

/*
       /\_/\
      (= ._.)
       />  \>  !!!!!!     
       author :: fly
	    
*/