【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 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 103 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
9HINT
Source
#include #include #include #include #include #include #define F(i,j,n) for(int i=j;i<=n;i++) #define D(i,j,n) for(int i=j;i>=n;i--) #define ll long long #define maxn 50005 #define inf 1000000000 using namespace std; struct edge_type{int to,next;}e[maxn*2]; struct seg{int l,r,mx,tag;}t[maxn*4]; int fa[maxn][20],d[maxn],size[maxn],head[maxn],pos[maxn],belong[maxn]; int n,m,x,y,tmp,cnt,tot; bool vst[maxn]; inline int read() { int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void add_edge(int x,int y) { e[++cnt]=(edge_type){y,head[x]};head[x]=cnt; e[++cnt]=(edge_type){x,head[y]};head[y]=cnt; } inline void dfs1(int x) { size[x]=1;vst[x]=true; F(i,1,20) { if (d[x]<(1<d[x]&&size[e[i].to]>size[k]) k=e[i].to; if (k==0) return; dfs2(k,chain); for(int i=head[x];i;i=e[i].next) if (d[e[i].to]>d[x]&&k!=e[i].to) dfs2(e[i].to,e[i].to); } inline int lca(int x,int y) { if (d[x]=d[y]) x=fa[x][i]; if (x==y) return x; t=int(log2(d[x])); D(i,t,0) if (fa[x][i]!=fa[y][i]) { x=fa[x][i]; y=fa[y][i]; } return fa[x][0]; } inline void pushup(int k) { t[k].mx=max(t[k<<1].mx,t[k<<1|1].mx); } inline void update(int k,int x) { t[k].mx+=x;t[k].tag+=x; } inline void pushdown(int k) { if (!t[k].tag) return; update(k<<1,t[k].tag);update(k<<1|1,t[k].tag); t[k].tag=0; } inline void build(int k,int l,int r) { t[k].l=l;t[k].r=r;t[k].tag=t[k].mx=0; if (l==r) return; int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); } inline void add(int k,int l,int r,int x) { if (t[k].l==l&&t[k].r==r){update(k,x);return;} int mid=(t[k].l+t[k].r)>>1; pushdown(k); if (r<=mid) add(k<<1,l,r,x); else if (l>mid) add(k<<1|1,l,r,x); else {add(k<<1,l,mid,x);add(k<<1|1,mid+1,r,x);} pushup(k); } inline void solveadd(int x,int f) { while (belong[x]!=belong[f]) { add(1,pos[belong[x]],pos[x],1); x=fa[belong[x]][0]; } add(1,pos[f],pos[x],1); } int main() { n=read();m=read(); F(i,1,n-1){x=read();y=read();add_edge(x,y);} dfs1(1); dfs2(1,1); build(1,1,n); F(i,1,m) { x=read();y=read(); tmp=lca(x,y); solveadd(x,tmp);solveadd(y,tmp); add(1,pos[tmp],pos[tmp],-1); } printf("%d\n",t[1].mx); return 0; }
上一篇: asp获取客户端浏览器的语言代码
下一篇: C++多线程编程