QOJ.ac

QOJ

時間限制: 2 s 記憶體限制: 512 MB 總分: 100

#4901. Spike & Tom

统计

Once, when Tom was blowing up Jerry's mouse hole with firecrackers, he accidentally blew up Spike's doghouse.

The paths in the backyard form an undirected tree with $n$ nodes. The chase between Spike and Tom proceeds as follows:

  • Tom and Spike start at nodes $a$ and $b$, respectively.
  • Tom and Spike take turns moving, with Tom moving first.
  • In each turn, the player can choose to stay put or move along an edge to an adjacent node.
  • If, after any move, Spike and Tom are at the same position, Spike wins.

Tom is smart enough to know that he will lose this game, so before Spike can react, he adds $m$ extra edges. These extra edges can only be traversed by Tom, not by Spike.

Now, Tom wants to know, for all $n \times (n - 1)$ possible pairs $(a, b)$ where $a \neq b$, in how many of these chases does Tom have a strategy to ensure that Spike can never win?

Input

The first line contains two integers $n$ and $m$.

The next $n - 1$ lines each contain two integers $x, y$, describing a tree edge.

The next $m$ lines each contain two integers $x, y$, describing an extra edge.

Output

Output a single line containing an integer representing the answer.

Examples

Input 1

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

Output 1

18

Note 1

The graph in the example is shown below. Black lines represent tree edges, and red lines represent extra edges. There are $18$ pairs of starting positions $(a, b)$ such that Spike cannot win. These pairs are $(1,2), (1,3), (1,4), (1,5), (2,1), (2,3), (2,4), (2,5), (3,1), (3,2), (3,4), (3,5), (4,1), (4,2), (4,3), (4,5), (5,1), (5,2)$.

Input 2

9 2
1 2
2 3
3 4
2 5
5 6
6 7
7 8
1 9
1 5
5 8

Output 2

48

Constraints

This problem uses bundled testing.

For $100\%$ of the data: $1 \leq n, m \leq 10^5$, $1 \leq x, y \leq n$. For the last $m$ lines, it is guaranteed that $x \neq y$ and all unordered pairs $(x, y)$ are distinct, but it is not guaranteed that the edge $(x, y)$ is not already in the tree.

Subtask 1 ($10$ pts): $n, m \leq 20$.

Subtask 2 ($15$ pts): $n, m \leq 300$.

Subtask 3 ($15$ pts): $n, m \leq 2000$.

Subtask 4 ($20$ pts): $n, m \leq 40000$.

Subtask 5 ($10$ pts): $m = 1$.

Subtask 6 ($30$ pts): No special constraints.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

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.