思路:
首先,我们两次bfs求出直径,并且记录路径.把路径上的点打上标记,然后把路径存入path数组中.注意到路径有长度限制s,我们可以用单调队列优化dp.现在我们考虑怎么求最小的最大路径.
最大路径的点可能在红色(除了直径外的向下的点),蓝色(直径上该点向上的点),紫色(直径下该点向下的点),红色的点可以预处理,蓝色和紫色的点可以在单调队列优化dp时用左右指针维护.
总的时间复杂度是O(n).
代码:
#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
#define double long double
#define ull unsigned long long
#define PII pair<int, int>
#define PDI pair<double, int>
#define PDD pair<double, double>
#define debug(a) cout << #a << " = " << a << endl
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end()
#define mem(x, y) memset((x), (y), sizeof(x))
#define lbt(x) (x & (-x))
#define SZ(x) ((x).size())
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int N = 2e6 + 10;
int n, ans = INF;
int h[N], v[N], w[N], to[N], tot = 1, d[N], pre[N], st, up, fad[N], path[N], m, maxd[N], q[N], hh, tt, curdis[N];
bool vis[N];
multiset<int> too[N];
void add(int a, int b, int c) {
w[++tot] = c, v[tot] = b, to[tot] = h[a], h[a] = tot;
}
PII bfs(int S) {
for (int i = 1; i <= n; ++i) d[i] = -1;
queue<int> q;
q.emplace(S), d[S] = 0;
while (q.size()) {
int x = q.front(); q.pop();
for (int i = h[x], y; i; i = to[i]) {
if (d[y = v[i]] != -1) continue;
d[y] = d[x] + w[i];
fad[y] = w[i];
pre[y] = i;
q.emplace(y);
}
}
int ans = 0, id;
for (int i = 1; i <= n; ++i)
if (d[i] > ans) {
ans = d[i];
id = i;
}
return {ans, id};
}
void update(int T) {
int x = T;
while (x != st) {
path[++m] = x;
vis[x] = 1;
int i = pre[x];
x = v[i ^ 1];
}
path[++m] = st;
vis[st] = 1;
}
void dfs(int x, int fa) {
int temp = 0;
for (int i = h[x], y; i; i = to[i]) {
if (vis[y = v[i]] || y == fa) continue;
dfs(y, x);
maxd[x] = max(maxd[x], maxd[y] + w[i]);
}
}
signed main() {
qio >> n >> up;
for (int i = 1; i < n; ++i) {
int a, b, c;
qio >> a >> b >> c;
add(a, b, c), add(b, a, c);
}
auto [len, ed] = bfs(st = bfs(1).second);
update(ed);
for (int i = 1; i <= m; ++i) dfs(path[i], -1);
hh = 1, tt = 0;
for (int i = 1, j = 1, toed = len, tost = 0; i <= m; toed -= fad[path[i]], ++i) {
curdis[path[i]] = toed;
while (hh <= tt && curdis[q[hh]] - up > curdis[path[i]]) ++hh;
while (j < i && len - toed - tost > up) tost += fad[path[j++]];
while (hh <= tt && maxd[q[tt]] < maxd[path[i]]) --tt;
q[++tt] = path[i];
ans = min(ans, max({tost, toed, maxd[q[hh]]}));
}
qio << ans << "\n";
}