洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]

题目描述

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

倍增LCA+树上差分。

树结点的权值等于其子树所有节点的差分结果。

704ms,不知道用树剖求LCA会有多快

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 const int mxn=50010;
 9 int read(){
10     int x=0,f=1;char ch=getchar();
11     while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
12     while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
13     return x*f;
14 }
15 struct edge{
16     int v,nxt;
17 }e[mxn<<1];
18 int hd[mxn],mct=0;
19 void add_edge(int u,int v){
20     e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return;
21 }
22 int n,k;
23 int dep[mxn];
24 int fa[mxn][17];
25 int a[mxn];//差分
26 void DFS(int u,int f){
27     dep[u]=dep[f]+1;
28     for(int i=1;i<17;i++)fa[u][i]=fa[fa[u][i-1]][i-1];
29     for(int i=hd[u];i;i=e[i].nxt){
30         int v=e[i].v;
31         if(v==f)continue;
32         fa[v][0]=u;
33         DFS(v,u);
34     }
35     return;
36 }
37 int LCA(int x,int y){
38     if(dep[x]<dep[y])swap(x,y);
39     for(int i=16;i>=0;i--){
40         if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
41     }
42     if(x==y)return x;
43     for(int i=16;i>=0;i--){
44         if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
45     }
46     return fa[x][0];
47 }
48 int ans=-1e9;
49 int clc(int u,int f){
50     int res=a[u];
51     for(int i=hd[u];i;i=e[i].nxt){
52         int v=e[i].v;
53         if(v==f)continue;
54         res+=clc(v,u);
55     }
56     ans=max(ans,res);
57     return res;
58 }
59 int main(){
60     n=read();k=read();
61     int i,j,x,y;
62     for(i=1;i<n;i++){
63         x=read();y=read();
64         add_edge(x,y);
65         add_edge(y,x);
66     }
67     DFS(1,0);
68     for(i=1;i<=k;i++){
69         x=read();y=read();
70         a[x]++;a[y]++;
71         int tmp=LCA(x,y);
72         a[tmp]--;
73         if(fa[tmp][0])a[fa[tmp][0]]--;
74     }
75     clc(1,0);
76     printf("%d\n",ans);
77     return 0;
78 }
时间: 2024-10-05 20:40:56

洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]的相关文章

洛谷P3128 [USACO15DEC]最大流Max Flow

洛谷P3128 [USACO15DEC]最大流Max Flow 题目描述 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

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

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

洛谷——P3128 [USACO15DEC]最大流Max Flow

https://www.luogu.org/problem/show?pid=3128 题目描述 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-

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, and all stalls are connected t

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, and all stalls are connected t

[USACO15DEC]最大流Max Flow

题目:洛谷P3128. 题目大意:一棵n个点的树,每次将两个节点最短路径所覆盖的所有节点的流量加1.问你最后流量最大的节点的流量是多少. 解题思路:裸的树上差分. 对于每次增加流量,我们把两个节点的流量+1,它们的lca和lca的父亲的流量-1. 最后求一遍子树和,求出来每个节点的子树和就是该节点实际流量. 最后求最大值即可. 时间复杂度:树剖.倍增LCA $O(m\log_2 n)$,Tarjan LCA $O(n+m)$. 我采用Tarjan算法,288ms. C++ Code: #incl

AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128

题目描述 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

洛谷 P2936 [USACO09JAN]全流Total Flow

题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an appa