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

Solution 1 (runtime: 843 ms):
#include<stdio.h>
#include<set>
struct edge
{
	int to,next;
}e[400010];
int head[200010];
int sum;
void add(int from,int to)
{
	e[++sum].to=to;
	e[sum].next=head[from];
	head[from]=sum;
}
int size[200010];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n;
		sum=0;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			size[i]=0;
			head[i]=0;
		}
		for(int i=1;i<n;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			add(u,v);
			add(v,u);
			size[u]++;
			size[v]++;
		}
		std::multiset<int> mul;
		for(int i=1;i<=n;i++)
			mul.insert(size[i]);
		int maxn=0;
		for(int i=1;i<=n;i++)
		{
//			for(auto it:mul)
//				printf("%d ",it);
//			printf("\n");
			int sze=size[i];
			mul.erase(mul.find(size[i]));
			for(int j=head[i];j;j=e[j].next)
			{
				mul.erase(mul.find(size[e[j].to]));
				mul.insert(size[e[j].to]-1);
			}
			std::multiset<int>::iterator it;
			it=mul.end();
			it--;
			maxn=std::max(maxn,sze+*it-1);
			for(int j=head[i];j;j=e[j].next)
			{
				mul.erase(mul.find(size[e[j].to]-1));
				mul.insert(size[e[j].to]);
			}
			mul.insert(size[i]);
		}
		printf("%d\n",maxn);
	}
	return 0;
}


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

#define pii pair <int, int>
#define s second
#define f first

const int N = 2e5 + 10;

int n, d[N], vert[N];

vector <pii> edge;

void input() {
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        edge.push_back(pii(--u, --v));
        d[u]++;
        d[v]++;
    }
}

void solve() {
    if (n == 2) {
        edge.clear();
        for (int i = 0; i < n; i++) d[i] = 0;
        cout << 0 << '\n';
        return;
    }
    iota(vert, vert + n, 0);
    sort(vert, vert + n, [] (int i, int j) {
        return d[i] < d[j];
    });
    int mx = 0, t = 0, x = 0;
    for (int i = n - 1; i >= 0; i--) {
        if (d[vert[i]] == d[vert[n - 1]]) mx++;
        else if (t == d[vert[i]]) {
            x++;
        }
        else if (t < d[vert[i]]) {
            x = 1;
            t = d[vert[i]];
        }
    }
    if (mx >= 3) {
        cout << d[vert[n - 1]] * 2 - 1 << '\n';
    }
    else if (mx == 2) {
        int flag = 1;
        for (int i = 0; i < n - 1; i++) {
            int u = edge[i].f, v = edge[i].s;
            if (d[u] == d[vert[n - 1]] && d[v] == d[vert[n - 1]]) {
                flag = 0;
                cout << d[vert[n - 1]] * 2 - 2 << '\n';
            }
        }
        if (flag) cout << d[vert[n - 1]] * 2 - 1 << '\n';
    }
    else {
        for (int i = 0; i < n - 1; i++) {
            int u = edge[i].f, v = edge[i].s;
            if (v == vert[n - 1]) swap(u, v);
            if (u == vert[n - 1]) {
                if (t == d[v]) {
                    x--;
                }
            }
        }
        if (x) cout << d[vert[n - 1]] + t - 1 << '\n';
        else cout << d[vert[n - 1]] + t - 2 << '\n';
    }
    edge.clear();
    for (int i = 0; i < n; i++) d[i] = 0;
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int tt;
    cin >> tt;
    while (tt--) {
        input();
        solve();
    }
    return 0;
}