Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 204  Solved: 129
[Submit][Status][Discuss]

Description

Farmer
John has installed a new system of N−1 pipes to transport milk between
the N stalls in his barn (2≤N≤50,000), conveniently numbered 1…N. Each
pipe connects a pair of stalls, and all stalls are connected to
each-other via paths of pipes.

FJ is pumping milk between KK pairs of stalls (1≤K≤100,000). For the
iith such pair, you are told two stalls sisi and titi, endpoints of a
path along which milk is being pumped at a unit rate. FJ is concerned
that some stalls might end up overwhelmed with all the milk being pumped
through them, since a stall can serve as a waypoint along many of the
KK paths along which milk is being pumped. Please help him determine the
maximum amount of milk being pumped through any stall. If milk is being
pumped along a path from sisi to titi, then it counts as being pumped
through the endpoint stalls sisi and titi, as well as through every
stall along the path between them.

给定一棵有N个点的树,所有节点的权值都为0。

有K次操作,每次指定两个点s,t,将s到t路径上所有点的权值都加一。

请输出K次操作完毕后权值最大的那个点的权值。

Input

The first line of the input contains NN and KK.

The next N−1 lines each contain two integers x and y (x≠y,x≠y) describing a pipe between stalls x and y.

The next K lines each contain two integers ss and t describing the endpoint stalls of a path through which milk is being pumped.

Output

An integer specifying the maximum amount of milk pumped through any stall in the barn.

Sample Input

5 10
3 4
1 5
4 2
5 4
5 4
5 4
3 5
4 3
4 3
1 3
3 5
5 4
1 5
3 4

Sample Output

9

Source

Platinum鸣谢Claris提供译文

思路

树链剖分

代码实现

 #include<cstdio>
const int maxn=5e4+;
inline int min_(int x,int y){return x<y?x:y;}
inline int max_(int x,int y){return x>y?x:y;}
inline int swap_(int&x,int&y){x^=y,y^=x,x^=y;}
int n,k;
int a,b;
int eh[maxn],hs,et[maxn<<],en[maxn<<];
int pd[maxn],pf[maxn],pws[maxn],psz[maxn],pps,pp[maxn],pt[maxn];
int ts[maxn<<],tf[maxn<<];
void dfs1(int k,int f,int d){
psz[k]=,pd[k]=d,pf[k]=f;
for(int i=eh[k];i;i=en[i])
if(et[i]!=f){
dfs1(et[i],k,d+);
psz[k]+=psz[et[i]];
if(psz[et[i]]>psz[pws[k]]) pws[k]=et[i];
}
}
void dfs2(int k,int t){
pp[k]=++pps,pt[k]=t;
if(pws[k]) dfs2(pws[k],t);
for(int i=eh[k];i;i=en[i])
if(et[i]!=pf[k]&&et[i]!=pws[k])
dfs2(et[i],et[i]);
}
void down(int k){
int ls=k<<,rs=ls|;
ts[ls]+=tf[k],ts[rs]+=tf[k];
tf[ls]+=tf[k],tf[rs]+=tf[k];
tf[k]=;
}
void change(int k,int l,int r,int al,int ar){
if(l==al&&r==ar){ts[k]++,tf[k]++;return;}
if(tf[k]) down(k);
int mid=l+r>>,ls=k<<,rs=ls|;
if(al<=mid) change(ls,l,mid,al,min_(ar,mid));
if(ar>mid) change(rs,mid+,r,max_(al,mid+),ar);
ts[k]=max_(ts[ls],ts[rs]);
}
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<n;i++){
scanf("%d%d",&a,&b);
++hs,et[hs]=b,en[hs]=eh[a],eh[a]=hs;
++hs,et[hs]=a,en[hs]=eh[b],eh[b]=hs;
}
dfs1(,,);
dfs2(,);
while(k--){
scanf("%d%d",&a,&b);
while(pt[a]!=pt[b]){
if(pd[pt[a]]<pd[pt[b]]) swap_(a,b);
change(,,n,pp[pt[a]],pp[a]);
a=pf[pt[a]];
}
if(pd[a]<pd[b]) swap_(a,b);
change(,,n,pp[b],pp[a]);
}
printf("%d\n",ts[]);
return ;
}

最新文章

  1. jquery编写插件的方法
  2. 更高效地提高redis client多线程操作的并发吞吐设计
  3. remot debug
  4. 【Hibernate】Hibernate系列5之检索策略
  5. linux和windows中设置环境变量经常使用命令
  6. (转)关于font-size:100%
  7. About USB Data Link Cable API
  8. hash_map和map的区别
  9. Asp.net MVC4之 一个简单的小例子
  10. SpringMVC返回json是设置编辑等消息头,消息头信息介绍(respone.setHeader,这个从网上获取)
  11. CentOS7 防火墙(firewall)的操作命令(转)
  12. LayoutInflater.inflate()方法两个参数和三个参数
  13. react 组件列表
  14. IntelliJ IDEA快捷键总结
  15. RedHat Linux关闭防火墙的命令
  16. RS485 VS 20mA 电流环
  17. epoll惊群原因分析
  18. 算法训练 P1101
  19. 常用文本编辑器 editor 的常用插件 —— CopyEdit
  20. Python的set集合详解

热门文章

  1. 生成自签名ca 证书 使nginx 支持https
  2. c语言小项目-使用mysql数据库的图书管理系统
  3. magento 翻译使用实例
  4. 生成清除某个数据库下的所有表的SQL语句
  5. A8ERP权限管理系统
  6. Java编程思想读书笔记_第7章
  7. Android 解决RecyclerView瀑布流效果结合Glide使用时图片变形的问题
  8. cideogniter部署到阿里云服务器出现session加载错误
  9. 并发编程学习笔记(13)----ConcurrentLinkedQueue(非阻塞队列)和BlockingQueue(阻塞队列)原理
  10. vba txt读写的几种方式