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

Previous submission (verdict: Time limit exceeded on test 3):
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <list>
using namespace std;

int n;
int cnt;
const int N = 20005;

typedef struct node
{
	int code;
	int du;
}node;

struct compare
{
	bool operator()(const node& a, const node& b)
	{
		return a.du < b.du;
	}
};
vector<node> d(N);

typedef struct graph
{
	int V;//顶点数
	vector<list<int>> adj;
	graph(int V) :V(n+1), adj(n+1) {};
}graph;

void dfsvisit(int s, vector<bool>& visit, vector<list<int>> adj)
{
	visit[s] = true;
	for (int v : adj[s])
	{
		if (!visit[v])
		{
			dfsvisit(v, visit, adj);
		}
	}
}

void dfs(vector<list<int>> adj, node n1, node n2)
{
	vector<bool> visit(N, false);
	list<int> tmp1 = adj[n1.code];
	list<int> tmp2 = adj[n2.code];
	while (!adj[n1.code].empty()) adj[n1.code].pop_back();
	while (!adj[n2.code].empty()) adj[n2.code].pop_back();
	visit[n1.code] = true, visit[n2.code] = true;

	for (int i = 1; i <= n; i++)
	{
		if (!visit[i])
		{
			//printf("visit[%d]\n", i);
			cnt++;
			dfsvisit(i, visit, adj);
		}
	}
	adj[n1.code] = tmp1;
	adj[n2.code] = tmp2;
}

bool check(vector<list<int>> adj, node n1, node n2)
{
	for (int i : adj[n1.code])
	{
		if (i == n2.code)
		{
			return true;
		}
	}
	for (int i : adj[n2.code])
	{
		if (i == n1.code)
			return true;
	}
	return false;
}

void solve()
{
	cin >> n;
	graph G(n);

	for (int i = 1; i <= n; i++)
	{
		d[i].du = 0;
		d[i].code = i;
	}

	int u, v;
	for (int i = 1; i < n; i++)
	{
		cin >> u >> v;
		G.adj[u].push_back(v);
		G.adj[v].push_back(u);
		d[u].du++, d[v].du++;
	}
	if (n <= 2)
	{
		cout << 0 << endl;
		return;
	}
	priority_queue<node, vector<node>, compare> pq, subpq;
	for (int i = 1; i <= n; i++)
	{
		pq.push(d[i]);
		subpq.push(d[i]);
	}
	node n1 = pq.top(); pq.pop();
	int m = 0;
	while (!pq.empty())
	{
		node n2 = pq.top(); pq.pop();
		//printf("log:n1(%d)=%d,n2(%d)=%d\n", n1.code, n1.du, n2.code, n2.du);
		dfs(G.adj, n1, n2);
		//cout << "cnt=" << cnt << endl;
		m = max(m, cnt);
		cnt = 0;
	}
	subpq.pop();
	n1 = subpq.top(); subpq.pop();
	while (!subpq.empty())
	{
		node n2 = subpq.top(); subpq.pop();
		dfs(G.adj, n1, n2);
		m = max(m, cnt);
		cnt = 0;
	}
	cout << m << endl;
}

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



Submission (verdict: Accepted):
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <list>
using namespace std;

int n;
int cnt;
const int N = 20005;

void solve()
{
	cin >> n;
	vector<int> deg(n, 0);
	vector<vector<int>> adj(n);
	int u, v;
	for (int i = 1; i < n; i++)
	{
		cin >> u >> v;
		u--, v--;
		deg[u]++, deg[v]++;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	int ans = 1;
	int mans = 0;
	vector<int> sdeg = deg;
	sort(sdeg.begin(), sdeg.end());
	
	for (int i = 0; i < n; i++)
	{
		ans = deg[i];
		vector<int> ideg;
		for (int j : adj[i])
		{
			ideg.push_back(deg[j]);
		}
		ideg.push_back(deg[i]);
		sort(ideg.rbegin(), ideg.rend()); //降序排序
		vector<int> rem;
		for (int j : ideg)
		{
			if (j == sdeg.back())
			{
				sdeg.pop_back();
				rem.push_back(j);
			}
		}
		reverse(rem.begin(), rem.end());
		
		int mx = -1;
		if (!sdeg.empty())
		{
			mx = sdeg.back();
		}
		for (int j : adj[i])
		{
			mx = max(mx, deg[j] - 1);
		}
		mans = max(mans, ans + mx - 1);
		for (int j : rem)
		{
			sdeg.push_back(j);
		}
	}
	cout << mans << endl;
}

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