Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5311   Accepted: 1523

Description

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

Source

 
题目大意:对于一棵树,加上m条非树边。每次只能删一条树边和一条非树边使图不连通有多少种方法 。
题解:LCA+树上差分
首先可以想到的暴力为两重for循环枚举删的树边与非树边,然后bfs判断图是否联通。
然后我们可以优化一下,枚举树边,讨论删掉当前枚举的树边,和多少条非树边图能不连通。
分类讨论。

从链的情况开始,红色的边为非树边。首先看1--2这条边,没有任何非树边覆盖,所以要删这条边,

你删任何一条树边都可以,所以这条边对答案的贡献是m。再看4--5这条边,被一条非树边覆盖,如果

删去这条边并且再删去一条非树边,想要使图不连通,只能删覆盖它的非树边,所以当只有一条非树边

覆盖这条边时,这条边对答案的贡献是1。再看2--3这条边,如果删去这条边,想要使图不连通,你删哪条

非树边都是没有用的,图仍会连通。对答案产生贡献的树边的条件是,如果没有被非树边覆盖,产生的

贡献是m,如果被一条非树边覆盖,产生的贡献就是1,即删掉覆盖它的非树边,如果被2及其以上的非树

边覆盖,对答案没有贡献,你删那一条非树边图仍然连通。

好了,我们已经讨论完答案的产生,在说明怎样实现。

进行树上差分来实现每一条树边被几条非树边覆盖。

当一条非树边的端点为u,v时,查分数组dp[u]++,dp[v]++,dp[lca(u,v)]-=2.

然后在dfs一遍求差分数组的后缀和,dp[i]表示i和它父亲相连的这条边被几条非树边覆盖。

最后统计答案即可。

1A好开心~=u=//
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100002
using namespace std; int n,m,sumedge,ans;
int head[maxn],top[maxn],deep[maxn],dad[maxn],size[maxn],dp[maxn]; struct Edge{
int x,y,nxt;
Edge(int x=,int y=,int nxt=):
x(x),y(y),nxt(nxt){}
}edge[maxn<<]; void add(int x,int y){
edge[++sumedge]=Edge(x,y,head[x]);
head[x]=sumedge;
} void dfs(int x){
size[x]=;deep[x]=deep[dad[x]]+;
for(int i=head[x];i;i=edge[i].nxt){
int v=edge[i].y;
if(v==dad[x])continue;
dad[v]=x;
dfs(v);
size[x]+=size[v];
}
} void dfs_(int x){
int s=;
if(!top[x])top[x]=x;
for(int i=head[x];i;i=edge[i].nxt){
int v=edge[i].y;
if(v!=dad[x]&&size[v]>size[s])s=v;
}
if(s){
top[s]=top[x];
dfs_(s);
}
for(int i=head[x];i;i=edge[i].nxt){
int v=edge[i].y;
if(v!=dad[x]&&v!=s)dfs_(v);
}
} int lca(int x,int y){
for(;top[x]!=top[y];){
if(deep[top[x]]>deep[top[y]])swap(x,y);
y=dad[top[y]];
}
if(deep[x]>deep[y])swap(x,y);
return x;
} void DP(int x){
for(int i=head[x];i;i=edge[i].nxt){
int v=edge[i].y;
if(v==dad[x])continue;
DP(v);
dp[x]+=dp[v];
}
} int main(){
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
dfs();dfs_();
for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
dp[x]++;dp[y]++;
dp[lca(x,y)]-=;
}
DP();
for(int i=;i<=n;i++){
if(dp[i]==)ans+=m;
else if(dp[i]==)ans++;
//cout<<dp[i]<<endl;
}
cout<<ans<<endl;
return ;
}
 

POJ3417Network的更多相关文章

  1. poj3417Network【LCA】【树形DP】

    Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...

  2. POJ3417Network(LCA+树上查分||树剖+线段树)

    Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...

随机推荐

  1. 自定义TextView带有各类.ttf字体的TextView

    最近项目遇到了将普通文字转化为带有字体样式的文字,这里就涉及到了.ttf文件,我上网百度了不少资料最终终于实现了,现在想想其实并不复杂 1,你需要下载一种.ttf字体文件,你可以从网上找到一种字体的. ...

  2. web.xml配置中的log4jRefreshInterval

    采用spring框架的项目如何使用log4j在spring中使用log4j,有些方便的地方, 1.动态的改变记录级别和策略,即修改log4j.properties,不需要重启web应用,这需要在web ...

  3. 九度OJ 1348:数组中的逆序对 (排序、归并排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2777 解决:656 题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组 ...

  4. First non repeating word in a file? File size can be 100GB.

    1 solution 1 1.1 数据结构 一个Hashmap和一个双向链表.如果想要快速获取first,并且只遍历一次,那么就要想到双向链表和HashMap的组合. 链表可以保证第一个在head处, ...

  5. php解析xml文件为数组

    $xml = simplexml_load_file($fullfilename); $arr = json_decode(json_encode($xml),true); echo "&l ...

  6. Centos设置开机启动Apache和Mysql

    先用chkconfig --list查询apache和mysql服务是否存在,不存在则需要手动添加 [root@centos64 vsftpd]# chkconfig --list 测试存在,只需要开 ...

  7. 让Editplus支持sql语法高亮显示

    版权声明:作者:jiankunking 出处:http://blog.csdn.net/jiankunking 本文版权归作者和CSDN共同拥有,欢迎转载.但未经作者允许必须保留此段声明,且在文章页面 ...

  8. 【Android】开源项目汇总

    Android开源项目第一篇——个性化控件(View)篇  包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Progres ...

  9. VS2013 Pro版本密钥

    Visual Studio Professional 2013 KEY(密钥): XDM3T-W3T3V-MGJWK-8BFVD-GVPKY

  10. python操作mysql(一)原生模块pymysql

    一.下载安装 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 下载安装 C:\Users\Administrator>pip install pymysq ...