atcoder#AGC033F. [AGC033F] Adding Edges

[AGC033F] Adding Edges

Score : 22002200 points

Problem Statement

You are given a tree TT with NN vertices and an undirected graph GG with NN vertices and MM edges. The vertices of each graph are numbered 11 to NN. The ii-th of the N1N-1 edges in TT connects Vertex aia_i and Vertex bib_i, and the jj-th of the MM edges in GG connects Vertex cjc_j and Vertex djd_j.

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

  • Choose three integers aa, bb and cc such that GG has an edge connecting Vertex aa and bb and an edge connecting Vertex bb and cc but not an edge connecting Vertex aa and cc. If there is a simple path in TT that contains all three of Vertex a,ba, b and cc in some order, add an edge in GG connecting Vertex aa and cc.

Print the number of edges in GG 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

  • 2N20002 \leq N \leq 2000
  • 1M20001 \leq M \leq 2000
  • 1ai,biN1 \leq a_i, b_i \leq N
  • aibia_i \neq b_i
  • 1cj,djN1 \leq c_j, d_j \leq N
  • cjdjc_j \neq d_j
  • GG does not contain multiple edges.
  • TT is a tree.

Input

Input is given from Standard Input in the following format:

NN MM

a1a_1 b1b_1

::

aN1a_{N-1} bN1b_{N-1}

c1c_1 d1d_1

::

cMc_M dMd_M

Output

Print the final number of edges in GG.

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

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

  • Let (a,b,c)=(1,5,4)(a,b,c)=(1,5,4) and add an edge connecting Vertex 11 and 44.
  • Let (a,b,c)=(1,5,2)(a,b,c)=(1,5,2) and add an edge connecting Vertex 11 and 22.
  • Let (a,b,c)=(2,1,4)(a,b,c)=(2,1,4) and add an edge connecting Vertex 22 and 44.
7 5
1 5
1 4
1 7
1 2
2 6
6 3
2 5
1 3
1 6
4 6
4 7
11
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
27