Language: C++

#pragma GCC optimize "-O3"
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") 
#include"bits/stdc++.h"

using namespace std;

#define MAX 2000

int n;
int m;

vector<int> v[MAX];

queue<tuple<int, int, int> > q;

set<pair<int, int> > s;

struct BITSET {
	__attribute__((aligned(16))) int64_t val[32];
	BITSET() {
		memset(val, 0, sizeof(val));
	}
	inline void operator ^= (const BITSET &a) {
		for (int i = 0; i < 32; i += 2) {
			__asm__ volatile(
				".inte_syntax_noprefix\n"
				"movdqu xmm0,XMMWORD ptr[%0]\n"
				"movdqu xmm1,XMMWORD ptr[%1]\n"
				"por xmm0,xmm1\n"
				"movdqu XMMWORD ptr[%0],xmm0\n"
				".att_syntax_noprefix\n"
				::"r"(val[i]),"r"(a.val[i]):"memory"
				);
		}
		/*val[0] ^= a.val[0];
		val[1] ^= a.val[1];
		val[2] ^= a.val[2];
		val[3] ^= a.val[3];
		val[4] ^= a.val[4];
		val[5] ^= a.val[5];
		val[6] ^= a.val[6];
		val[7] ^= a.val[7];
		val[8] ^= a.val[8];
		val[9] ^= a.val[9];
		val[10] ^= a.val[10];
		val[11] ^= a.val[11];
		val[12] ^= a.val[12];
		val[13] ^= a.val[13];
		val[14] ^= a.val[14];
		val[15] ^= a.val[15];
		val[16] ^= a.val[16];
		val[17] ^= a.val[17];
		val[18] ^= a.val[18];
		val[19] ^= a.val[19];
		val[20] ^= a.val[20];
		val[21] ^= a.val[21];
		val[22] ^= a.val[22];
		val[23] ^= a.val[23];
		val[24] ^= a.val[24];
		val[25] ^= a.val[25];
		val[26] ^= a.val[26];
		val[27] ^= a.val[27];
		val[28] ^= a.val[28];
		val[29] ^= a.val[29];
		val[30] ^= a.val[30];
		val[31] ^= a.val[31];*/
	}
	inline void operator &= (const BITSET &a) {
		for (int i = 0; i < 32; i += 2) {
			__asm__ volatile(
				".inte_syntax_noprefix\n"
				"movdqu xmm0,XMMWORD ptr[%0]\n"
				"movdqu xmm1,XMMWORD ptr[%1]\n"
				"pand xmm0,xmm1\n"
				"movdqu XMMWORD ptr[%0],xmm0\n"
				".att_syntax_noprefix\n"
				::"r"(val[i]), "r"(a.val[i]) : "memory"
				);
		}
		/*val[0] &= a.val[0];
		val[1] &= a.val[1];
		val[2] &= a.val[2];
		val[3] &= a.val[3];
		val[4] &= a.val[4];
		val[5] &= a.val[5];
		val[6] &= a.val[6];
		val[7] &= a.val[7];
		val[8] &= a.val[8];
		val[9] &= a.val[9];
		val[10] &= a.val[10];
		val[11] &= a.val[11];
		val[12] &= a.val[12];
		val[13] &= a.val[13];
		val[14] &= a.val[14];
		val[15] &= a.val[15];
		val[16] &= a.val[16];
		val[17] &= a.val[17];
		val[18] &= a.val[18];
		val[19] &= a.val[19];
		val[20] &= a.val[20];
		val[21] &= a.val[21];
		val[22] &= a.val[22];
		val[23] &= a.val[23];
		val[24] &= a.val[24];
		val[25] &= a.val[25];
		val[26] &= a.val[26];
		val[27] &= a.val[27];
		val[28] &= a.val[28];
		val[29] &= a.val[29];
		val[30] &= a.val[30];
		val[31] &= a.val[31];*/
	}
	inline void set(int c) {
		val[c / 64] |= (1LL << (long long int)(c % 64));
	}
	inline bool test(int c) {
		return (val[c / 64] >> (long long int)(c % 64)) & 1LL;
	}
	inline void set2(int c) {
		set(c);
		val[c / 64] ^= (1LL << (long long int)(c % 64));
	}
	inline void reset() {
		memset(val, 0, sizeof(val));
	}
	inline int _Find_next(int idx) {
		idx++;
		while (idx / 64 < 32) {
			int beg = idx / 64;
			long long int ps = idx % 64;
			if ((val[beg] >> ps) == 0)idx = (beg+1) * 64;
			else {
				if ((val[beg] >> ps) & 1LL) {
					return idx;
				}
				idx++;
			}
		}
		return idx;
	}
	inline int _Find_first() {
		return _Find_next(-1);
	}
};


BITSET con[MAX];

BITSET  child[MAX];
BITSET par[MAX];

BITSET stk;

inline void dfs(int b, int pr = -1) {
	auto &c = child[b];
	c.set(b);
	for (int go : v[b]) {
		if (go == pr)continue;
		dfs(go, b);
		c ^= child[go];
	}
}

int pat[MAX][MAX];
int lca[MAX][MAX];

int dep[MAX];


inline void dfs2(int b, int pr = -1, int star = 0) {
	pat[star][b] = pr;
	lca[star][b] = b;
	if (pr != -1) {
		if (lca[star][pr] != -1) {
			if (dep[lca[star][pr]] < dep[b]) {
				lca[star][b] = lca[star][pr];
			}
		}
	}
	for (int go : v[b]) {
		if (go == pr)continue;
		dfs2(go, b, star);
	}
}
inline void dfs3(int b, int pr = -1, int d = 0) {
	stk.set(b);
	par[b] = stk;
	dep[b] = d;
	for (int go : v[b]) {
		if (go == pr)continue;
		dfs3(go, b, d + 1);
	}
	stk.set2(b);
}
BITSET ff;
int main() {
	cin >> n >> m;
	for (int i = 1; i < n; i++) {
		int a, b;
		scanf("%d%d", &a, &b);
		a--;
		b--;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	dfs3(0);
	dfs(0);
	for (int i = 0; i < n; i++) {
		dfs2(i, -1, i);
	}
	int edd = 0;
	for (int i = 0; i < m; i++) {
		int c, d;
		scanf("%d%d", &c, &d);
		c--;
		d--;
		con[c].set(d);
		con[d].set(c);
		q.push(make_tuple(0, c, d));
		edd++;
	}
	BITSET full;
	for (int i = 0; i < n; i++) {
		full.set(i);
	}
	while (!q.empty()) {
		int centre, lef, rig;
		tie(centre, lef, rig) = q.front();
		q.pop();
		int A = lef;
		int B = rig;
		ff.reset();
		if (lca[A][B] == B) {
			swap(A, B);
		}
		if (lca[A][B] == A) {
			ff ^= child[B];
			ff ^= full;
			ff ^= child[pat[B][A]];
			ff ^= par[B];
			ff^= par[A];
		}
		else {
			ff = child[B];
			ff ^= child[A];
			ff ^= par[A];
			ff^= par[B];
			ff.set(lca[A][B]);
		}
		BITSET r = con[A];
		r ^= con[B];
		ff &= r;
		//the node unconnected to node B
		//connected to A
		//the node is available at the pair(A,B)
		for (int i = (int)ff._Find_first(); i < n; i = (int)ff._Find_next(i)) {
			if (i == A || i == B)continue;
			if (con[B].test(i)) {
				swap(A, B);
			}
			q.push(make_tuple(A, i, B));
			con[i].set(B);
			con[B].set(i);
			edd++;
		}
	}
	cout << edd << endl;
	return 0;
}

Compiler output:
In file included from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:4:
/usr/include/c++/14/bits/allocator.h: In destructor 'std::_Vector_base<int, std::allocator<int> >::_Vector_impl::~_Vector_impl()':
/usr/include/c++/14/bits/allocator.h:182:7: error: inlining failed in call to 'always_inline' 'std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = int]': target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53:
/usr/include/c++/14/bits/stl_vector.h:132:14: note: called from here
  132 |       struct _Vector_impl
      |              ^~~~~~~~~~~~