4390: [Usaco2015 dec]Max Flow
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 88 Solved: 55
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
Source
Platinum鸣谢Claris提供译文
本来是道链剖傻逼题。。
结果我TMpaint写错了。。
导致5A。。
我是渣渣。。
附上本蒟蒻的代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<climits>
using namespace std;
#define MAXN 50001
int n,m,cnt,sz,h[MAXN],father[MAXN][17],deep[MAXN],size[MAXN],pos[MAXN],belong[MAXN],delta[MAXN<<2];
bool vis[MAXN];
struct data
{
int to,next;
}edge[MAXN<<1];
struct kx
{
int value;
}node[MAXN<<2];
int read()
{
int w=0,c=1; char ch=getchar();
while (ch<'0' || ch>'9')
{
if (ch=='-') c=-1;
ch=getchar();
}
while (ch>='0' && ch<='9')
w=w*10+ch-'0',ch=getchar();
return w*c;
}
void add(int u,int v)
{
cnt++,edge[cnt].next=h[u],h[u]=cnt,edge[cnt].to=v;
cnt++,edge[cnt].next=h[v],h[v]=cnt,edge[cnt].to=u;
}
void dfs1(int x)
{
int i;
size[x]=1,vis[x]=true;
for (i=1;i<=16;i++)
{
if (deep[x]<(1<<i)) break;
father[x][i]=father[father[x][i-1]][i-1];
}
for (i=h[x];i;i=edge[i].next)
{
if (vis[edge[i].to]) continue;
deep[edge[i].to]=deep[x]+1,father[edge[i].to][0]=x;
dfs1(edge[i].to),size[x]+=size[edge[i].to];
}
}
void dfs2(int x,int chain)
{
int k=0,i;
sz++,pos[x]=sz,belong[x]=chain;
for (i=h[x];i;i=edge[i].next)
if (deep[edge[i].to]>deep[x] && size[edge[i].to]>size[k])
k=edge[i].to;
if (!k) return;
dfs2(k,chain);
for (i=h[x];i;i=edge[i].next)
if (deep[edge[i].to]>deep[x] && k!=edge[i].to)
dfs2(edge[i].to,edge[i].to);
}
void update(int s)
{
node[s].value=max(node[s*2].value,node[s*2+1].value);
}
void build(int s,int l,int r)
{
int mid=(l+r)/2;
if (l==r) return;
build(s*2,l,mid);
build(s*2+1,mid+1,r);
update(s);
}
void paint(int s,int z,int l,int r)
{
node[s].value+=z;
delta[s]+=z;
}
void pushdown(int s,int l,int r)
{
int mid=(l+r)/2;
paint(s*2,delta[s],l,mid);
paint(s*2+1,delta[s],mid+1,r);
delta[s]=0;
}
void insert(int s,int l,int r,int x,int y,int z)
{
int mid=(l+r)/2;
if (x<=l && y>=r)
{
paint(s,z,l,r);
return;
}
if (delta[s]) pushdown(s,l,r);
if (x<=mid) insert(s*2,l,mid,x,y,z);
if (y>mid) insert(s*2+1,mid+1,r,x,y,z);
update(s);
}
void solveinsert(int x,int y,int z)
{
for (;belong[x]!=belong[y];x=father[belong[x]][0])
{
if (deep[belong[x]]<deep[belong[y]]) swap(x,y);
insert(1,1,n,pos[belong[x]],pos[x],z);
}
if (deep[x]<deep[y]) swap(x,y);
insert(1,1,n,pos[y],pos[x],z);
}
int querymax(int s,int l,int r,int x,int y)
{
int mid=(l+r)/2,ans;
if (x<=l && y>=r) return node[s].value;
if (delta[s]) pushdown(s,l,r);
if (x<=mid) ans=querymax(s*2,l,mid,x,y);
else ans=-INT_MAX;
if (y>mid) ans=max(ans,querymax(s*2+1,mid+1,r,x,y));
update(s);
return ans;
}
int main()
{
int i,x,y;
n=read(),m=read();
for (i=1;i<n;i++)
x=read(),y=read(),add(x,y);
dfs1(1),dfs2(1,1),build(1,1,n);
for (i=1;i<=m;i++)
{
x=read(),y=read();
solveinsert(x,y,1);
}
printf("%d\n",querymax(1,1,n,1,n));
return 0;
}

本文讨论了如何在给定的树形结构中,通过一系列操作来更新每个节点的权重,并最终找出权重最大的节点。操作涉及在树中选择任意两点间的所有路径,将这些路径上的节点权重增加。通过实现DFS遍历和使用线段树进行区间更新与查询,解决了此问题。
307

被折叠的 条评论
为什么被折叠?



