QOJ.ac

QOJ

Límite de tiempo: 3 s Límite de memoria: 512 MB Puntuación total: 100

#2580. Language

Estadísticas

Keli is a girl who likes patterns. Following the pattern, the second problem should be related to data structures.

In a distant kingdom, there are $n$ cities. There are $n-1$ bidirectional roads between the cities, which ensure that any two cities can reach each other directly or indirectly.

In ancient times, these $n$ cities were in a state of war. In a highly isolated environment, each city developed its own language. After the unification of the kingdom, the lack of a common language brought great obstacles to the development of the kingdom. To improve this situation, the king ordered the design of $m$ common languages and carried out $m$ language unification tasks. In the $i$-th unification task, a minister started from city $s_i$, traveled along the shortest path to $t_i$, and taught all cities along the way (including $s_i$ and $t_i$) to use the $i$-th common language.

Once a common language is available, trade activities can be carried out between cities. Trade activities can be carried out between two cities $u_i$ and $v_i$ if and only if there exists a common language $L$ such that all cities on the shortest path from $u_i$ to $v_i$ (including $u_i$ and $v_i$) use $L$.

To measure the effectiveness of the language unification work, the king wants you to calculate how many pairs of cities $(u, v)$ ($u < v$) can carry out trade activities.

Input

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

The next $n-1$ lines each contain two integers $x_i, y_i$ ($1 \le x_i, y_i \le n$), representing a road connecting cities $x_i$ and $y_i$.

The next $m$ lines each contain two integers $s_i, t_i$ ($1 \le s_i, t_i \le n, s_i \neq t_i$), representing a language popularization task.

Output

Output a single integer representing the number of pairs of cities that can carry out trade activities.

Examples

Input 1

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

Output 1

8

Note 1

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

Constraints

Test Case $n$ $m$ Other Constraints
1 $\le 300$ $\le 300$ None
2
3 $\le 5000$ $\le 5000$ None
4
5 $\le 10^5$ $\le 10^5$ $y_i = x_i + 1$
6
7 $\le 10^5$ $\le 10^5$ None
8
9
10

For 100% of the data, $1 \le s_i, t_i \le n, m \ge 1, s_i \neq t_i, x_i \neq y_i$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.