Problem:
Score : 2200 points

Problem Statement

You are given a tree T with N vertices and an undirected graph G with N vertices and M edges.
The vertices of each graph are numbered 1 to N.
The i-th of the N-1 edges in T connects Vertex a_i and Vertex b_i, and the j-th of the M edges in G connects Vertex c_j and Vertex d_j.

Consider adding edges to G by repeatedly performing the following operation:

Choose three integers a, b and c such that G has an edge connecting Vertex a and b and an edge connecting Vertex b and c but not an edge connecting Vertex a and c. If there is a simple path in T that contains all three of Vertex a, b and c in some order, add an edge in G connecting Vertex a and c.

Print the number of edges in G when no more edge can be added.
It can be shown that this number does not depend on the choices made in the operation.

Constraints

2 \leq N \leq 2000

1 \leq M \leq 2000

1 \leq a_i, b_i \leq N

a_i \neq b_i

1 \leq c_j, d_j \leq N

c_j \neq d_j

G does not contain multiple edges.

T is a tree.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 d_1
:
c_M d_M

Output

Print the final number of edges in G.

Sample Input 1

5 3
1 2
1 3
3 4
1 5
5 4
2 5
1 5

Sample Output 1

6

We can have at most six edges in G by adding edges as follows:

Let (a,b,c)=(1,5,4) and add an edge connecting Vertex 1 and 4.

Let (a,b,c)=(1,5,2) and add an edge connecting Vertex 1 and 2.

Let (a,b,c)=(2,1,4) and add an edge connecting Vertex 2 and 4.

Sample Input 2

7 5
1 5
1 4
1 7
1 2
2 6
6 3
2 5
1 3
1 6
4 6
4 7

Sample Output 2

11

Sample Input 3

13 11
6 13
1 2
5 1
8 4
9 7
12 2
10 11
1 9
13 7
13 11
8 10
3 8
4 13
8 12
4 7
2 3
5 11
1 4
2 11
8 10
3 5
6 9
4 10

Sample Output 3

27

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#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];

unordered_map<int, 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;
					}
				}
			}
		}
	}*/
	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;
}