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

Previous submission (verdict: Wrong answer on test 2):
#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;
	for (int i = 1; i <= n; i++)
	{
		pq.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;
	}
	
	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: 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;
}