Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 353 Solved: 236
[Submit][Status][Discuss]
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次操作完毕后权值最大的那个点的权值。
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.
An integer specifying the maximum amount of milk pumped through any stall in the barn.
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
9
数上差分,对于x,y 在x处+1,y处+1,lca(x,y)-1 fa[lca(x,y)]-1,就可以了。
1 #include2 #include 3 #include 4 #include 5 #include 6 7 #define N 50007 8 using namespace std; 9 inline int read() 10 { 11 int x=0,f=1;char ch=getchar(); 12 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 13 while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} 14 return x*f; 15 } 16 17 int n,m,ans; 18 int val[N],dep[N],fa[N][20]; 19 int cnt,hed[N],nxt[N<<1],rea[N<<1]; 20 21 void add(int u,int v) 22 { 23 nxt[++cnt]=hed[u]; 24 hed[u]=cnt; 25 rea[cnt]=v; 26 } 27 void add_two_way(int x,int y) 28 { 29 add(x,y); 30 add(y,x); 31 } 32 void dfs_init(int u,int par) 33 { 34 for (int i=1;(1<=0;j--) 49 if (dep[a]-(1< =dep[b]) a=fa[a][j]; 50 if (a==b) return a; 51 for (int j=i;j>=0;j--) 52 if (fa[a][j]!=fa[b][j]) a=fa[a][j],b=fa[b][j]; 53 return fa[a][0]; 54 } 55 void solve(int u) 56 { 57 for (int i=hed[u];i!=-1;i=nxt[i]) 58 { 59 int v=rea[i]; 60 if (v==fa[u][0]) continue; 61 solve(v),val[u]+=val[v]; 62 } 63 ans=max(val[u],ans); 64 } 65 int main() 66 { 67 memset(hed,-1,sizeof(hed)); 68 n=read(),m=read(); 69 for (int i=1;i