title

「BZOJ4390」[Usaco2015 dec] Max Flow - 树上差分 - LCA

->戳我进原题

[Usaco2015 dec]Max Flow


Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 576 Solved: 386


### 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 N and K.

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 s 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

思路

典型的树上差分,而且是点差分,记录 \(s\) 数组为差分数组,推算出将 \(s\) 节点与 \(t\) 节点的差分 \(+1\) ,把其 \(LCA\)\(LCA\) 父亲的差分 \(-1\) 即可,统计答案直接在 \(diff\) 函数里更新 \(maxn\) 就行。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#define rg register 
using namespace std;
inline int read(){
	rg char ch = getchar();
	rg int f = 0, x = 0;
	while(!isdigit(ch))	f |= (ch == '-'), ch = getchar();
	while( isdigit(ch))	x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return f? -x: x;
}
const int N = 50010;
const int deplen = 21;
int n, m, st[N][deplen + 1], dep[N], head[N], tot, s[N], maxn = -1;
struct edge{
	int nxt, to;
}e[N << 1];
inline void add(rg int u, rg int v){
	e[++tot].nxt = head[u];
	e[tot].to = v;
	head[u] = tot;
}
inline void dfs(rg int u, rg int fa){
	dep[u] = dep[fa] + 1;
	st[u][0] = fa;
	for(rg int i = 1; (1<<i) <= dep[u];++i)	st[u][i] = st[st[u][i-1]][i-1];
	for(rg int i = head[u]; i; i = e[i].nxt){
		int v = e[i].to;
		if(v != fa)	dfs(v, u);
	}
}
inline int lca(rg int u, rg int v){
	if(dep[u] > dep[v])	swap(u, v);
	for(rg int i = deplen; i >= 0; --i)
		if(dep[u] + (1 << i) <= dep[v])
			v = st[v][i];
	if(u == v)	return u;
	for(rg int i = deplen; i >= 0; --i)
		if(st[u][i] != st[v][i])
			u = st[u][i],
			v = st[v][i];
	return st[u][0];
}
inline void diff(rg int u, rg int fa){
	for(rg int i = head[u]; i; i = e[i].nxt){
		int v = e[i].to;
		if(v != fa){
			diff(v, u);
			s[u] += s[v];
		}
	}
	maxn = max(maxn, s[u]);
}
signed main(){
	n = read(), m = read();
	for(rg int i = 1; i < n; ++i){
		int u = read(), v = read();
		add(u, v), add(v, u);
	}
	dfs(1,0);
	for(rg int i = 1; i <= m; ++i){
		int l = read(), r = read();
		s[l]++, s[r]++, s[lca(l,r)]--, s[st[lca(l,r)][0]]--;
	}
	diff(1,0);
	printf("%d", maxn);
	return 0;
} 
posted @ 2018-09-25 20:57  Horrigue_JyowYang  阅读(98)  评论(0编辑  收藏  举报