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

感觉LCA真的是高深莫测

还是看的题解

原来的图是一棵树,新增的m条边

每增加一条,一定会构成一个u-lca(u,v)-v-u的一个环

每两个节点之间的边一定都会被一些环给覆盖 称为覆盖数

计算原图中每条边的覆盖数

如果覆盖数为0 那么这条边只要被删除,新图中任意删去一条边 整个network都会被分割

如果覆盖数为1 那么删除这条边和新图中对应的边 也会被分割

只有覆盖数为2 这两个点才不会被分割

用dp[u]表示节点u和他父亲相连的边的覆盖数

每增加一个新边(u, v)就使dp[u]++,dp[v]++ 而dp[lca(u, v)] -= 2

因为最后从根开始遍历,累加dp

dp[u]++后,路径u-lca(u,v)上的覆盖数都加上了,lca(u,v)到根节点的也被加上了,而实际上是不需要加的因为这条路径没有在环里

所以-2就行了

#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <queue>
#include <stack>
#define inf 0x3f3f3f3f
using namespace std; int n, m, ecnt;
const int maxn = 100005;
int dep[maxn], dp[maxn], parent[maxn][22];
struct node{
int to, next;
}edge[2 * maxn];
int head[maxn];
bool vis[maxn]; void dfs(int u)
{
vis[u] = true;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(!vis[v]){
parent[v][0] = u;
dep[v] = dep[u] + 1;
dfs(v);
}
}
} void init()
{
ecnt = 0;
memset(head, -1, sizeof(head));
memset(vis, 0, sizeof(vis));
memset(dep, 0, sizeof(dep));
memset(parent, -1, sizeof(parent));
memset(dp, 0, sizeof(dp));
} void add(int u, int v)
{
edge[ecnt].to = v;
edge[ecnt].next = head[u];
head[u] = ecnt++;
} void rmq()
{
for(int j = 1; (1 << j) <= n; j++){
for(int i = 1; i <= n; i++){
if(~parent[i][j - 1]){
parent[i][j] = parent[parent[i][j - 1]][j - 1];
}
}
}
} int LCA(int a, int b)
{
if(dep[a] < dep[b]) swap(a, b);
int i;
for(i = 0; (1 << i) <= dep[a]; i++);
i--;
for(int j = i; j >= 0; j--){
if(dep[a] - (1 << j) >= dep[b]){
a = parent[a][j];
}
}
if(a == b){
return a;
}
for(int j = i; j >= 0; j--){
if(parent[a][j] != -1 && parent[a][j] != parent[b][j]){
a = parent[a][j];
b = parent[b][j];
}
}
return parent[a][0];
} void goto_dp(int u)
{
vis[u] = true;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(!vis[v]){
goto_dp(v);
dp[u] += dp[v];
}
}
} int main()
{
while(scanf("%d%d", &n, &m) != EOF){
init();
for(int i = 1; i < n; i++){
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs(1);
rmq();
for(int i = 1; i <= m; i++){
int u, v;
scanf("%d%d", &u, &v);
dp[u]++;
dp[v]++;
dp[LCA(u, v)] -= 2;
}
memset(vis, 0, sizeof(vis));
goto_dp(1);
int ans = 0;
for(int i = 2; i <= n; i++){
if(dp[i] == 0) ans += m;
else if(dp[i] == 1) ans++;
}
printf("%d\n", ans);
}
return 0;
}

poj3417Network【LCA】【树形DP】的更多相关文章

  1. poj3417 LCA + 树形dp

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Descripti ...

  2. Codeforces Round #343 (Div. 2) E. Famil Door and Roads lca 树形dp

    E. Famil Door and Roads 题目连接: http://www.codeforces.com/contest/629/problem/E Description Famil Door ...

  3. HDU 4008 Parent and son LCA+树形dp

    题意: 给定case数 给定n个点的树,m个询问 以下n-1行给出树边 m个询问 x y 问:以x为根.y子树下 y的最小点标的儿子节点 和子孙节点 思路: 用son[u][0] 表示u的最小儿子 s ...

  4. [poj3417]Network(LCA+树形dp)

    题意:给出一棵无根树,然后下面再给出m条边,把这m条边连上,每次你去两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 解题关键:边权转化为点权,记录每条边被环覆盖的次数,通过val[a] ...

  5. 可恶!学了这么久的LCA,联考的题目却是LCA+树形DP!!!可恶|!!!这几天想学学树形DP吧!先来一道入门题HDU 1520 Anniversary party

    题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri, ...

  6. poj3417 Network 树形Dp+LCA

    题意:给定一棵n个节点的树,然后在给定m条边,去掉m条边中的一条和原树中的一条边,使得树至少分为两部分,问有多少种方案. 神题,一点也想不到做法, 首先要分析出加入一条边之后会形成环,形成环的话,如果 ...

  7. hdu_5293_Tree chain problem(DFS序+树形DP+LCA)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5293 被这题打蹦了,看着题解写的,很是爆炸,确实想不到,我用的DFS序+LCA+树形DP,当然也可以写 ...

  8. 【BZOJ-3631】松鼠的新家 树形DP?+ 倍增LCA + 打标记

    3631: [JLOI2014]松鼠的新家 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1231  Solved: 620[Submit][Stat ...

  9. Codeforces 418d Big Problems for Organizers [树形dp][倍增lca]

    题意: 给你一棵有n个节点的树,树的边权都是1. 有m次询问,每次询问输出树上所有节点离其较近结点距离的最大值. 思路: 1.首先是按照常规树形dp的思路维护一个子树节点中距离该点的最大值son_di ...

  10. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

随机推荐

  1. convertView&setTag方法的一点理解

    前言 首先我们要知道setTag方法是干什么的,SDK解释为 Tags Unlike IDs, tags are not used to identify views. Tags are essent ...

  2. 让Json更懂中文(JSON_UNESCAPED_UNICODE)

    我们知道, 用PHP的json_encode来处理中文的时候, 中文都会被编码, 变成不可读的, 类似”\u***”的格式, 还会在一定程度上增加传输的数据量. <?php echo json_ ...

  3. IOS私有API的使用(转)

    最近在做企业级程序,需要搞设备的udid等信息,但是ios7把udid私有化了,不公开使用.所以研究了一下ios的私有api.   调查了一下文章,发现这方面的文章不多,国内更是不全,高手们都懒得写基 ...

  4. js数组获取相同元素个数,归档排序

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. GSAP JS基础教程--TweenLite操作元素的相关属性

    今天来学习用TweenLite操作元素的各种属性,以Div为例,其他元素的操作也是一样的,只是可能一些元素有它们的特殊属性,就可能不同罢了.   代码里用详细注释,我就不再重复啦,大家看代码就可以啦! ...

  6. jenkins构建的robot result结果不更新

    描述:构建的结果不进行更新,仍然显示以往的构建结果 定位原因:pybot 命令中生成的结果文件保存路径与构建后robot结果显示路径不一致所致 解决办法:修改二者的结果保存路径一致

  7. Nginx 过期时间

    客户端(浏览器)访问服务器上的资源后,会缓存在浏览器中,对于一些经常变更的静态资源,我们可以设置缓存时间,也就是设置静态资源的过期时间 [root@localhost ~]$ cat /usr/loc ...

  8. u3d 加密资源并缓存加载

    // C# Example // Builds an asset bundle from the selected objects in the project view. // Once compi ...

  9. MVC的简单分页【转】

    传值的方式是通过querystring. 本例子是把整需要的数据查出来再分页的,因为当时做的时候数据很少,只有几十条. 如果数据多的话,可以在存储过程里分页,只是要传页码和记录的条数过来. 控制器: ...

  10. 《转载》Eclipse项目上传码云

    本文转载自http://blog.csdn.net/izzyliao/article/details/53074452 把Eclipse项目上传到码云的步骤: 1.登录码云:新建项目 2.输入项目名: ...