P3128 [USACO15DEC]最大流Max Flow

题目描述

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

FJ is pumping milk between  pairs of stalls (). For the th such pair, you are told two stalls  and , 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  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  to , then it counts as being pumped through the endpoint stalls  and

, as well as through every stall along the path between them.

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

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

输入输出格式

输入格式:

The first line of the input contains  and .

The next  lines each contain two integers  and  () describing a pipe

between stalls  and .

The next  lines each contain two integers  and  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
/*
树上差分:对于树上x,y之间的路径区间修改时,设数组为c
则c[x]+1,c[y]+1,c[lca(x,y)]-1,c[father[lca(x,y)]-1.
最后dfs一下,使每个节点c[x]+=每个子节点的c值就ok了..
那就来说明一下树上差分这个据说完爆树剖的东西:
对于两个点,把他们的路径上所有点包括他们权值加一。开始路径上权值都为零,
先把起点终点权值加一,然后把它们分别往LCA权值上传,易知,LCA被加了2遍,所以减一。
因为标记上传时不可避免的把LCA的+1标记也上传了,所以就要把她减一成为零,然后上传。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100001
#define S 21 using namespace std;
int deep[maxn],head[maxn],p1,p2,n,m,num,ans,s,x,y,fa[maxn][S+];
int w[maxn],w2[maxn];
struct node {
int from;
int to;
int next;
}e[maxn*]; void add(int from,int to)
{
e[num].from=from;
e[num].to=to;
e[num].next=head[from];
head[from]=num;
num++;
} int init()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} void swap(int &a,int &b)
{
int t=a;a=b;b=t;
} void get_fa()
{
for(int j=;j<=S;j++)
for(int i=;i<=n;i++)
fa[i][j]=fa[fa[i][j-]][j-];
} void Dfs(int now,int from,int c)
{
fa[now][]=from;
deep[now]=c;
for(int i=head[now];~i;i=e[i].next)
{
int& v=e[i].to;
if(v!=from)
Dfs(v,now,c+);
}
} int get_same(int a,int t)
{
for(int i=;i<S;i++)
if(t&(<<i)) a=fa[a][i];
return a;
} int LCA(int a,int b)
{
if(deep[a]<deep[b]) swap(a,b);
a=get_same(a,deep[a]-deep[b]);
if(a==b) return a;
for(int i=S;i>=;i--) {
if(fa[a][i]!=fa[b][i])
{
a=fa[a][i];
b=fa[b][i];
}
}
return fa[a][];
} void work(int u,int v)//树上差分
{
int s=LCA(u,v);
w[u]++;
w[v]++;
w[s]--;
if(fa[s][]!=-) w[fa[s][]]--;
} int dfs2(int now,int from)
{
w2[now]=w[now];
for(int i=head[now];~i;i=e[i].next)
{
int& v=e[i].to;
if(v!=from)
dfs2(v,now),
w2[now]+=w2[v];//上传标记
}
ans=max(ans,w2[now]);
return ans;
} int main()
{
memset(head,-,sizeof head);
n=init();m=init();
int x,y;
for(int i=;i<n;i++)
{
x=init();y=init();
add(x,y);
add(y,x);
}
Dfs(,-,);
get_fa();
for(int i=;i<=m;i++)
{
x=init();y=init();
work(x,y);
}
ans=dfs2(,-);
printf("%d\n",ans);
return ;
}

最新文章

  1. RecyclerView解密篇(一)
  2. Huffman Tree
  3. noip 借教室 线段树95分做法
  4. 基于 URL 的权限控制
  5. Careercup | Chapter 7
  6. [ZZ] 基于DirectX shader的Per-pixel lighting实现
  7. php 支持断点续传的文件下载类
  8. C# 微信海报
  9. DTrace to Troubleshoot Java Native Memory Problems
  10. DOM的event对象的属性和方法
  11. http接口加密《一》:移动应用中,通过在客户端对访问的url进行加密处理来保护服务器上的数据
  12. 团队项目第二阶段个人进展——Day3
  13. 【PHP篇】输出方法
  14. 虚拟机中安装Virtualbox,嵌套的虚拟机不能运行64位系统
  15. requests库入门09-OAUTH认证
  16. db2 reorg详解
  17. 使用 HTTP/2 提升性能的几个建议
  18. flex对象.show()的时候display变成block
  19. python3 得到a.txt中有的而b.txt中没有的汉字
  20. 网络性能测试工具iperf

热门文章

  1. Bullet:关于ORACLE中的HASH JOIN的参数变化
  2. jQuery元素节点的插入
  3. cin的返回对象
  4. FileReader实现读取文件内容并输出到屏幕上
  5. change legend layout from &#39;vertical&#39; to &#39;horizontal&#39; in Paraview
  6. Ural 1036 Lucky Tickets
  7. android Fragment用法
  8. 6、Java并发性和多线程-并发性与并行性
  9. bzoj1072【SCOI2007】排列perm
  10. License使用成本估算