#P5327. [ZJOI2019] 语言

[ZJOI2019] 语言

Description

Jiutiao Keliang is a girl who likes patterns. By this pattern, the second problem should be related to data structures.

In a distant kingdom, there are nn cities. There are n1n - 1 bidirectional roads between the cities, and these roads guarantee that any two cities can reach each other directly or indirectly.

In ancient times, these nn cities were at war with each other. In a highly isolated environment, each city developed its own language. After the kingdom was unified, language barriers greatly hindered the kingdom’s development. To improve this, the king ordered the design of mm common languages and carried out mm language unification operations. In the ii-th operation, a minister started from city sis_i, walked to tit_i along the shortest path, and taught every city on the way (including sis_i and tit_i) to use the ii-th common language.

Once there is a shared language, cities can conduct trade. Two cities ui,viu_i, v_i can conduct trade if and only if there exists a common language LL such that every city on the shortest path from uiu_i to viv_i (including uiu_i and viv_i) can use LL.

To measure the effect of the language unification operations, the king wants you to compute how many pairs of cities (u,v)(u, v) with u<vu < v can conduct trade.

Input Format

The first line contains two positive integers n,mn, m, representing the number of cities and the number of common languages.

The next n1n - 1 lines each contain two integers xi,yix_i, y_i (1xi,yin1 \le x_i, y_i \le n), indicating a road connecting cities xix_i and yiy_i.

The next mm lines each contain two integers si,tis_i, t_i (1si,tin1 \le s_i, t_i \le n), indicating one language promotion operation.

Output Format

Output one line with one integer, representing the number of pairs of cities that can conduct trade.

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

Hint

Sample Explanation

The pairs of cities that can conduct trade are $(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5)$.

Constraints

Test Point nn mm Other Constraints
1,21,2 300\le 300 None
3,43,4 5×103\le 5\times 10^3
5,65,6 105\le 10^5 yi=xi+1y_i=x_i+1
7107\sim 10 None

For 100%100\% of the testdata, 1xi,yi,si,tin1051 \le x_i, y_i, s_i, t_i \le n\leq 10 ^ 5, 1m1051\leq m\leq 10 ^ 5, and xiyix_i\neq y_i.

Translated by ChatGPT 5