#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 cities. There are bidirectional roads between the cities, and these roads guarantee that any two cities can reach each other directly or indirectly.
In ancient times, these 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 common languages and carried out language unification operations. In the -th operation, a minister started from city , walked to along the shortest path, and taught every city on the way (including and ) to use the -th common language.
Once there is a shared language, cities can conduct trade. Two cities can conduct trade if and only if there exists a common language such that every city on the shortest path from to (including and ) can use .
To measure the effect of the language unification operations, the king wants you to compute how many pairs of cities with can conduct trade.
Input Format
The first line contains two positive integers , representing the number of cities and the number of common languages.
The next lines each contain two integers (), indicating a road connecting cities and .
The next lines each contain two integers (), 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 | Other Constraints | ||
|---|---|---|---|
| None | |||
| None | |||
For of the testdata, , , and .
Translated by ChatGPT 5
京公网安备 11011102002149号