Network POJ - 3417(LCA+dfs)

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的情况,直接跳过

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

时间: 2024-10-12 15:31:07

Network POJ - 3417(LCA+dfs)的相关文章

Network POJ - 3694(lca并查集+连通图求桥)

就是求出原先图中的桥的数量,在每一次询问时加入一条新边,求加入当前边后图中剩余的桥的数量 求出原先图中的桥的数量,然后减去新加入边的两端点之间的桥的数量,就是剩余桥的数量.. 用并查集把属于同一集合的放到一起(即两个点之间没有桥的) #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <algo

POJ 1330(LCA模板)

链接:http://poj.org/problem?id=1330 题意:q次询问求两个点u,v的LCA 思路:LCA模板题,首先找一下树的根,然后dfs预处理求LCA(u,v) AC代码: 1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 #include<set> 6 #include<string> 7 #incl

POJ 3411 Paid Roads(dfs)

*注:这一题很重要的是对与数据的处理和细节的把握把! http://poj.org/problem?id=3411 题目大意: 有n个城市,m条路,(0<=n,m<=10).从a到b,如果之前已经经过c点,那么付费p,否者付费r.求最小的费用,从1-->n! 注意: There may be more than one road connecting one city with another. so:你不能用map[a][b]存a->b的距离.只能有road [ i ]了. 还有

Mondriaan&#39;s Dream - POJ 2411(状态压缩)

题目大意:有一些1*2的矩形,现在用这些小矩形覆盖M*N的大矩形,不能重复覆盖,并且要覆盖完全,求有多少种覆盖方式. 分析:可以使用1和0两种状态来表示这个位置有没有放置,1表示放置,0表示没有放置,可以有三种放置方式. 一,竖着放. 二,不放.三,横着放.直接DFS这些情况就行了................还是递归容易理解. 代码如下: =============================================================================

POJ 2342 (树形DP)

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure

poj2386 Lake Counting(简单DFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1562 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢

King&#39;s Quest POJ - 1904(强连通分量)

建图:王子u喜欢女孩v,则u到v连一条边.对于给出的初始完美匹配,王子u与女孩v匹配,则v到u连一条边.然后求SCC. 显然对于同一个SCC中王子数目和女孩数目是相等的,并且从某个王子出发能够到达所有女孩,这样,王子可以和属于同一个SCC中的任意一个女孩结婚,而不会影响其他王子. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <

Poj 1328(雷达安装)几何+贪心

[题目描述]: 给定n个小岛以及这些小岛的位置,并输入雷达的辐射面积,问最少需要多少个雷达站才能覆盖所有小岛? [思路分析]: 本题首先想到的是运用贪心算法,但是算法想到了如何贪心?这道题我自己开始做之时只有一点思路,就是让每一个雷达覆盖较多的点,但是如何较多覆盖,这就是典型的数学问题了,自己没有思索出来,最后在网上看了题解才明白如何做.下面我们看看如何建图: 我们通过这个图首先运用了一个数学知识,就是以小岛为圆心,雷达辐射范围为圆心建立一个圆,该圆与x轴有一个交点,以该交点作为雷达站铺设点那么

poj 1006(剩余定理)

生理周期 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 111426   Accepted: 34702 Description 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰,人会思维敏捷,精力容易高度集中.因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天.对于每个人,我们想知道