题目描述

\(FJ\)给他的牛棚的\(N(2≤N≤50,000)\)个隔间之间安装了\(N-1\)根管道,隔间编号从\(1\)到\(N\)。所有隔间都被管道连通了。

\(FJ\)有\(K(1≤K≤100,000)\)条运输牛奶的路线,第\(i\)条路线从隔间\(s_i\)运输到隔间\(t_i\)。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少。

输入输出格式

输入格式:

The first line of the input contains \(N\) and \(K\).

The next \(N-1\) lines each contain two integers \(x\) and \(y\) \((x \ne y\)) describing a pipe

between stalls \(x\) and \(y\).

The next \(K\) lines each contain two integers \(s\) and \(t\) describing the endpoint

stalls of a path through which milk is being pumped.

输出格式:

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

barn.

输入输出样例

输入样例#1:

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

输出样例#1:

9

思路:树链剖分+线段树,题目中给出的每组点就相当于是路径修改,也就是树链剖分里面的基本操作,要求输出的最大压力值的隔间就是一到n压力值的最大值,因此我们只需要再用线段树维护区间最大值就好了。

代码:

#include<cstdio>
#include<algorithm>
#include<cctype>
#define maxn 50007
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
int id[maxn],d[maxn],n,k,cnt,son[maxn],top[maxn],maxx[maxn<<2];
int head[maxn],num,fa[maxn],siz[maxn],lazy[maxn<<2];
inline int qread() {
char c=getchar();int num=0,f=1;
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) num=num*10+c-'0';
return num*f;
}
const int inf=0x7fffffff;
struct node {
int v,nxt;
}e[maxn<<1];
inline void ct(int u, int v) {
e[++num].v=v;
e[num].nxt=head[u];
head[u]=num;
}
void dfs1(int u) {
siz[u]=1;
for(int i=head[u];i;i=e[i].nxt) {
int v=e[i].v;
if(v!=fa[u]) {
d[v]=d[u]+1;
fa[v]=u;
dfs1(v);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]]) son[u]=v;
}
}
}
void dfs2(int u, int t) {
id[u]=++cnt;
top[u]=t;
if(son[u]) dfs2(son[u],t);
for(int i=head[u];i;i=e[i].nxt) {
int v=e[i].v;
if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
}
}
inline void pushup(int rt) {
maxx[rt]=max(maxx[ls],maxx[rs]);
}
inline void pushdown(int rt) {
if(lazy[rt]) {
lazy[ls]+=lazy[rt],maxx[ls]+=lazy[rt];
lazy[rs]+=lazy[rt],maxx[rs]+=lazy[rt];
lazy[rt]=0;
}
}
void modify(int rt, int l, int r, int L, int R, int val) {
if(L>r||R<l) return;
if(L<=l&&r<=R) {
lazy[rt]+=val,maxx[rt]+=val;
return;
}
int mid=(l+r)>>1;
pushdown(rt);
modify(ls,l,mid,L,R,val),modify(rs,mid+1,r,L,R,val);
pushup(rt);
}
int cmax(int rt, int l, int r, int L, int R) {
if(L>r||R<l) return -inf;
if(L<=l&&r<=R) return maxx[rt];
int mid=(l+r)>>1,ans=-inf;
pushdown(rt);
if(L<=mid) ans=max(ans,cmax(ls,l,mid,L,R));
if(R>mid) ans=max(ans,cmax(rs,mid+1,r,L,R));
return ans;
}
void cal(int x, int y, int val) {
int fx=top[x],fy=top[y];
while(fx!=fy) {
if(d[fx]<d[fy]) swap(x,y),swap(fx,fy);
modify(1,1,cnt,id[fx],id[x],val);
x=fa[fx],fx=top[x];
}
if(id[x]>id[y]) swap(x,y);
modify(1,1,cnt,id[x],id[y],val);
}
int main() {
n=qread(),k=qread();
for(int i=1,u,v;i<n;++i) {
u=qread(),v=qread();
ct(u,v),ct(v,u);
}
dfs1(1);dfs2(1,1);
for(int i=1,u,v;i<=k;++i) {
u=qread(),v=qread();
cal(u,v,1);
}
printf("%d\n",cmax(1,1,cnt,1,cnt));
return 0;
}

最新文章

  1. Android抓包方法(一)之Fiddler代理
  2. linux集群运维工具:clustershell和pssh
  3. [IIS]IIS扫盲(一)
  4. IIS问题解决:URL中制表符引起的Bad Request - Invalid URL
  5. CodeFirst写界面——自己写客户端UI库
  6. Unity3D 自动打包整个项目(以AssetBundle实现)
  7. (原创)CityEngine 2014和ArcGIS 10.3冲突问题的解决
  8. 提示35. 怎样实现OfTypeOnly&lt;TEntity&gt;()这样的写法
  9. console.debug()浏览器控制台打印输出 仅仅在支持console的浏览器下打印
  10. D - Silver Cow Party
  11. JavaScript:Object.prototype.toString方法的原理
  12. DataTable复制自身行
  13. nodejs之简介及安装(一)
  14. Zepto
  15. Python3中的模块
  16. 【JAVASCRIPT】React学习- 与 flux 结合使用
  17. RESTful Console Application
  18. luogu P5301 [GXOI/GZOI2019]宝牌一大堆
  19. Delphi 带星期几的日期格式化
  20. 1005 继续(3n+1)猜想 (25 分)

热门文章

  1. Mex混合编程专题二:MEX Hello Word
  2. 本地未安装Oracle数据库,如何连接远程Oracle数据库
  3. 利用stomp.js实现websocket功能,接收ActiveMQ消息队列
  4. uoj problem 14 DZY Loves Graph
  5. POJ1456:Supermarket
  6. yum配置文件位置
  7. 关于 sklearn.decomposition.KernelPCA的简单介绍
  8. SSDB VS redis
  9. 8、非root权限下安装perl以及perl模块
  10. windows10 Ubuntu子系统下卸载Mysql重装