题目描述
Farmer John has installed a new system of N−1N-1N−1 pipes to transport milk between the NNN stalls in his barn (2≤N≤50,0002 \leq N \leq 50,0002≤N≤50,000 ), conveniently numbered 1…N1 \ldots N1…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 KKK pairs of stalls (1≤K≤100,0001 \leq K \leq 100,0001≤K≤100,000 ). For the iii th such pair, you are told two stalls sis_isi and tit_iti , 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 KKK 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 sis_isi to tit_iti , then it counts as being pumped through the endpoint stalls sis_isi and
tit_iti , as well as through every stall along the path between them.
FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N。所有隔间都被管道连通了。
FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少。
输入输出格式
输入格式:
The first line of the input contains NNN and KKK .
The next N−1N-1N−1 lines each contain two integers xxx and yyy (x≠yx \ne yx≠y ) describing a pipe
between stalls xxx and yyy .
The next KKK lines each contain two integers sss and ttt 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.
给出一棵树,50000个节点,100000次修改,每次给定s和t,把s到t的路径上的点权+1,问k次操作后最大点权。
1 //2018年2月14日13:03:17 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 const int N = 500001; 8 const int M = 500001; 9 10 int n, k; 11 12 int fir[N], nxt[M], to[M], w[N], edge_Num; 13 void addEdge(int x, int y){ 14 to[++edge_Num] = y; 15 nxt[edge_Num] = fir[x]; 16 fir[x] = edge_Num; 17 } 18 19 int deep[N], p[N][23], d[N][23]; 20 void dfs(int u){ 21 for(int i=1;i<=22;i++){ 22 if(deep[u] < (1<<i)) break; 23 p[u][i] = p[p[u][i-1]][i-1]; 24 } 25 for(int i=fir[u]; i; i=nxt[i]) 26 if(!deep[to[i]]){ 27 deep[to[i]] = deep[u] + 1; 28 p[to[i]][0] = u; 29 dfs(to[i]); 30 } 31 } 32 33 int lca(int u, int v){ 34 if(deep[u] < deep[v]) swap(u, v); 35 int c = deep[u] - deep[v]; 36 for(int i=0; i<=22; i++) 37 if((1<<i) & c) 38 u = p[u][i]; 39 if(u == v) return u; 40 for(int i=22; i>=0; i--) 41 if(p[u][i] != p[v][i]){ 42 u = p[u][i]; 43 v = p[v][i]; 44 } 45 return p[u][0]; 46 } 47 48 int maxx; 49 void DFS(int x, int fa){ 50 for(int i=fir[x]; i; i=nxt[i]){ 51 if(to[i] != fa){ 52 DFS(to[i], x); 53 w[x] += w[to[i]]; 54 } 55 } 56 } 57 58 int main(){ 59 scanf("%d%d", &n, &k); 60 for(int i=1;i<n;i++){ 61 int x, y; 62 scanf("%d%d", &x, &y); 63 addEdge(x, y); 64 addEdge(y, x); 65 } 66 p[1][0] = 1; 67 deep[1] = 1; 68 dfs(1); 69 70 for(int i=1;i<=k;i++){ 71 int x, y; 72 scanf("%d%d", &x, &y); 73 w[x]++; 74 w[y]++; 75 int fff = lca(x, y); 76 w[fff]--; 77 w[p[fff][0]]--; 78 } 79 DFS(1, 1); 80 for(int i=1;i<=n;i++) 81 if(w[i] > maxx) 82 maxx = w[i]; 83 cout << maxx << endl; 84 85 return 0; 86 }
来源:https://www.cnblogs.com/sineagle/p/8448250.html