传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2561

考虑做mst的时候,什么时候这条边不在这棵mst上呢? 就是比他小的权值的边讲这条边的两边并进了一个联通块里面,那么对于所有的小于所求边的权值的边建一个图,然后求一个最小割使得U, V 不联通,一共做两遍加起来就是答案

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long ll;
const ll maxn = 210000; struct node {
ll val, size, dis, lazy;
node *l, *r;
}e[maxn * 8]; ll ne = 0; void swap(node *&a, node* &b) {
node* t = a; a = b, b = t;
} void test(node* x) {
if(x) {
cout << x-> val <<" "<< x-> size << endl;
test(x-> l), test(x-> r);
}
} void update(node *x) {
x-> size = 1;
if(x-> l) x-> size += x-> l-> size;
if(x-> r) x-> size += x-> r-> size;
} void pushdown(node* x) {
if(!x || !x-> lazy) return;
if(x-> l) x-> l-> val += x-> lazy, x-> l-> lazy += x-> lazy;
if(x-> r) x-> r-> val += x-> lazy, x-> r-> lazy += x-> lazy;
x-> lazy = 0;
} node* merge(node* a, node *b) {
if(!a) return b;
if(!b) return a;
pushdown(a), pushdown(b);
if(a-> val < b-> val) swap(a, b);
a-> r = merge(a-> r, b);
ll dl = a-> l ? a-> l-> size : -1;
ll dr = a-> r ? a-> r-> size : -1;
if(dl < dr) swap(a-> l, a-> r);
a-> dis = a-> r ? a-> r-> dis + 1 : 0;
update(a);
return a;
} void pop(node* &x) {
pushdown(x);
x = merge(x-> l, x-> r);
} ll n, m; struct edge {
ll t, d;
edge* next;
}se[maxn * 2], *head[maxn]; ll oe = 0; void addedge(ll f, ll t, ll d) {
se[oe].t = t, se[oe].d = d, se[oe].next = head[f], head[f] = se + oe ++;
} ll int_get() {
ll x = 0; char c = (char)getchar(); bool f = 0;
while(!isdigit(c)) {
if(c == '-') f = 1;
c = (char)getchar();
}
while(isdigit(c)) {
x = x * 10 + (int)(c - '0');
c = (char)getchar();
}
if(f) x= -x;
return x;
} void read() {
n = int_get(); m = int_get();
for(ll i = 2; i <= n; ++ i) {
ll f, w;
f = int_get(), w = int_get();
addedge(i, f, w), addedge(f, i, w);
}
} ll s[maxn], top = 0; node* rt[maxn];
ll h[maxn]; void dfs(ll x, ll fa) {
h[x] = h[fa] + 1;
for(edge* p = head[x]; p; p = p-> next) {
if(p-> t != fa) dfs(p-> t, x);
}
s[++ top] = x;
} ll ans[maxn]; void sov() {
dfs(1, 0);
for(ll j = 1; j <= top; ++ j) {
ll i = s[j];
rt[i] = e + ne ++; rt[i]-> dis = 0; rt[i]-> val = 0; rt[i]-> size = 1;
for(edge* p = head[i]; p; p = p-> next) {
if(h[p-> t] > h[i]) {
if(rt[p-> t]) rt[p-> t]-> lazy += p-> d, rt[p-> t]-> val += p-> d;
rt[i] = merge(rt[i], rt[p-> t]);
}
}
while(rt[i] && rt[i]-> val > m) pop(rt[i]);
ans[i] += rt[i] ? rt[i]-> size : 0;
}
for(ll i = 1; i <= n; ++ i) printf("%lld\n", ans[i]);
} int main() {
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
read(), sov();
return 0;
}

bzoj 2561的更多相关文章

  1. BZOJ 2561 最小生成树 | 网络流 最小割

    链接 BZOJ 2561 题解 用Kruskal算法的思路来考虑,边(u, v, L)可能出现在最小生成树上,就是说对于所有边权小于L的边,u和v不能连通,即求最小割: 对于最大生成树的情况也一样.容 ...

  2. BZOJ 2561 最小生成树(最大流)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2561 题意:给定一个边带正权的连通无向图G= (V,E),其中N=|V|,M=|E|,N ...

  3. BZOJ 2561: 最小生成树(最小割)

    U,V能在最小(大)生成树上,当且仅当权值比它小(大)的边无法连通U,V. 两次最小割就OK了. --------------------------------------------------- ...

  4. bzoj 2561: 最小生成树

    #include<cstdio> #include<iostream> #include<cstring> #define M 100009 #define inf ...

  5. BZOJ 2561: 最小生成树【最小割/最大流】

    Description 给定一个边带正权的连通无向图G=(V,E),其中N=|V|,M=|E|,N个点从1到N依次编号,给定三个正整数u,v,和L (u≠v),假设现在加入一条边权为L的边(u,v), ...

  6. bzoj 2561: 最小生成树【最小割】

    看错题了以为多组询问吓得不行-- 其实还挺好想的,就是数据范围一点都不网络流.把U作为s,V作为t,以最小生成树为例,(U,V,L)要在最小生成树上,就要求所有边权比L小的边不能连通(U,V)所在的联 ...

  7. 【BZOJ】【2561】最小生成树

    网络流/最小割 对于所有小于L的边求一个割使得U,V不连通,这样就可以保证L可能在最小生成树里. 最大生成树同理. 答案累加一下即可.(Orz Hzwer) (我一开始怎么会sb地去想到一起求呢……) ...

  8. 8月清北学堂培训 Day5

    今天是杨思祺老师的讲授~ 最短路练习题: POJ 1125 Stockbroker Grapevine 有 N 个股票经济人可以互相传递消息,他们之间存在一些单向的通信路径.现在有一个消息要由某个人开 ...

  9. DP&图论 DAY 5 上午

    DP&图论  DAY 5  上午 POJ 1125 Stockbroker Grapevine 有 N 个股票经济人可以互相传递消息,他们之间存在一些单向的通信路径.现在有一个消息要由某个人开 ...

随机推荐

  1. Android processDebugManifest 异常

    1.使用 gradlew processDebugManifest --stacktrace 进行排查; 2.异常: processDebugManifest (Thread[Execution wo ...

  2. 基于c语言数据结构+严蔚敏——线性表章节源码,利用Codeblocks编译通过

    白天没屌事,那我们就来玩玩线性表的实现吧,快要失业了,没饭吃了咋整哦 题目描述假设利用两个线性表LA和LB分别表示两个集合A和B(即:线性表中的数据元素即为集合中的成员),现要求一个新的集合A=A∪B ...

  3. kubernetes批量删除pod

    监控页面出现看到有运行失败的pod 1) 查看有哪些不运行的podcustom-metrics-apiserver日志占满空间被驱逐 [root@hadoop03 ~]# kubectl get po ...

  4. CDN(Content Delivery Network)内容分发网络

    CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在网络各处放置节 ...

  5. Python(二)

    函数Python的函数支持递归.默认参数值.可变参数,但不支持函数重载.为了增强代码的可读性,可以在函数后书写“文档字符串”(Documentation Strings,或者简称docstrings) ...

  6. 如何做LR自动关联和手动关联?

    一.什么时候需要关联   1.关联的含义        关联的含义A(correlation):在脚本回放过程中,客户端发出请求,通过关联函数所定义的左右边界值(也就是关联规则),在服务器所响应的内容 ...

  7. python random模块随机取list中的某个值

    import random from random import randint ''' random.randint()随机生一个整数int类型,可以指定这个整数的范围,同样有上限和下限值,pyth ...

  8. PHP错误检测

    开发的时候,我们有时候需要打开错误信息.这时候,可以在php文件里设置:ini_set('display_errors','on');error_reporting(E_ALL); 不过有时候我们及时 ...

  9. thymeleaf数组下标

    <tr th:if="${exercisers != null}"th:each="exerciser:${exercisers}"> <td ...

  10. git使用记录一:配置账户信息

    配置的级别 git config --gloabal 针对当前用户下所有的项目 设置 git config --local 针对当前工作区的项目来进行设置 git config --system 针对 ...