F - Adding Edges Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 22002200

問題文

NN 頂点からなる木 TTNN 頂点 MM 辺からなる無向グラフ GG が与えられます。 それぞれの各頂点には 11 から NN の番号が割り振られています。 TTN1N-1 本の辺のうち、ii 本目の辺は頂点 aia_i と頂点 bib_i を繋いでいます。 GGMM 本の辺のうち、jj 本目の辺は頂点 cjc_j と頂点 djd_j を繋いでいます。

GG に対して以下の操作を繰り返し行うことで、GG に辺を追加することを考えます。

  • 33 つの整数 aa,bb,cc であって、GG の頂点 abab 間と bcbc 間に辺があり、acac 間に辺がないようなものを選ぶ。 TT のある単純パス上に頂点 aa,bb,cc が何らかの順序ですべて含まれるとき、GG の頂点 acac 間に辺を追加する。

これ以上辺を追加することができなくなったとき、GG の辺の数はいくつになるか求めてください。 この数はどのように操作を行っても変わらないことが示せます。

制約

  • 2N20002 ≦ N ≦ 2000
  • 1M20001 ≦ M ≦ 2000
  • 1ai,biN1 ≦ a_i, b_i ≦ N
  • aibia_i \neq b_i
  • 1cj,djN1 ≦ c_j, d_j ≦ N
  • cjdjc_j \neq d_j
  • GG は多重辺を含まない
  • TT は木である

入力

入力は以下の形式で標準入力から与えられる。

NN MM
a1a_1 b1b_1
::
aN1a_{N-1} bN1b_{N-1}
c1c_1 d1d_1
::
cMc_M dMd_M

出力

最終的な GG の辺の数を出力せよ。


入力例 1Copy

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

出力例 1Copy

Copy
6

以下の順で辺を追加することで 66 本まで辺を増やすことができます。

  • (a,b,c)=(1,5,4)(a,b,c)=(1,5,4) とし、頂点 11 と頂点 44 の間に辺を追加する。
  • (a,b,c)=(1,5,2)(a,b,c)=(1,5,2) とし、頂点 11 と頂点 22 の間に辺を追加する。
  • (a,b,c)=(2,1,4)(a,b,c)=(2,1,4) とし、頂点 22 と頂点 44 の間に辺を追加する。

入力例 2Copy

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

出力例 2Copy

Copy
11

入力例 3Copy

Copy
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

出力例 3Copy

Copy
27

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.


Sample Input 1Copy

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

Sample Output 1Copy

Copy
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.

Sample Input 2Copy

Copy
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 2Copy

Copy
11

Sample Input 3Copy

Copy
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 3Copy

Copy
27


2025-04-29 (Tue)
02:15:36 +00:00