// 例题:洛谷P3128 https://www.luogu.org/problem/P3128
题目描述
Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls in his barn (2 \leq N \leq 50,0002≤N≤50,000), conveniently numbered 1 \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 KK pairs of stalls (1 \leq K \leq 100,0001≤K≤100,000). For the iith such pair, you are told two stalls s_isi and t_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 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 s_isi to t_iti, then it counts as being pumped through the endpoint stalls s_isi and
t_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 NN and KK.
The next N-1N−1 lines each contain two integers xx and yy (x \ne yx≠y) describing a pipe
between stalls xx and yy.
The next KK lines each contain two integers ss and tt 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
// 何为差分&&为什么要用差分 见博客:https://www.zybuluo.com/Junlier/note/1232395
// 方便自己看的。。。所以丑点也没关系。。
//上图揭示了差分数组的下标两次变换为何取 s、lca 和 lca的father、e
// 这里用的是倍增法求LCA 再通过LCA确定每次更改差分数组的两个下标
// 详解见博客https://www.cnblogs.com/cjoierljl/p/8728215.html
1 #include2 #include 3 #include 4 using namespace std; 5 const int MAXN = 5e4+5; 6 7 inline int read() { 8 char c = getchar(); int s = 0; 9 while(c < '0' || c > '9') c = getchar(); 10 while(c >= '0' && c <= '9') s = s*10 + c - '0', c = getchar(); 11 return s; 12 } 13 14 struct node { 15 int v, next; 16 } edge[2*MAXN]; 17 int num = 1; 18 19 int n, m; 20 int head[2*MAXN]; 21 int deep[MAXN]; 22 int f[MAXN][21]; 23 int power[MAXN]; // 差分数组 24 25 inline void add_edge(int x, int y) { 26 edge[num].v = y; 27 edge[num].next = head[x]; 28 head[x] = num++; 29 } 30 31 void dfs(int cur) { 32 for(int i = head[cur]; i != -1; i = edge[i].next) { 33 if(deep[edge[i].v] == 0) { 34 deep[edge[i].v] = deep[cur] + 1; 35 f[edge[i].v][0] = cur; 36 dfs(edge[i].v); 37 } 38 } 39 } 40 41 void PRE() { 42 for(int j = 1; j <= 19; ++j) { 43 for(int i = 1; i <= n; ++i) { 44 f[i][j] = f[f[i][j-1]][j-1]; 45 } 46 } 47 } 48 49 inline int LCA(int x, int y) { 50 if(deep[x] < deep[y]) swap(x, y); 51 for(int i = 19; i >= 0; --i) { 52 if(deep[f[x][i]] >= deep[y]) { 53 x = f[x][i]; 54 } 55 } 56 if(x == y) return x; 57 for(int i = 19; i >= 0; --i) { 58 if(f[x][i] != f[y][i]) { 59 x = f[x][i]; 60 y = f[y][i]; 61 } 62 } 63 return f[x][0]; 64 } 65 66 int ans = 0; 67 void sum(int cur) { 68 for(int i = head[cur]; i != -1; i = edge[i].next) { 69 if(edge[i].v != f[cur][0]) { 70 sum(edge[i].v); 71 power[cur] += power[edge[i].v]; 72 } 73 } 74 ans = max(ans, power[cur]); 75 } 76 77 int main() { 78 memset(head, -1, sizeof(head)); 79 memset(deep, 0, sizeof(deep)); 80 memset(f, 0, sizeof(f)); 81 memset(power, 0, sizeof(power)); 82 n = read(), m = read(); 83 int s, e; 84 for(int i = 0; i != n-1; ++i) { 85 s = read(), e = read(); 86 add_edge(s, e); 87 add_edge(e, s); 88 } 89 deep[1] = 1; 90 dfs(1); 91 PRE(); 92 for(int i = 0; i != m; ++i) { 93 s = read(), e = read(); 94 int lca = LCA(s, e); 95 power[s]++, power[lca]--; 96 power[e]++, power[f[lca][0]]--; 97 }
98 sum(1); 100 printf("%d\n", ans); 101 return 0; 102 }
// ---------------分割--------------
// poj 3417http://poj.org/problem?id=3417
1 #include2 #include 3 #include 4 using namespace std; 5 6 const int MAXN = 1e5+5; 7 8 inline int read() { 9 char c = getchar(); int s = 0; 10 while(c < '0' || c > '9') c = getchar(); 11 while(c >= '0' && c <= '9') s = s*10 + c-'0', c = getchar(); 12 return s; 13 } 14 15 struct node { 16 int v, next; 17 } edge[2*MAXN]; 18 int num; 19 20 int n, m; 21 int head[2*MAXN]; 22 int deep[MAXN]; 23 int f[MAXN][21]; 24 int power[MAXN]; 25 26 inline void add_edge(int x, int y) { 27 edge[num].v = y; 28 edge[num].next = head[x]; 29 head[x] = num++; 30 } 31 32 void dfs(int cur) { 33 for(int i = head[cur]; i != -1; i = edge[i].next) { 34 if(deep[edge[i].v] == 0) { 35 deep[edge[i].v] = deep[cur] + 1; 36 f[edge[i].v][0] = cur; 37 dfs(edge[i].v); 38 } 39 } 40 } 41 42 void PRE() { 43 for(int j = 1; j <= 19; ++j) { 44 for(int i = 1; i <= n; ++i) { 45 f[i][j] = f[f[i][j-1]][j-1]; 46 } 47 } 48 } 49 50 inline int LCA(int x, int y) { 51 if(deep[x] < deep[y]) swap(x, y); 52 for(int i = 19; i >= 0; --i) { 53 if(deep[f[x][i]] >= deep[y]) { 54 x = f[x][i]; 55 } 56 } 57 if(x == y) return x; 58 for(int i = 19; i >= 0; --i) { 59 if(f[x][i] != f[y][i]) { 60 x = f[x][i]; 61 y = f[y][i]; 62 } 63 } 64 return f[x][0]; 65 } 66 67 void SUM(int cur) { 68 for(int i = head[cur]; i != -1; i = edge[i].next) { 69 if(edge[i].v != f[cur][0]) { 70 SUM(edge[i].v); 71 power[cur] += power[edge[i].v]; 72 } 73 } 74 } 75 76 int main() { 77 while(scanf("%d%d", &n, &m) == 2) { 78 num = 1; 79 memset(head, -1, sizeof(head)); 80 memset(deep, 0, sizeof(deep)); 81 memset(f, 0, sizeof(f)); 82 memset(power, 0, sizeof(power)); 83 84 int s, e; 85 for(int i = 0; i != n-1; ++i) { 86 s = read(), e = read(); 87 add_edge(s, e); 88 add_edge(e, s); 89 } 90 deep[1] = 1; 91 dfs(1); 92 PRE(); 93 for(int i = 0; i != m; ++i) { 94 s = read(), e = read(); 95 int lca = LCA(s, e); 96 power[s]++, power[lca] -= 2;// 边 97 power[e]++; 98 } 99 100 SUM(1); 101 int a0 = 0, a1 = 0; 102 for(int i = 2; i <= n; ++i) {// 1不用看 剪断的是边不是点 103 if(power[i] == 0) a0++; 104 if(power[i] == 1) a1++; 105 } 106 printf("%d\n", a0*m + a1); 107 } 108 return 0; 109 }