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 ...
随机推荐
- C语言合并两个集合(L,L1) 将L1中不在L中的元素插入到L线性表中
void main(){ Sqlist L,L1; InitList(&L); InitList(&L1); ListInsert(&L, 1, 2); ListInsert( ...
- 【python】-- 类的反射
反射 反射我们以后会经常用到,这个东西实现了动态的装配,通过字符串来反射类中的属性和方法 一.反射函数 1.hasarttr(obj,name_str) 作用:判断一个对象obj中是否有对应的name ...
- 我的Android进阶之旅------>关于android:layout_weight属性的一个面试题
最近碰到一个面试题,按照下图,由Button和EditText组成的界面下厨布局代码,解决这题目需要使用android:layout_weight的知识. 首先分析上图所示的界面可以看成一下3个部分. ...
- 我的Android进阶之旅------>HTTP Header 详解
HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服务器给与响应.就整个网络资源传 ...
- PAT 组合数的和(15)
给定N个非0的个位数字,用其中任意2个数字都可以组合成1个2位的数字.要求所有可能组合出来的2位数字的和.例如给定2.5.8,则可以组合出:25.28.52.58.82.85,它们的和为330. 输入 ...
- cocos2d-x3.6 生成带类图的离线文档
我的博客:http://blog.csdn.net/dawn_moon cocos2d-x的官网有点慢,并且最新3.6的在线API文档居然没有了类图,不知道什么原因,之前2.2.6都是有的. 只是能够 ...
- [转载]Hibernate如何提升数据库查询的性能
目录(?)[-] 数据库查询性能的提升也是涉及到开发中的各个阶段在开发中选用正确的查询方法无疑是最基础也最简单的 SQL语句的优化 使用正确的查询方法 使用正确的抓取策略 Hibernate的性能优化 ...
- 禁用chrome浏览器的cookie
Chrome: 1.打开chrome浏览器,点击右上角的“自定义和控制Google Chrome”按钮 2.在下拉菜单中选择设置 3.点击设置页底部的“显示高级设置...” 4.在隐私设置下,点击“内 ...
- html ---- web sql 例子
<!doctype html> <html> <head> <meta charset="utf-8"> <script> ...
- Python 3 udp 套接字
Python 3 udp套接字 TCP是建立可靠连接,并且通信双方都可以以流的形式发送数据.相对TCP,UDP则是面向无连接的协议 使用UDP协议时,不需要建立连接,只需要知道对方的IP地址和端口号, ...