P4149 Race 点分治
这题细节巨多(不过也可能是因为我菜),贡献了半页的提交记录
点分治最容易出锅的地方就在于信息应该在统计完该子树之后再加入。
信息在到下一层分治时一定要清干净。
这题的思路基本和模板一样,把一条路径拆成两段,开一个桶记录
然后就是常数巨大的代码了
Code:
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <tuple>
#include <bitset>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inl inline
#define re register int
#define fa(x) t[x].fa
#define ls(x) t[x].child[0]
#define rs(x) t[x].child[1]
#define ll long long
const int inf = 0x3f3f3f3f;
#define lowbit(x) ((x) & (-x))
using namespace std;
template <class Read>
inl Read read() {
Read x = 0;
register bool w = 0;
register char c = getchar();
while (c > '9' || c < '0') {
if (c == '-') w = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return w ? -x : x;
}
struct edge {
int next, to, w;
}e[5000001];
int focus, min_siz, ncnt, k, n, d[2000001], siz[2000001], head[2000001], tot, st[2000001], top, rem[2000001], rtop, minn[10000001], ans;
bool bucket[10000001], vis[2000001];
inl void dfs1(int x, int fa) {
re max_sub = 0;
siz[x] = 1;
for (re i = head[x]; i; i = e[i].next) {
if (fa != e[i].to && !vis[e[i].to]) {
dfs1(e[i].to, x);
siz[x] += siz[e[i].to];
if (siz[e[i].to] > max_sub)max_sub = siz[e[i].to];
}
}
max_sub = max(max_sub, ncnt - siz[x]);
if (max_sub < min_siz) {
min_siz = max_sub;
focus = x;
}
}
inl void dfs2(int x, int fa, int now) {
siz[x] = 1;
for (re i = head[x]; i; i = e[i].next) {
if (e[i].to != fa && !vis[e[i].to]) {
d[e[i].to] = d[x] + e[i].w;
if (d[e[i].to] > k)d[e[i].to] = k + 1;
dfs2(e[i].to, x, now + 1);
siz[x] += siz[e[i].to];
}
}
}
inl void calc(int x, int fa, int now) {
if (d[x] <= k)st[++top] = d[x], rem[++rtop] = d[x];
if (d[x] <= k && bucket[k - d[x]]) {
minn[k] = min(minn[k], now + minn[k - d[x]]);
}
for (re i = head[x]; i; i = e[i].next) {
if (!vis[e[i].to] && e[i].to != fa) {
calc(e[i].to, x, now + 1);
}
}
}
inl void dfs3(int x, int fa, int now) {
minn[d[x]] = min(minn[d[x]], now);
for (re i = head[x]; i; i = e[i].next) {
if (!vis[e[i].to] && e[i].to != fa) {
dfs3(e[i].to, x, now + 1);
}
}
}
inl void divide(int x, int fa) {
min_siz = inf;
dfs1(x, fa);
x = focus, vis[x] = 1, bucket[0] = 1, minn[0] = 0, d[x] = 0, top = 0;
dfs2(x, fa, 0);
for (re i = head[x]; i; i = e[i].next) {
if (!vis[e[i].to]) {
calc(e[i].to, x, 1);
if (e[i].next) {
while (top) {
if (st[top] <= k)bucket[st[top]] = 1;
top--;
}
dfs3(e[i].to, x, 1);
}
}
}
ans = min(ans, minn[k]);
while (rtop) {
if (rem[rtop] <= k)bucket[rem[rtop]] = 0, minn[rem[rtop]] = inf;
rtop--;
}
for (re i = head[x]; i; i = e[i].next) {
if (!vis[e[i].to]) {
ncnt = siz[e[i].to];
divide(e[i].to, x);
}
}
}
inl void add(int x, int y, int w) {
e[++tot] = edge{ head[x],y,w }, head[x] = tot;
e[++tot] = edge{ head[y],x,w }, head[y] = tot;
}
signed main() {
memset(minn, 0x3f, sizeof(minn)), ans = inf;
ncnt = n = read<int>(), k = read<int>();
for (re i = 1, x, y, w; i < n; i++)x = read<int>() + 1, y = read<int>() + 1, w = read<int>(), add(x, y, w);
divide(1, 0);
printf("%d\n", ans == inf ? -1 : ans);
}