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

Previous submission (verdict: Wrong answer on test 1):
#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 = 0; i < n; i++){
		cout<<sz[i].F<<" "<<sz[i].S<<"\n";
	}
	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;
}


Submission (verdict: Accepted):
#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;
}