码迷,mamicode.com
首页 > Web开发 > 详细

Network POJ - 3417(LCA+dfs)

时间:2019-04-27 00:50:17      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:sam   more   div   基于   lan   ==   思路   附加   des   

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)

标签:sam   more   div   基于   lan   ==   思路   附加   des   

原文地址:https://www.cnblogs.com/iwannabe/p/10777269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!