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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #pragma GCC optimize(2) #include <iostream> #include <cstdio>
using LL = long long;
const int N = 2e5 + 10; const LL INF = 1e17; int h[N], e[N << 1], ne[N << 1], w[N << 1], idx; int n, k, added, delst, fw[N], fa[N]; int dfn[N], sz[N], nw[N], top[N], dep[N], son[N]; bool vis[N];
struct Sgt1 { struct Node { int l, r, id; LL lt, mx; } tr[N << 2];
void pushup(int x) { if (tr[x << 1].mx > tr[x << 1 | 1].mx) tr[x].mx = tr[x << 1].mx, tr[x].id = tr[x << 1].id; else tr[x].mx = tr[x << 1 | 1].mx, tr[x].id = tr[x << 1 | 1].id; }
void build(int x, int l, int r) { tr[x] = {l, r, l, 0, 0}; if (l == r) return; int mid = (l + r) >> 1; build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r); }
void update(int x, LL c) { tr[x].mx += c, tr[x].lt += c; }
void pushdown(int x) { if (!tr[x].lt) return; update(x << 1, tr[x].lt), update(x << 1 | 1, tr[x].lt); tr[x].lt = 0; }
void modify(int x, int l, int r, LL c) { if (tr[x].l > r || tr[x].r < l) return; if (tr[x].l >= l && tr[x].r <= r) return update(x, c); pushdown(x); modify(x << 1, l, r, c), modify(x << 1 | 1, l, r, c); pushup(x); } } seg1;
struct Sgt2 { struct Node { int l, r, mx, mn, lt; } tr[N << 2];
void pushup(int x) { tr[x].mx = std::max(tr[x << 1].mx, tr[x << 1 | 1].mx); tr[x].mn = std::min(tr[x << 1].mn, tr[x << 1 | 1].mn); }
void build(int x, int l, int r) { tr[x] = {l, r, 0, 0, 0}; if (l == r) return; int mid = (l + r) >> 1; build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r); }
void update(int x, int c) { tr[x].lt += c, tr[x].mx += c, tr[x].mn += c; }
void pushdown(int x) { if (!tr[x].lt) return; update(x << 1, tr[x].lt), update(x << 1 | 1, tr[x].lt); tr[x].lt = 0; }
void modify(int x, int l, int r, int c) { if (tr[x].l > r || tr[x].r < l) return; if (tr[x].l >= l && tr[x].r <= r && (tr[x].mn >= delst || tr[x].mx + c <= added)) return update(x, c); if (tr[x].l == tr[x].r) { int to = 0; if (tr[x].mn <= added) to --; if (tr[x].mn + c <= added) to ++; else if (tr[x].mn + c >= delst) to --; update(x, c); seg1.modify(1, tr[x].l, tr[x].l + sz[nw[tr[x].l]] - 1, to * fw[nw[tr[x].l]]); return; } pushdown(x); modify(x << 1, l, r, c), modify(x << 1 | 1, l, r, c); pushup(x); } } seg2;
template <class T> inline bool chkmax(T &x, T y) { return x < y ? x = y, 1 : 0; }
void add(int a, int b, int c) { e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++; }
void dfs1(int x, int fa = 0) { sz[x] = 1, ::fa[x] = fa, dep[x] = dep[fa] + 1; for (int i = h[x], v; ~i; i = ne[i]) { if ((v = e[i]) == fa) continue; fw[v] = w[i], dfs1(v, x); if (sz[v] > sz[son[x]]) son[x] = v; sz[x] += sz[v]; } }
void dfs2(int x, int tp = 1) { top[x] = tp, nw[dfn[x] = ++ *dfn] = x; if (!son[x]) return; dfs2(son[x], tp); for (int i = h[x], v; ~i; i = ne[i]) if ((v = e[i]) != fa[x] && v != son[x]) dfs2(v, v); }
void update(int x) { while (x) seg2.modify(1, dfn[top[x]], dfn[x], 1), x = fa[top[x]]; }
int main() { std::cin >> n >> k; for (int i = 1; i <= n; ++ i) h[i] = -1; 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); } dfs1(1), dfs2(1); added = k / 2 - 1, delst = (k + 1) / 2; seg1.build(1, 1, n), seg2.build(1, 1, n); for (int i = 1; i <= n; ++ i) seg1.modify(1, dfn[i], dfn[i] + sz[i] - 1, fw[i]);
LL res = 0; while (k --) { int cur = nw[seg1.tr[1].id]; res += seg1.tr[1].mx; seg1.modify(1, dfn[cur], dfn[cur], -INF), update(cur); } std::cout << res * 2 << '\n'; return 0; }
|