1. 程式人生 > >Network POJ - 3417(LCA+dfs)

Network POJ - 3417(LCA+dfs)

rsquo formal connect intent contain build one network bad

Yixght is a manager of the company called SzqNetwork(SN). Now she‘s very worried because she has just received a bad news which denotes that DxtNetwork(DN), the SN‘s business rival, intents to attack the network of SN. More unfortunately, the original network of SN is so weak that we can just treat it as a tree. Formally, there are N

nodes in SN‘s network, N-1 bidirectional channels to connect the nodes, and there always exists a route from any node to another. In order to protect the network from the attack, Yixght builds M new bidirectional channels between some of the nodes.

As the DN‘s best hacker, you can exactly destory two channels, one in the original network and the other among the M

new channels. Now your higher-up wants to know how many ways you can divide the network of SN into at least two parts.

Input

The first line of the input file contains two integers: N (1 ≤ N ≤ 100 000), M (1 ≤ M ≤ 100 000) — the number of the nodes and the number of the new channels.

Following N-1 lines represent the channels in the original network of SN, each pair (a,b) denote that there is a channel between node a and node b.

Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between node a and node b is added to the network of SN.

Output

Output a single integer — the number of ways to divide the network into at least two parts.

Sample Input

4 1
1 2
2 3
1 4
3 4

Sample Output

3

題意:給出一棵n個點的無根樹,然後有m條附加邊,可以先斬斷一條樹邊,然後再斬斷一條附加邊
問有多少種方法將其變成兩個聯通塊

思路:我們想到對於原本的無附加邊的無根樹,添加一條附加邊,就相當於變成一個環。
基於‘樹上差分’思想,對一條附加邊的兩端端點+1,然後沿著兩個端點向上賦值,直到最近公共祖先(剛好環結束),
這樣一個環上的路徑都被賦值了1,為了保證環之外不會被賦值,應該再最近公共祖先處-2。

坑點:在附加邊給出中 會出現 a == b的情況,直接跳過



Network POJ - 3417(LCA+dfs)