#P5327. Universal Language Trade

    ID: 18560 Type: Default 1000ms 256MiB

Universal Language Trade

Universal Language Trade

In a kingdom with \( n \) cities connected by \( n-1 \) bidirectional roads (forming a tree), the diversity of languages once impeded trade. In ancient times, each city developed its own language. After unification, the king initiated \( m \) language unification operations in order to establish common languages across the kingdom.

In the \( i \)-th unification operation, a minister travels from city \( s_i \) to city \( t_i \) along the unique shortest path in the tree. Every city on this path (including \( s_i \) and \( t_i \)) learns the \( i \)-th common language.

Two cities \( u \) and \( v \) (with \( u < v \)) can engage in trade if and only if there exists a common language \( L \) such that every city on the unique shortest path between \( u \) and \( v \) uses \( L \). Your task is to compute the number of city pairs \( (u, v) \) with \( u < v \) that can trade.

Note: In a tree, the unique path between any two cities is the simple path connecting them.

inputFormat

The first line contains two integers \( n \) and \( m \) --- the number of cities and the number of language unification operations.

The next \( n-1 \) lines each contain two integers \( u \) and \( v \), representing a bidirectional road connecting cities \( u \) and \( v \).

The following \( m \) lines each contain two integers \( s_i \) and \( t_i \), indicating that in the \( i \)-th operation a minister travels from city \( s_i \) to city \( t_i \) to teach the \( i \)-th common language.

outputFormat

Output a single integer, the number of pairs \( (u, v) \) with \( u < v \) that can engage in trade.

sample

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

</p>