如果没有边数限制就是裸的淀粉质,如果有了加上一个树状数组记边数就行了。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define re register
#define ll long long
inline int gi(){
int f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
const int N=400010;
int n,w,front[N],cnt,rt,siz[N],vis[N],tot,f[N];
struct node{int to,nxt,w;}e[N];
void Add(int u,int v,int w){e[++cnt]=(node){v,front[u],w};front[u]=cnt;}
void getroot(int u,int ff){
siz[u]=1;f[u]=0;
for(int i=front[u];i;i=e[i].nxt){
int v=e[i].to;if(vis[v] || v==ff)continue;
getroot(v,u);
siz[u]+=siz[v];
f[u]=max(f[u],siz[v]);
}
f[u]=max(f[u],tot-siz[u]);
if(f[u]<f[rt])rt=u;
}
ll ans;int dis[N],dep[N];
struct thing{
int dis,dep;
bool operator<(const thing &b)const{return dis<b.dis;}
}p[N];
void getdis(int u,int ff){
p[++cnt]=(thing){dis[u],dep[u]};
for(int i=front[u];i;i=e[i].nxt){
int v=e[i].to;if(v==ff || vis[v])continue;
dep[v]=dep[u]+1;dis[v]=dis[u]+e[i].w;
getdis(v,u);
}
}
int c[N],L;
int lowbit(int x){return x&(-x);}
void Add(int x,int d){while(x<=n+1){c[x]+=d;x+=lowbit(x);}}
int query(int x){if(x<=0)return 0;int ret=0;while(x){ret+=c[x];x-=lowbit(x);}return ret;}
ll calc(int u,int ds,int dp){
dis[u]=ds;dep[u]=dp;cnt=0;
getdis(u,u);
sort(p+1,p+cnt+1);
for(int i=1;i<=cnt;i++)Add(p[i].dep+1,1);
int l=1,r=cnt;ll ret=0;
while(l<r){
if(p[l].dis+p[r].dis<=w){
Add(p[l].dep+1,-1);
ret+=query(L-p[l].dep+1);
l++;
}
else{
Add(p[r].dep+1,-1);
r--;
}
}
Add(p[l].dep+1,-1);
return ret;
}
void solve(int u){
ans+=calc(u,0,0);
vis[u]=1;
for(int i=front[u];i;i=e[i].nxt){
int v=e[i].to;if(vis[v])continue;
ans-=calc(v,e[i].w,1);
rt=0;tot=siz[v];
getroot(v,v);
solve(rt);
}
}
int main(){
n=gi();L=gi();w=gi();
for(int i=2;i<=n;i++){int fa=gi(),v=gi();Add(fa,i,v);Add(i,fa,v);}
tot=f[0]=n;
getroot(1,0);
solve(rt);
printf("%lld\n",ans);
return 0;
}

CF293E Close Vertice的更多相关文章

  1. cf293E Close Vertices(树分治+BIT)

    E. Close Vertices You've got a weighted tree, consisting of n vertices. Each edge has a non-negative ...

  2. CF293E Close Vertices 点分治+树状数组

    开始zz写了一个主席树,后来发现写个树状数组就行~ #include <cstdio> #include <vector> #include <algorithm> ...

  3. 纸上谈兵: 图 (graph)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 图(graph)是一种比较松散的数据结构.它有一些节点(vertice),在某些节 ...

  4. ICEM(2)—机翼翼稍网格绘制

    有时我们需要观察翼尖涡,这就需要将机翼全部被网格包围.但是网上比较多的教程都是机翼边缘即为网格边缘,机翼位于网格内部的不多.若是直接将网格拉伸,则会产生结构和非结构网格交错的情况.下面是绘制步骤 1. ...

  5. CF 407B Long Path[观察性质 DP]

    B. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  6. HDU 4358 Boring counting(莫队+DFS序+离散化)

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  7. hdu 4358 Boring counting dfs序+莫队+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  8. 纸上谈兵:图(graph)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 图(graph)是一种比较松散的数据结构.它有一些节点(vertice),在某些节 ...

  9. Shape comparison language

      形状比较语言, 九交模型 In this topic About shape comparison language Dimensionality Extensions to the CBM SC ...

随机推荐

  1. JavaScript 入门与进阶

    JavaScript 介绍 javascript 是运行在浏览器端的脚本语言,javascript 主要解决的是前端与用户交互的问题,包括使用交互 和 数据交互,javascript 是浏览器解释执行 ...

  2. 一行css代码搞定响应式布局

    在这篇文章中,我将教你如何使用 CSS Grid 来创建一个超酷的图像网格图,它将根据屏幕的宽度来改变列的数量.最精彩的地方在于:所有的响应特性被添加到了一行 css 代码中.这意味着我们不必将 HT ...

  3. 五、eureka客户端自动配置

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 前面的几篇文章中,我们从eureka Server端的角度看了看eureka的几个核心要 ...

  4. ReactNative报错null is not an object (evaluating '_rngesturehandlermodule.default.direction')

    程序报错: null is not an object (evaluating 'rngesturehandlermodule.default.direction') 解决: react-native ...

  5. switch语句中 参数的类型

    switch可作用于char byte short int switch可作用于char byte short int对应的包装类 switch不可作用于long double float boole ...

  6. springboot学习入门简易版二---springboot2.0项目创建

    2 springboot项目创建(5) 环境要求:jdk1.8+ 项目结构: 2.1创建maven工程 Group id :com.springbootdemo Artifact id: spring ...

  7. py网络编程学习笔记

    一.异常处理 异常就是程序运行时发生错误的信号(在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常,程序的运行也随之终止),在python中,错误触发的异常如下 而错误分为两种: 1 ...

  8. JAVA工程师必学技能,进阶&涨薪的推进器!这份实战教程请收下

    Netty 作为互联网中间件的基石,是 JAVA 工程师进阶为高级程序员必备的能力之一.也是目前是互联网中间件领域使用最广泛最核心的网络通信框架. Netty是一个高性能.异步事件驱动的NIO框架,它 ...

  9. arduino之16*16点阵庆祝祖国70周年

    之前电脑上存了很多自己写的关于arduino的有趣的小demo,因为重装了系统,不小心误删了所有的文件(气的半死),所以现在准备一有空就重写一下之前写过的东东,顺带再温习一次,这次总不能再删了吧,嘿嘿 ...

  10. Python Django开发遇到的坑(版本不匹配)

    这个问题 进入django 后台, 添加,修改都不可以,只有删除可以,那么百分之百是这个问题 对照一下,是你的django 版本低了还是 python版本高了,对照的话就没问题了 这个坑,弄了两天啊! ...