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 to each-other via paths of pipes.
FJ is pumping milk between KK pairs of stalls (1 \leq K \leq 100,0001≤K≤100,000 ). For the ii th such pair, you are told two stalls s_isi and t_iti, 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 s_isi to t_iti , then it counts as being pumped through the endpoint stalls s_isi and
t_iti , 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 NN and KK .
The next N-1N−1 lines each contain two integers xx and yy (x \ne yx≠y ) describing a pipe
between stalls xx and yy .
The next KK lines each contain two integers ss and tt 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
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int N=1e5+5; int n,m; int head[N],num_edge; struct Edge { int v,nxt; }edge[N<<1]; struct Node { int fa,son; int dep,top; int s,t; int size; }node[N]; struct TREE { TREE *lson,*rson; int l,r,mid,len; int num,lazy; }tree[N<<2]; typedef TREE* Tree; Tree now_node=tree,Root; inline int read() { char c=getchar();int num=0; for(;!isdigit(c);c=getchar()); for(;isdigit(c);c=getchar()) num=num*10+c-'0'; return num; } inline void add_edge(int u,int v) { edge[++num_edge].v=v; edge[num_edge].nxt=head[u]; head[u]=num_edge; } void dfs1(int u) { node[u].size=1; for(int i=head[u],v;i;i=edge[i].nxt) { v=edge[i].v; if(v==node[u].fa) continue; node[v].dep=node[u].dep+1; node[v].fa=u; dfs1(v); node[u].size+=node[v].size; if(node[v].size>node[node[u].son].size) node[u].son=v; } } int bound; void dfs2(int u,int top) { node[u].top=top; node[u].s=++bound; if(node[u].son) { dfs2(node[u].son,top); for(int i=head[u],v;i;i=edge[i].nxt) { v=edge[i].v; if(v==node[u].son||v==node[u].fa) continue; dfs2(v,v); } } node[u].t=bound; } void build(Tree &root,int l,int r) { root=++now_node; root->l=l,root->r=r,root->mid=l+r>>1,root->len=r-l+1; if(l==r) return; build(root->lson,l,root->mid); build(root->rson,root->mid+1,r); } void pushdown(Tree root) { if(root->lazy) { root->lson->lazy+=root->lazy; root->rson->lazy+=root->lazy; root->lson->num+=root->lazy; root->rson->num+=root->lazy; root->lazy=0; } } void update(Tree root,int l,int r) { if(l==root->l&&r==root->r) { root->lazy+=1; ++root->num; return; } pushdown(root); if(r<=root->mid) update(root->lson,l,r); else if(l>root->mid) update(root->rson,l,r); else { update(root->lson,l,root->mid); update(root->rson,root->mid+1,r); } root->num=max(root->lson->num,root->rson->num); } inline void Modify(int x,int y) { int fx=node[x].top,fy=node[y].top; while(fx!=fy) { if(node[fx].dep>node[fy].dep) { update(Root,node[fx].s,node[x].s); x=node[fx].fa; fx=node[x].top; } else { update(Root,node[fy].s,node[y].s); y=node[fy].fa; fy=node[y].top; } } if(node[x].dep>node[y].dep) update(Root,node[y].s,node[x].s); else update(Root,node[x].s,node[y].s); } int main() { n=read(),m=read(); for(int i=1,u,v;i<n;++i) { u=read(),v=read(); add_edge(u,v); add_edge(v,u); } dfs1(1); dfs2(1,1); build(Root,1,n); for(int i=1,u,v;i<=m;++i) { u=read(),v=read(); Modify(u,v); } printf("%d",Root->num); return 0; }