4390: [Usaco2015 dec]Max Flow
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 281 Solved: 178
[ 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
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
#include<cstdio>
#include<cstring>
int read()
{
int ans=0,f=1;char t=getchar();
while(t<'0'||t>'9') f=(t=='-'?-1:1),t=getchar();
while(t>='0'&&t<='9') ans=ans*10+t-'0',t=getchar();
return ans*f;
}
const int N=1e5+7,M=N<<1;
struct node
{
int to,next;
}e[M];
int first[N],cnt;int dep[N];
int qu[M],log[M],f[M][23];
inline void insert(int u,int v)
{
e[++cnt]=(node){v,first[u]};first[u]=cnt;
e[++cnt]=(node){u,first[v]};first[v]=cnt;
}
inline void swap(int &a,int &b)
{
int tmp=a;a=b,b=tmp;
}
inline int minn(int a,int b)
{
if(dep[a]<dep[b]) return a;
return b;
}
int tar[N],lg[M],visit[N];
inline int lca(int l,int r)
{
l=tar[l],r=tar[r];
if(l>r) swap(l,r);
int t=lg[r-l+1];
return minn(f[l][t],f[r-(1<<t)+1][t]);
}
int fa[N];
void dfs(int x)
{
visit[x]=1;qu[++cnt]=x;tar[x]=cnt;
for(int k=first[x];k;k=e[k].next)
if(!visit[e[k].to])
{
dep[e[k].to]=dep[x]+1;
fa[e[k].to]=x;
dfs(e[k].to);
qu[++cnt]=x;
}
}
inline void init()
{
lg[1]=0;
for(int i=2;i<=cnt;i++) lg[i]=lg[i>>1]+1;
for(int i=1;i<=cnt;i++) f[i][0]=qu[i];
for(int j=1;j<=22;j++)
for(int i=1;i<=cnt-( 1<<(j -1) );i++)
f[i][j]=minn(f[i][j-1],f[ i+( 1<< (j-1) )][ j-1 ]);
}
int tr[N];
int ans[N];
void dfs2(int x)
{
visit[x]=1;ans[x]=tr[x];
for(int k=first[x];k;k=e[k].next)
if(!visit[e[k].to])
{
dfs2(e[k].to);
ans[x]+=ans[e[k].to];
}
}
#define test(a) printf("std::%d\n",a)
int main()
{
int n,k;
n=read(),k=read();int u,v;
for(int i=1;i<n;i++) u=read(),v=read(),insert(u,v);
cnt=0;
dfs(1);
init();
for(int i=1;i<=k;i++)
{
u=read(),v=read();
tr[u]++,tr[v]++;
int lc=lca(u,v);
tr[lc]--,tr[fa[lc]]--;
}
memset(visit,0,sizeof(visit));
dfs2(1);
int max=0;
for(int i=1;i<=n;i++) if(ans[i]>max) max=ans[i];
printf("%d\n",max);
return 0;
}