传送门
题目大意:
codeJan有一天脑洞大开,想到一个有趣的问题。给一个固定根为1号结点的树,定义一个子树的beauty是这个子树的根节点到所有这棵树上其他节点的距离和,叶子节点的beauty是0。定义一个子树的sub-beauty是这个子树的beauty值减去这个子树的某一个子树(不包括自身)的beauty值。显然一个子树的beauty值是唯一的,而sub-beauty值可以有很多个。codeJan想要知道所有子树的所有sub-beauty中不超过m的最大值
思路: 首先预处理出所有节点的beauty值, 然后可以注意到这个值一定是递减的, 也就是有序的, 所以每次更新一个节点时我们就二分找一下答案即可, 注意lower_bound要求序列时递增的, 所以在用的时候加一个参数 greater() 即可.
AC Code
const int maxn = 1e5+5;
int son[maxn];
ll d[maxn];
int n, m;
struct node {
int to, next, w;
}e[maxn<<1];
int cnt, head[maxn];
void add(int u, int v, int w) {
e[cnt] = node{v, head[u], w};
head[u] = cnt++;
}
vector<ll>ve;
ll ans;
void init() {
cnt = 0 ; Fill(d, 0); ans = -1;
Fill(head, -1); ve.clear();
}
int dfs1(int u, int fa) {
son[u] = 1;
for (int i = head[u]; ~i ; i = e[i].next) {
int to = e[i].to;
if (to == fa) continue;
son[u] += dfs1(to, u);
d[u] += 1ll*son[to]*e[i].w + d[to];
}
return son[u];
}
void dfs2(int u, int fa) {
ve.pb(d[u]);
for (int i = head[u]; ~i ; i = e[i].next) {
int to = e[i].to;
if (to == fa) continue;
int pos = lower_bound(ve.begin(), ve.end(), d[to] + m, greater<ll>() ) - ve.begin();
if (pos < sz(ve)) ans = max(ans, ve[pos] - d[to]);
dfs2(to, u);
}
ve.pop_back();
}
void solve()
{
scanf("%d%d", &n, &m); init();
for (int i = 1 ; i < n ; i ++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
if (m <= 0) {
printf("-1\n");
return ;
}
dfs1(1, -1); dfs2(1, -1);
printf("%lld\n", ans);
}