Language: C++

#include"bits/stdc++.h"

using namespace std;

#define MAX 2002

int n;
int m;

vector<int> v[MAX];

vector<int> g[MAX];

vector<pair<int, int> > ed;

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

set<pair<int, int> > s;

bitset<MAX> con[MAX];

bitset<MAX> child[MAX];
bitset<MAX> par[MAX];

bitset<MAX> stk;

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

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, 1);
	par[b] = stk;
	dep[b] = d;
	for (int go : v[b]) {
		if (go == pr)continue;
		dfs3(go, b, d + 1);
	}
	stk.set(b, 0);
}
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);
	for (int i = 0; i < n; i++) {
		dfs(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--;
		g[c].push_back(d);
		g[d].push_back(c);
		ed.push_back(make_pair(c, d));
		s.insert(make_pair(c, d));
		s.insert(make_pair(d, c));
		con[c].set(d, 1);
		con[d].set(c, 1);
		q.push(make_tuple(0, c, d));
		edd++;
	}
	/*for (int i = 0; i < ed.size(); i++) {
		for (int j = i + 1; j < ed.size(); j++) {
			vector<int> ez;
			ez.push_back(ed[i].first);
			ez.push_back(ed[i].second);
			ez.push_back(ed[j].first);
			ez.push_back(ed[j].second);
			sort(ez.begin(), ez.end());
			ez.erase(unique(ez.begin(), ez.end()), ez.end());
			for (int ii = 0; ii < ez.size();ii++) {
				int k = ez[ii];
				if (ed[i].first == k || ed[i].second == k) {
					if (ed[j].first == k || ed[j].second == k) {
						swap(ez[ii], ez[0]);
						if (s.count(make_pair(ez[1], ez[2])))break;
						q.push({ ez[0],ez[1],ez[2] });
						break;
					}
				}
			}
		}
	}*/
	int tim = 0;
	while (!q.empty()) {
		int centre, lef, rig;
		tie(centre, lef, rig) = q.front();
		q.pop();
		//if (s.count(make_pair(lef, rig)))continue;
		//s.insert(make_pair(lef, rig));
		//s.insert(make_pair(rig, lef));
		con[lef].set(rig, 1);
		con[rig].set(lef, 1);
		vector<int> p;
		p.push_back(lef);
		p.push_back(rig);
		int A = p[0];
		int B = p[1];
		auto ff = (par[lca[A][B]] ^ (child[B][pat[A][B]] | child[A][pat[B][A]])).set(lca[A][B], 1);
		for (int i2 = 0; i2 < 2; i2++) {
			A = p[i2];
			B = p[i2 ^ 1];
			//the node unconnected to node B
			//connected to A
			//the node is available at the pair(A,B)
			auto f = con[A] & (~con[B]);
			f &= ff;
			for (int i = f._Find_first(); i < n; i = f._Find_next(i)) {
				if (i == A || i == B)continue;
				if (true) {
					//if (s.count(make_pair(A, i)) && s.count(make_pair(A, B))) {
					q.push(make_tuple(A, i, B));
					con[i].set(B, 1);
					con[B].set(i, 1);
					edd++;
					s.insert(make_pair(i, B));
					s.insert(make_pair(B, i));
					if (edd > n*(n - 1) / 2) {
						return 1;
					}
					//}
				}
			}
		}
	}
	cout << s.size() / 2 << endl;
	return 0;
}

Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:28:27: error: no matching function for call to 'std::bitset<2002>::count(int&)'
   28 |         if (child[b].count(pr))return;
      |             ~~~~~~~~~~~~~~^~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bitset:1366:7: note: candidate: 'std::size_t std::bitset<_Nb>::count() const [with long unsigned int _Nb = 2002; std::size_t = long unsigned int]'
 1366 |       count() const _GLIBCXX_NOEXCEPT
      |       ^~~~~
/usr/include/c++/14/bitset:1366:7: note:   candidate expects 0 arguments, 1 provided
a.cc:29:22: error: 'class std::bitset<2002>::reference' has no member named 'reset'
   29 |         child[b][pr].reset();
      |                      ^~~~~
a.cc:30:30: error: cannot bind non-const lvalue reference of type 'std::bitset<2002>::reference&' to an rvalue of type 'std::bitset<2002>::reference'
   30 |         auto &c = child[b][pr];
      |                   ~~~~~~~~~~~^
a.cc:31:11: error: 'class std::bitset<2002>::reference' has no member named 'set'
   31 |         c.set(b, 1);
      |           ^~~
a.cc:35:19: error: no match for 'operator|=' (operand types are 'std::bitset<2002>::reference' and 'std::bitset<2002>::reference')
   35 |                 c |= child[go][b];
      |                 ~~^~~~~~~~~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:41:
/usr/include/c++/14/cstddef:168:3: note: candidate: 'constexpr std::byte& std::operator|=(byte&, byte)'
  168 |   operator|=(byte& __l, byte __r) noexcept
      |   ^~~~~~~~
/usr/include/c++/14/cstddef:168:20: note:   no known conversion for argument 1 from 'std::bitset<2002>::reference' to 'std::byte&'
  168 |   operator|=(byte& __l, byte __r) noexcept
      |              ~~~~~~^~~
In file included from /usr/include/c++/14/streambuf:43,
                 from /usr/include/c++/14/bits/streambuf_iterator.h:35,
                 from /usr/include/c++/14/iterator:66,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:54:
/usr/include/c++/14/bits/ios_base.h:104:3: note: candidate: 'constexpr const std::_Ios_Fmtflags& std::operator|=(_Ios_Fmtflags&, _Ios_Fmtflags)'
  104 |   operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b) _GLIBCXX_NOTHROW
      |   ^~~~~~~~
/usr/include/c++/14/bits/ios_base.h:104:29: note:   no known conversion for argument 1 from 'std::bitset<2002>::reference' to 'std::_Ios_Fmtflags&'
  104 |   operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b) _GLIBCXX_NOTHROW
      |              ~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/ios_base.h:154:3: note: candidate: 'constexpr const std::_Ios_Openmode& std::operator|=(_Ios_Openmode&, _Ios_Openmode)'
  154 |   operator|=(_Ios_Openmode& __a, _Ios_Openmode __b) _GLIBCXX_NOTHROW
      |   ^~~~~~~~
/usr/include/c++/14/bits/ios_base.h:154:29: note:   no known conversion for argument 1 from 'std::bitset<2002>::reference' to 'std::_Ios_Openmode&'
  154 |   operator|=(_Ios_Openmode& __a, _Ios_Openmode __b) _GLIBCXX_NOTHROW
      |              ~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/ios_base.h:201:3: note: candidate: 'constexpr const std::_Ios_Iostate& std::operator|=(_Ios_Iostate&, _Ios_Iostate)'
  201 |   operator|=(_Ios_Iostate& __a, _Ios_Iostate __b) _GLIBCXX_NOTHROW
      |   ^~~~~~~~
/usr/include/c++/14/bits/ios_base.h:201:28: note:   no known conversion for argument 1 from 'std::bitset<2002>::reference' to 'std::_Ios_Iostate&'
  201 |   operator|=(_Ios_Iostate& __a, _Ios_Iostate __b) _GLIBCXX_NOTHROW
      |              ~~~~~~~~~~~~~~^~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:176:
/usr/include/c++/14/future:185:18: note: candidate: 'constexpr std::launch& std::operator|=(launch&, launch)'
  185 |   inline launch& operator|=(launch& __x, launch __y) noexcept
      |                  ^~~~~~~~
/usr/include/c++/14/future:185:37: note:   no known conversion for argument 1 from 'std::bitset<2002>::reference' to 'std::launch&'
  185 |   inline launch& operator|=(launch& __x, launch __y) noexcept
      |                             ~~~~~~~~^~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:198:
/usr/include/c++/14/charconv:646:3: note: candidate: 'constexpr std::chars_format& std::operator|=(chars_format&, chars_format)'
  646 |   operator|=(chars_format& __lhs, chars_format __rhs) noexcept
      |   ^~~~~~~~
/usr/include/c++/14/charconv:646:28: note:   no known conversion for argument 1 from 'std::bitset<2002>::reference' to 'std::chars_format&'
  646 |   operator|=(chars_format& __lhs, chars_format __rhs) noexcept
      |              ~~~~~~~~~~~~~~^~~~~
a.cc:37:11: error: no match for 'operator|=' (operand types are 'std::bitset<2002>::reference' and 'std::bitset<2002>')
   37 |         c |= par[b];
      |         ~~^~~~~~~~~
/usr/include/c++/14/cstddef:168:3: note: candidate: 'constexpr std::byte& std::operator|=(byte&, byte)'
  168 |   operator|=(byte& __l, byte __r) noexcept
      |   ^~~~~~~~
/usr/include/c++/14/cstddef:168:20: note:   no known conversion for argument 1 from 'std::bitset<2002>::reference' to 'std::byte&'
  168 |   operator|=(byte& __l, byte __r) noexcept
      |              ~~~~~~^~~
/usr/include/c++/14/bits/ios_base.h:104:3: note: candidate: 'constexpr const std::_Ios_Fmtflags& std::operator|=(_Ios_Fmtflags&, _Ios_Fmtflags)'
  104 |   operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b) _GLIBCXX_NOTHROW
      |   ^~~~~~~~
/usr/include/c++/14/bits/ios_base.h:104:29: note:   no known conversion for argument 1 from 'std::bitset<2002>::reference' to 'std::_Ios_Fmtflags&'
  104 |   operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b) _GLIBCXX_NOTHROW
      |              ~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/ios_base.h:154:3: note: candidate: 'constexpr const std::_Ios_Openmode& std::operator|=(_Ios_Openmode&, _Ios_Openmode)'
  154 |   operator|=(_Ios_Openmode& __a, _Ios_Openmode __b) _GLIBCXX_NOTHROW
      |   ^~~~~~~~
/usr/include/c++/14/bits/ios_base.h:154:29: note:   no known conversion for argument 1 from 'std::bitset<2002>::reference' to 'std::_Ios_Openmode&'
  154 |   operator|=(_Ios_Openmode& __a, _Ios_Openmode __b) _GLIBCXX_NOTHROW
      |              ~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/ios_base.h:201:3: note: candidate: 'constexpr const std::_Ios_Iostate& std::operator|=(_Ios_Iostate&, _Ios_Iostate)'
  201 |   operator|=(_Ios_Iostate& __a, _Ios_Iostate __b) _GLIBCXX_NOTHROW
      |   ^~~~~~~~
/usr/include/c++/14/bits/ios_base.h:201:28: note:   no known conversion for argument 1 from 'std::bitset<2002>::reference' to 'std::_Ios_Iostate&'
  201 |   operator|=(_Ios_Iostate& __a, _Ios_Iostate __b) _GLIBCXX_NOTHROW
      |              ~~~~~~~~~~~~~~^~~
/usr/include/c++/14/future:185:18: note: candidate: 'constexpr std::launch& std::operator|=(launch&, launch)'
  185 |   inline launch& operator|=(launch& __x, launch __y) noexcept
      |                  ^~~~~~~~
/usr/include/c++/14/future:185:37: note:   no known conversion for argument 1 from 'std::bitset<2002>::reference' to 'std::launch&'
  185 |   inline launch& operator|=(launch& __x, launch __y) noexcept
      |                             ~~~~~~~~^~~
/usr/include/c++/14/charconv:646:3: note: candidate: 'constexpr std::chars_format& std::operator|=(chars_format&, chars_format)'
  646 |   operator|=(chars_format& __lhs, chars_format __rhs) noexcept
      |   ^~~~~~~~
/usr/include/c++/14/charconv:646:28: note:   no known conversion for argument 1 from 'std::bitset<2002>::reference' to 'std::chars_format&'
  646 |   operator|=(chars_format& __lhs, chars_format __rhs) noexcept
      |              ~~~~~~~~~~~~~~^~~~~
a.cc: In function 'int main()':
a.cc:139:43: error: no match for 'operator^' (operand types are 'std::bitset<2002>' and 'int')
  139 |                 auto ff = (par[lca[A][B]] ^ (child[B][pat[A][B]] | child[A][pat[B][A]])).set(lca[A][B], 1);
      |                            ~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                         |                        |
      |                                         std::bitset<2002>        int
/usr/include/c++/14/bitset:1577:5: note: candidate: 'template<long unsigned int _Nb> std::bitset<_Nb> std::operator^(const bitset<_Nb>&, const bitset<_Nb>&)'
 1577 |     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
      |     ^~~~~~~~
/usr/include/c++/14/bitset:1577:5: note:   template argument deduction/substitution failed:
a.cc:139:87: note:   mismatched types 'const std::bitset<_Nb>' and 'int'
  139 |                 auto ff = (par[lca[A][B]] ^ (child[B][pat[A][B]] | child[A][pat[B][A]])).set(lca[A][B], 1);
      |                                                                                       ^
In file included from /usr/include/c++/14/valarray:605,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:166:
/usr/include/c++/14/bits/valarray_after.h:410:5: note: candidate: 'template<class _Dom1, class _Dom2> std::_Expr<std::__detail::_BinClos<std::__bitwise_xor, std::_Expr, std::_Expr, _Dom1, _Dom2>, typename std::__fun<std::__bitwise_xor, typename _Dom1::value_type>::result_type> std::operator^(const _Expr<_Dom1, typename _Dom1::value_type>&, const _Expr<_Dom2, typename _Dom2::value_type>&)'
  410 |     _DEFINE_EXPR_BINARY_OPERATOR(^, struct std::__bitwise_xor)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:410:5: note:   template argument deduction/substitution failed:
a.cc:139:87: note:   'std::bitset<2002>' is not derived from 'const std::_Expr<_Dom1, typename _Dom1::value_type>'
  139 |                 auto ff = (par[lca[A][B]] ^ (child[B][pat[A][B]] | child[A][pat[B][A]])).set(lca[A][B], 1);
      |                                                                                       ^
/usr/include/c++/14/bits/valarray_after.h:410:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__bitwise_xor, std::_Expr, std::_Constant, _Dom, typename _Dom::value_type>, typename std::__fun<std::__bitwise_xor, typename _Dom1::value_type>::result_type> std::operator^(const _Expr<_Dom1, typename _D