传送门:HDU-4123
题意:有n个点组成一棵树编号为1~n,定义一个点的最远距离为从该点出发的最长简单路径,有m次询问:求最大区间长度,使得区间内所有点最远距离的最大值-最远距离的最小值<=k
题解:先2次dfs求出每个点的最远距离,再用尺取法+RMQ找出最长区间
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MX = 5e4 + 5;
const LL mod = 998244353;
struct Edge {
int v, w, nxt;
} E[MX * 2];
int head[MX], tot, n, m;
void add(int u, int v, int w) {
E[tot].v = v;
E[tot].w = w;
E[tot].nxt = head[u];
head[u] = tot++;
}
void init() {
for (int i = 1; i <= n; i++) head[i] = -1;
tot = 0;
}
int ma[MX], se[MX], id1[MX], id2[MX];
void dfs(int u, int fa) {
ma[u] = se[u] = id1[u] = id2[u] = 0;
for (int i = head[u]; ~i; i = E[i].nxt) {
int v = E[i].v, w = E[i].w;
if (v == fa) continue;
dfs(v, u);
if (ma[v] + w > ma[u]) {
se[u] = ma[u];
id2[u] = id1[u];
ma[u] = ma[v] + w;
id1[u] = v;
} else if (ma[v] + w > se[u]) {
se[u] = ma[v] + w;
id2[u] = v;
}
}
}
void DFS(int u, int fa) {
for (int i = head[u]; ~i; i = E[i].nxt) {
int v = E[i].v, w = E[i].w;
if (v == fa) continue;
if (id1[u] != v) {
if (ma[u] + w > ma[v]) {
se[v] = ma[v];
id2[v] = id1[v];
ma[v] = ma[u] + w;
id1[v] = u;
} else if (ma[u] + w > se[v]) {
se[v] = ma[u] + w;
id2[v] = u;
}
} else if (id2[u] != v) {
if (se[u] + w > ma[v]) {
se[v] = ma[v];
id2[v] = id1[v];
ma[v] = se[u] + w;
id1[v] = u;
} else if (se[u] + w > se[v]) {
se[v] = se[u] + w;
id2[v] = u;
}
}
DFS(v, u);
}
}
int f[MX][30][2];
void ST() {
for (int i = 1; i <= n; i++) f[i][0][0] = f[i][0][1] = ma[i];
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
f[i][j][0] = max(f[i][j - 1][0], f[i + (1 << (j - 1))][j - 1][0]);
f[i][j][1] = min(f[i][j - 1][1], f[i + (1 << (j - 1))][j - 1][1]);
}
}
}
int RMQ(int l, int r) {
int j = 0;
while ((1 << (j + 1)) <= (r - l + 1)) j++;
int ans1 = max(f[l][j][0], f[r - (1 << j) + 1][j][0]);
int ans2 = min(f[l][j][1], f[r - (1 << j) + 1][j][1]);
return ans1 - ans2;
}
int main() {
int T;
//freopen("in.txt", "r", stdin);
while (scanf("%d%d", &n, &m), n || m) {
init();
for (int i = 1, u, v, w; i < n; i++) {
scanf("%d%d%d", &u, &v, &w);
add(u, v, w); add(v, u, w);
}
dfs(1, -1);
DFS(1, -1);
ST();
while (m--) {
int k, ans = 0;
scanf("%d", &k);
for (int s = 1, t = 1; t <= n; t++) {
while (s < t && RMQ(s, t) > k) s++;
ans = max(ans, t - s + 1);
}
printf("%d\n", ans);
}
}
return 0;
}