CF293E Close Vertice
如果没有边数限制就是裸的淀粉质,如果有了加上一个树状数组记边数就行了。
#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的更多相关文章
- cf293E Close Vertices(树分治+BIT)
E. Close Vertices You've got a weighted tree, consisting of n vertices. Each edge has a non-negative ...
- CF293E Close Vertices 点分治+树状数组
开始zz写了一个主席树,后来发现写个树状数组就行~ #include <cstdio> #include <vector> #include <algorithm> ...
- 纸上谈兵: 图 (graph)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 图(graph)是一种比较松散的数据结构.它有一些节点(vertice),在某些节 ...
- ICEM(2)—机翼翼稍网格绘制
有时我们需要观察翼尖涡,这就需要将机翼全部被网格包围.但是网上比较多的教程都是机翼边缘即为网格边缘,机翼位于网格内部的不多.若是直接将网格拉伸,则会产生结构和非结构网格交错的情况.下面是绘制步骤 1. ...
- CF 407B Long Path[观察性质 DP]
B. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- hdu 4358 Boring counting dfs序+莫队+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- 纸上谈兵:图(graph)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 图(graph)是一种比较松散的数据结构.它有一些节点(vertice),在某些节 ...
- Shape comparison language
形状比较语言, 九交模型 In this topic About shape comparison language Dimensionality Extensions to the CBM SC ...
随机推荐
- ol li 兼容
ol 标签在 chrome 60 和 safari12 缩进不一样. 因为序号距离copy距离不一样,导致显示不一样.解决办法. list-style-position: inside;text-in ...
- UI5-技术篇-Navigation And Routing
主要记录下Router设置过程中出现的几个问题 1.View 首页设置 controlId 2.Manifest设置 2.1设置启动页 2.2设置默认配置 2.3设置Targets 首页设置 子页设置 ...
- SAP-参数(条件表)配置教程–GS01/GS02/GS03
转载:http://www.baidusap.com/abap/others/2849 在SAP开发中,某段代码运行可能需要满足某个条件,通常解决办法有两种:一种是在代码中写死限制条件,此种方式当限制 ...
- C++ 解决文件重复包含
// 如果zzz没有定义就定义zzz,然后执行下面的代码,定义了就不执行 #if !defined(zzz) #define zzz struct PPoint { int x; int y; }; ...
- S3C2440 OpenJtag
C:\Users\Administrator\Desktop>oflash.exe leds.bin +--------------------------------------------- ...
- flask打包安装文件
如果在一台新的电脑需要运行项目的时候,这时候就需要将项目项目所用到的模块都导出来 依赖文件生成pip freeze > requirements.txt 执行该命令会在项目根目录下生成一个 re ...
- C#-FileHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- TLS握手秘钥套件分析
1.为了弄清楚TLS1.3的内部结构,觉得有必要将TLS的整个结构从新整理一遍,方便后续在做握手协议的形式化分析的时候能够不遗漏每个加密和认证的的环节. TLS1.3不论文在协议内容上还是性能上都较之 ...
- KVM虚拟化——简介
KVM 基于内核的虚拟机KVM(Kernel-Based Virtual Machine)是2007年问世的开源虚拟化解决方案.KVM需要两个条件: ①硬件支持全虚拟化 ②操作系统为Linux KVM ...
- 190906mysql常用语法
1.创删数据库: create database mydb; 复杂一点可以设置字符集和校对规则:create database mydb character set gbk collate gbk_c ...