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

#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. 利用python爬取王者荣耀英雄皮肤图片

    前两天看到同学用python爬下来LOL的皮肤图片,感觉挺有趣的,我也想试试,于是决定来爬一爬王者荣耀的英雄和皮肤图片. 首先,我们找到王者的官网http://pvp.qq.com/web201605 ...

  2. git push error. ! [rejected] master -> master (non-fast-forward)

    错误提示: Cheetah@xxxx MINGW64 /e/Projs/enft/data/cv_key_frame (master) $ git push To github.com:Anthony ...

  3. Java 之 网络编程基础

    一.软件结构 C/S 结构:全称为 Client/Server 结构,是指客户端和服务器结构.常见的程序有微信,QQ,迅雷等软件. B/S 结构:全称 Brower/Server 结构,是指浏览器和服 ...

  4. 编写一个stm32 svc关中断函数

    做到了让stm32触发svc中断并传递进去参数然后切换到handler模式并修改特殊寄存器的值,从而达到关中断,但是其实这个程序直接就是特权级,故不进入handler模式也可以修改特殊寄存器..... ...

  5. Linux 终端下的颜色

    Linux 终端下颜色的输出 在命令行下也能产生五颜六色的字体和图案,只需要加上一些颜色代码,例如 echo -e "\033[41;36m 红底绿字\033[0m" 其中41的位 ...

  6. C++——new & delete

    C++ new Complex类 String类 C++ delete Comlex类 String类 array new 一定要搭配array delete VC架构下new内存分配演绎 Linux ...

  7. 数据库索引碎片——数据库sql

    文章:检测和整理索引碎片 文章:[笔记整理]SQL Server 索引碎片 和 重建索引 文章介绍了检查表的索引碎片百分比 文章:[小问题笔记(八)] 常用SQL(读字段名,改字段名,打印影响行数,添 ...

  8. 能ping通Linux但是ssh连不上问题解决方法

    问题:能ping通Linux服务器 但是ssh连不上  <Linux redhat AS4 版本> 解决方法这个问题花了我20分钟去查资料,网上写的解决方法也是五花八门,不过,总算解决了, ...

  9. 你应该知道的16个linux命令

    Linux常用命令很多比如cd.ls.pwd.du.df.tail.head.yum.mv.touch.mkdir.cp.tar等,根据工作内容不同,经常使用的命令也不同,比如运维人员使用tail.h ...

  10. Codeforces Round #336 (Div. 2) D. Zuma(区间DP)

    题目链接:https://codeforces.com/contest/608/problem/D 题意:给出n个宝石的颜色ci,现在有一个操作,就是子串的颜色是回文串的区间可以通过一次操作消去,问最 ...