bzoj4390: [Usaco2015 dec]Max Flow

4390: [Usaco2015 dec]Max Flow

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 139  Solved: 85
[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

HINT

Source

已经忘了树上差分怎么写的...

给s和t加一 lca(s,t)和lca(s,t)的父亲-1

然后每个点的值就是子树的权值和

 1 #include<bits/stdc++.h>
 2 #define rep(i,l,r) for(int i=l;i<=r;++i)
 3 using namespace std;
 4 const int N=102333;
 5 int head[N],tot,dep[N],sz[N],fa[N],son[N],top[N],size[N],sum[N],n,Q,a,b,ans,c;
 6 struct node{
 7     int next,to;
 8 }e[N<<1];
 9 inline void ins(int u,int v) {
10      e[++tot].to=v; e[tot].next=head[u]; head[u]=tot;
11 }
12 inline void dfs1(int x,int pre) {
13      dep[x]=dep[fa[x]=pre]+1; sz[x]=1;
14      for(int k=head[x];k;k=e[k].next) {
15           if(e[k].to==pre)  continue;
16           dfs1(e[k].to,x);
17           sz[x]+=sz[e[k].to];
18           if(sz[e[k].to]>sz[son[x]]) son[x]=e[k].to;
19      }
20 }
21 void dfs2(int x,int tr) {
22      top[x]=tr;
23      if(son[x]) dfs2(son[x],tr);
24      for(int k=head[x];k;k=e[k].next) {
25           if(e[k].to==fa[x] || e[k].to==son[x]) continue;
26           dfs2(e[k].to,e[k].to);
27      }
28 }
29 inline int lca(int x,int y) {
30      int a,b;
31      a=top[x]; b=top[y];
32      while(a!=b) {
33          if(dep[a]<dep[b]) swap(a,b),swap(x,y);
34          x=fa[a]; a=top[x];
35      }
36      if(dep[x]<dep[y]) return x;else return y;
37 }
38 void dfs(int x,int fa){
39     size[x]=sum[x];
40     for(int k=head[x];k;k=e[k].next) if(fa!=e[k].to){
41         dfs(e[k].to,x); size[x]+=size[e[k].to];
42     }
43 }
44 int main(){
45     scanf("%d%d",&n,&Q);
46     rep(i,1,n-1) scanf("%d%d",&a,&b),ins(a,b),ins(b,a);
47     dfs1(1,0); dfs2(1,1);
48     rep(i,1,Q) {
49          scanf("%d%d",&a,&b);  c=lca(a,b);
50          sum[fa[c]]--; sum[a]++; sum[b]++; sum[c]--;
51     }
52     dfs(1,0);
53     rep(i,1,n) ans=max(ans,size[i]);
54     printf("%d\n",ans);
55 }

时间: 2024-08-01 22:48:13

bzoj4390: [Usaco2015 dec]Max Flow的相关文章

bzoj4390: [Usaco2015 dec]Max Flow(LCA+树上差分)

题目大意:给出一棵树,n(n<=5w)个节点,k(k<=10w)次修改,每次给定s和t,把s到t的路径上的点权+1,问k次操作后最大点权. 对于每次修改,给s和t的点权+1,给lca(s,t)和lca(s,t)的父亲的点权-1,每一个点的权就是它与它的子树权和,实际上就是树上的差分,又涨姿势了... 代码如下: uses math; type point=^rec; rec=record data:longint; next:point; end; var n,m,x,y,i,ans,fa,k

[Usaco2015 dec]Max Flow

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 p

bzoj4393: [Usaco2015 Dec]Fruit Feast

题意: T,A,B.T是上限.A和B可以随意吃但是不能超过T.有一次将吃的东西/2的机会.然后可以继续吃,不能超过T.问最多可以吃多少. =>我们先处理不能/2可以吃到哪些.然后弄个双指针扫一扫就可以了TAT #include<cstdio> #include<cstring> #include<cctype> #include<algorithm> using namespace std; #define rep(i,s,t) for(int i=s

洛谷 P3128 [USACO15DEC]最大流Max Flow

P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls in his barn (2 \leq N \leq 50,0002≤N≤50,000), conveniently numbered 1 \ldots N1…N. Each pipe connects a pair of stalls,

bzoj4397【Usaco2015 Dec】Breed Counting

4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 29  Solved: 25 [Submit][Status][Discuss] Description Farmer John's N cows, conveniently numbered 1-N, are all standing in a row (they seem to do so often that it now

洛谷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

bzoj4396[Usaco2015 dec]High Card Wins*

bzoj4396[Usaco2015 dec]High Card Wins 题意: 一共有2n张牌,Alice有n张,Bob有n张,每一局点数大的赢.知道Bob的出牌顺序,求Alice最多能赢几局.n≤50000. 题解: 贪心.将Alice和Bob的牌按点数大小排序,然后如果Alice当前牌能赢Bob当前牌就ans++否则就不断调整Bob的当前牌直到Alice当前牌能赢Bob当前牌. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #

bzoj4397[Usaco2015 dec]Breed Counting*

bzoj4397[Usaco2015 dec]Breed Counting 题意: 给定一个长度为N的序列,每个位置上的数只可能是1,2,3中的一种.有Q次询问,每次给定两个数a,b,请分别输出区间[a,b]里数字1,2,3的个数.n≤100000,q≤100000. 题解: 裸前缀和. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define inc(i,j,k) for

bzoj4395[Usaco2015 dec]Switching on the Lights*

bzoj4395[Usaco2015 dec]Switching on the Lights 题意: n*n个房间,奶牛初始在(1,1),且只能在亮的房间里活动.每当奶牛经过一个房间,就可以打开这个房间里控制其它房间灯的开关.问奶牛最多可点亮多少个房间.n≤100. 题解: 因为只要一个房间灯亮了,它将一直亮着,所以可以做bfs,每次由队列中的节点扩展可以到的节点.然而这样做不行,因为可能之前尝试过不能到达的房间的灯可以在之后到达的房间里被打开.解决方法是不停做bfs,直到答案不再更新. 代码: