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

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T>
void read(T& x)
{
	x = 0;
	int f = 0, v = getchar();
	while(v != '-' && (v < '0' || v > '9')) v = getchar();
	if(v == '-') f = 1, v = getchar();
	while(v >= '0' && v <= '9') x = (x << 3) + (x << 1) + (v & 15), v = getchar();
	if(f) x = -x;
}
template <typename T>
void write(T x)
{
	if(x < 0) putchar('-'), x = -x;
	if(x < 10) putchar(x | 48);
	else write(x / 10), putchar(x % 10 | 48);
}
const int maxn = 2e5;
int t, n, u, v;
int sz[maxn + 5];
vector<int> g[maxn + 5];
int main(void)
{
	cin >> t;
	while(t--)
	{
		cin >> n;
		int maxv = 0, maxi, ans = 1;
		for(int i = 1; i <= n; ++ i) 
		{
			g[i].clear();
			sz[i] = 0;
		}
		for(int i = 1; i < n; ++ i)
		{
			cin >> u >> v;
			++ sz[u], ++ sz[v];
			g[u].push_back(v);
			g[v].push_back(u);
		}
		for(int i = 1; i <= n; ++ i)
		{
			if(sz[i] > maxv)
			{
				maxv = sz[i];
				maxi = i;
			}
		}
		ans += maxv - 1;
		maxv = 0;
		for(int i : g[maxi])
		{
			-- sz[i];
		} 
		for(int i = 1; i <= n; ++ i)
		{
			if(i == maxi) continue;
			maxv = max(maxv, sz[i]);
		}
		ans += maxv - 1;
		cout << ans<< endl;
	}
	return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T>
void read(T& x)
{
	x = 0;
	int f = 0, v = getchar();
	while(v != '-' && (v < '0' || v > '9')) v = getchar();
	if(v == '-') f = 1, v = getchar();
	while(v >= '0' && v <= '9') x = (x << 3) + (x << 1) + (v & 15), v = getchar();
	if(f) x = -x;
}
template <typename T>
void write(T x)
{
	if(x < 0) putchar('-'), x = -x;
	if(x < 10) putchar(x | 48);
	else write(x / 10), putchar(x % 10 | 48);
}
const int maxn = 2e5;
int t, n, u, v;
ll sz[maxn + 5];
ll c[maxn + 5];
vector<int> g[maxn + 5];
int lowbit(int x)
{
	return x & -x;
}
void add(int id, ll v)
{
	for(int i = id; i <= n; i += lowbit(i)) c[i] = max(c[i], v);
}
ll findans(int l, int r)
{
	ll res = -1e18;
	while(l <= r)
	{
		int nxt = r - lowbit(r) + 1;
		if(nxt >= l) res = max(res, c[r]), r -= lowbit(r);
		else res = max(res, sz[r]), -- r;
	}
	return res;
}
ll findans2(int l, int r)
{
	ll res = -1e18, yr = r;
	while(l <= r)
	{
		int nxt = r - lowbit(r) + 1;
		if(nxt >= l && r != yr) res = max(res, c[r]), r -= lowbit(r);
		else res = max(res, sz[r]), -- r;
	}
	return res;
}
void upd(int id, ll v)
{
	if(v >= 0) sz[id] += v, add(id, sz[id]);
	else 
	{
		ll tmp = sz[id];
		sz[id] += v;
		for(int i = id; i <= n; i += lowbit(i))
		{
			if(c[i] == tmp) c[i] = findans2(i - lowbit(i) + 1, i);
		}
	}
}
int main(void)
{
	cin >> t;
	while(t--)
	{
		cin >> n;
		ll maxv = 0, maxi, ans = 0;
		for(int i = 1; i <= n; ++ i) 
		{
			g[i].clear();
			sz[i] = 0;
			c[i] = 0;
		}
		for(int i = 1; i < n; ++ i)
		{
			cin >> u >> v;
			++ sz[u], ++ sz[v];
			g[u].push_back(v);
			g[v].push_back(u);
		}
		for(int i = 1; i <= n; ++ i)
		{
			add(i, sz[i]);
			maxv = max(maxv, sz[i]);
		}
		for(int i = 1; i <= n; ++ i)
		{
			if(sz[i] == maxv)
			{
				for(int j : g[i]) upd(j, -1);
				upd(i, -1e9);
				ans = max(ans, sz[i] + (ll)1e9 + findans(1, n) - 1);
				upd(i, 1e9);
				for(int j : g[i]) upd(j, 1);
			}
		}
		cout << ans << endl;
	}
	return 0;
}
/*
1
8
1 2
1 3
1 4
2 5
2 6
3 7
3 8
*/