POJ3417Network
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

从链的情况开始,红色的边为非树边。首先看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和它父亲相连的这条边被几条非树边覆盖。
最后统计答案即可。
#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的更多相关文章
- poj3417Network【LCA】【树形DP】
Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...
- POJ3417Network(LCA+树上查分||树剖+线段树)
Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...
随机推荐
- 看了就很快学会jQuery
一.jQuery简介与第一个jQuery程序 1.1.jQuery简介 1.2.jQuery特点 1.3.jQuery版本 1.4.获得jQuery库 1.5.第一个jQuery程序 二.jQuery ...
- random随机模块,time时间模块
random /随机模块: 作用: 在某个范围内取到每一个值得概率是相通的. 一.随机小数 random.random() import random print(random.random()) ...
- [JavaScript]WebBrowser控件下IE版本的检测
转载请注明原文地址:https://www.cnblogs.com/litou/p/10772272.htm 在客户端检查用户使用的浏览器类型和版本,都是根据navigator.userAgent属性 ...
- can t connect to mysql server on 'localhost'解决方法
源:http://www.111cn.net/database/mysql/37700.htm 先重启服务器可解决大部分问题 错误编号:2003 问题分析: 无法连接到 mysql 服务器,可能的情况 ...
- [note]树链剖分
树链剖分https://www.luogu.org/problemnew/show/P3384 概念 树链剖分,是一种将树剖分成多条不相交的链的算法,并通过其他的数据结构来维护这些链上的信息. 最简单 ...
- win7 32位下载安装redis并安装php_redis扩展
redis打包文件下载地址:http://files.cnblogs.com/files/cuiwenyuan/Redis-3.2.100-Windows-32.zip php_redis.dll下载 ...
- Lattice绘图
lattice包提供了用于可视化单变量和多变量数据的一整套图形系统.许多用户转向使用lattice包是因为它能很容易地生成网格图形.网格图形能够展示变量的分布或变量之间的关系. 例:data(mtca ...
- 每天一个Linux命令(7)pwd命令
pwd命令以绝对路径的方式显示用户当前工作目录.命令将当前目录的全路径名称(从根目录)写入标准输出.全部目录使用/分隔.第一个/表示根目录,最后一个目录是当前目录. (1)用法介绍: pwd[ ...
- 用linux搭建ranzhi环境
一.安装红帽6.5 1.安装时需选择桥接模式: 2.选择自定义,在设置中将镜像文件(ISO)选择进去: 3.安装时选择[桌面]安装(在/etc/inittab文件中,若id=5则为桌面模式,id=3为 ...
- 通过代码设置资源名字,为打包AssetBundle做准备,以及新打包系统
核心代码就是 importer.assetBundleName = name; 但是在这之前,我们需要超找到具体的资源,我们当然是不希望一个一个手动去查找.如果我选择一个文件夹,就可以查找到里边所 ...