「BZOJ2870」最长道路

Description

给定一棵NN个点的树,求树上一条链使得链的长度乘链上所有点中的最小权值所得的积最大。
其中链长度定义为链上点的个数。

Input

第一行NN
第二行NN个数分别表示1N1\to N的点权viv_i
接下来N1N-1行每行两个数xxyy,表示一条连接xxyy的边

Output

一个数,表示最大的痛苦程度。

Sample Input

1
2
3
4
3
5 3 5
1 2
1 3

Sample Output

1
10

样例解释

选择从1133的路径,痛苦程度为min(5,5)×2=10\min(5,5)\times2=10

HINT

100%100\%的数据n50000n\leq 50000
其中有20%20\%的数据树退化成一条链
所有数据点权65536\leq 65536
建议答案使用6464位整型

Solution

边分治的模板题。
边分治形同点分治,点分治找重心,好像边分治也是找重心不过是把树分成了两部分,每一次平均分。
操作基本同点分治,主要看代码就能掌握了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<map>
#include<bitset>
#define mk make_pair
#define fi first
#define nd second
#define pii pair<int,int>
#define pb push_back
#define sqr(x) ((x)*(x))
using namespace std;
typedef long long ll;
inline ll read() {ll x = 0; char ch = getchar(), w = 1;while(ch < '0' || ch > '9') {if(ch == '-') w = -1;
ch = getchar();}while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();}return x * w;}
void write(ll x) {if(x < 0) putchar('-'), x = -x;if(x > 9) write(x / 10);putchar(x % 10 + '0');}
inline void writeln(ll x) {write(x);puts("");}
const int N = 110000;
int n;
int v[N];
struct Edge {
int u, v, nxt;
}e[N * 2];
int head[N], en;
void addedge(int x, int y) {
e[++en].u = x, e[en].v = y, e[en].nxt = head[x], head[x] = en;
}
int root, vr;
int siz[N], sum;
bool vis[N * 2];
void find(int x, int F) {
siz[x] = 1;
for(int i = head[x]; i;i = e[i].nxt) if(!vis[i]) {
int y = e[i].v;
if(y == F) continue;
find(y, x);
siz[x] += siz[y];
int tmp = max(siz[y], sum - siz[y]);
if(tmp < vr) {
vr = tmp;
root = i;
}
}
}
struct Te {
int d, v;
}a[N], b[N];
bool cmp(Te _x, Te _y) {
return _x.v > _y.v;
}
void dfs(int x, int dep, Te *t, int val, int &tot, int F) {
t[++tot] = (Te){dep, val};
for(int i = head[x]; i; i = e[i].nxt) if(!vis[i] && e[i].v != F) {
dfs(e[i].v, dep + 1, t, min(val, v[e[i].v]), tot, x);
}
}
ll ans = 0;
void show(Te *t, int top) {
for(int i = 1; i <= top; ++i) {
printf("%d: %d %d\n", i, t[i].d, t[i].v);
}
}
void work(int x, int Siz) {
root = 0;
sum = Siz;
vr = 1e9;
find(x, 0);
x = root;
if(!x) return;
vis[root ^ 1] = vis[root] = 1;
int t1 = 0, t2 = 0;
dfs(e[x].u, 0, a, v[e[x].u], t1, 0);
dfs(e[x].v, 0, b, v[e[x].v], t2, 0);
sort(a + 1, a + 1 + t1, cmp);
sort(b + 1, b + 1 + t2, cmp);
for(int i = 1, j = 1, mxl = 0; i <= t1; ++i) {
while(j <= t2 && b[j].v >= a[i].v) mxl = max(mxl, b[j].d), ++j;
ans = max(ans, (ll)(mxl + a[i].d + 2 - (j == 1)) * a[i].v);
}
for(int i = 1, j = 1, mxl = 0; i <= t2; ++i) {
while(j <= t1 && a[j].v >= b[i].v) mxl = max(mxl, a[j].d), ++j;
ans = max(ans, (ll)(mxl + b[i].d + 2 - (j == 1)) * b[i].v);
}
int tmp = siz[e[x].u];
work(e[x].u, tmp);
work(e[x].v, Siz - tmp);
}
int main() {
en = 1;
n = read();
for(int i = 1; i <= n; ++i) v[i] = read();
for(int i = 1; i < n; ++i) {
int x = read(), y = read();
addedge(x, y);
addedge(y, x);
}
work(1, n);
writeln(ans);
return 0;
}