E. Close Vertices

You've got a weighted tree, consisting of n vertices. Each edge has a non-negative weight. The length of the path between any two vertices of the tree is the number of edges in the path. The weight of the path is the total weight of all edges it contains.

Two vertices are close if there exists a path of length at most l between them and a path of weight at most w between them. Count the number of pairs of vertices v, u (v < u), such that vertices v and u are close.

Input

The first line contains three integers n, l and w (1 ≤ n ≤ 105, 1 ≤ l ≤ n, 0 ≤ w ≤ 109). The next n - 1 lines contain the descriptions of the tree edges. The i-th line contains two integers pi, wi (1 ≤ pi < (i + 1), 0 ≤ wi ≤ 104), that mean that the i-th edge connects vertex (i + 1) and pi and has weight wi.

Consider the tree vertices indexed from 1 to n in some way.

Output

Print a single integer — the number of close pairs.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Sample test(s)
Input
4 4 6
1 3
1 4
1 3
Output
4
Input
6 2 17
1 3
2 5
2 13
1 6
5 9
Output
9

【思路】

树分治。大体思路和这道题相似。

不同的是有两个需要满足的条件,只需要把dis排序,扫描的同时用BIT维护dep的区间信息并统计答案即可。

【代码】

 #include<map>
#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; typedef long long LL;
const int N = 1e5+;
const int INF = 1e9+;
struct Edge {
int v,w;
Edge(int v=,int w=) :v(v),w(w){}
};
int n,W,L; LL ans;
int root,size,vis[N],siz[N],f[N],dis[N],dep[N],l1,l2;
pair<int,int> list[N];
vector<int> rec;
vector<Edge> g[N];
//BIT
int C[N];
void add(int x,int v) {
while(x<=n) C[x]+=v,x+=x&(-x);
}
int query(int x) {
int ans=;
while(x>) ans+=C[x],x-=x&(-x);
return ans;
}
//fenzhi
void getroot(int u,int fa) {
siz[u]=; f[u]=;
for(int i=;i<g[u].size();i++) {
int v=g[u][i].v;
if(v!=fa && !vis[v]) {
getroot(v,u);
siz[u]+=siz[v];
f[u]=max(f[u],siz[v]);
}
}
f[u]=max(f[u],size-siz[u]);
if(f[u]<f[root]) root=u;
}
void getdis(int u,int fa) {
list[++l1]=make_pair(dis[u],dep[u]);
for(int i=;i<g[u].size();i++) {
int v=g[u][i].v;
if(v!=fa && !vis[v]) {
dep[v]=dep[u]+;
dis[v]=dis[u]+g[u][i].w;
getdis(v,u);
}
}
}
LL getans(int l,int r) {
sort(list+l,list+r+);
LL res=; int j=l;
for(int i=r;i>=l;i--) {
while(j<=r && list[i].first+list[j].first<=W) {
add(list[j].second,);
rec.push_back(list[j].second);
j++;
}
if(list[i].first*<=W && list[i].second*<=L) res--;
res+=(LL)query(L-list[i].second);
}
return res/;
}
void clear() {
for(int i=;i<rec.size();i++) add(rec[i],-);
rec.clear();
}
void solve(int u) {
vis[u]=; l1=l2=;
LL S1=,S2=;
for(int i=(int)g[u].size()-;i>=;i--) {
int v=g[u][i].v;
if(!vis[v]) {
l2=l1+;
dep[v]=; dis[v]=g[u][i].w;
getdis(v,u);
clear();
S1+=getans(l2,l1);
}
}
FOR(i,,l1) //AT:根为终点
if(list[i].first<=W && list[i].second<=L) S2++;
clear(); //AT:clear
S2+=getans(,l1);
ans=ans+S2-S1;
for(int i=(int)g[u].size()-;i>=;i--) {
int v=g[u][i].v;
if(!vis[v]) {
size=siz[v]; root=;
getroot(v,-); solve(root);
}
}
} void read(int& x) {
char c=getchar(); int f=; x=;
while(!isdigit(c)) {if(c=='-')f=-;c=getchar();}
while(isdigit(c)) x=x*+c-'',c=getchar();
x*=f;
}
int main() {
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
read(n),read(L),read(W);
int u,v,w;
FOR(i,,n) {
read(u),read(v),read(w);
g[u].push_back(Edge(v,w));
g[v].push_back(Edge(u,w));
}
root=,f[]=INF,size=n;
getroot(,-); solve(root);
cout<<ans;
return ;
}

cf293E Close Vertices(树分治+BIT)的更多相关文章

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

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

  2. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  3. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  4. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  5. poj 1744 tree 树分治

    Tree Time Limit: 1000MS   Memory Limit: 30000K       Description Give a tree with n vertices,each ed ...

  6. poj 1741 楼教主男人八题之中的一个:树分治

    http://poj.org/problem? id=1741 Description Give a tree with n vertices,each edge has a length(posit ...

  7. hdu-5977 Garden of Eden(树分治)

    题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  8. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  9. BZOJ 2152: 聪聪可可 树分治

    2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...

随机推荐

  1. 基于C语言EOF与getchar()的使用详解

    转自:http://www.jb51.net/article/36848.htm   大师级经典的著作,要字斟句酌的去读,去理解.以前在看K&R的The C Programming Langu ...

  2. x264_param_t结构

    typedef struct x264_param_t { unsigned int cpu; // CPU 标志位 int i_threads; // 并行编码多帧; 线程数,为0则自动多线程编码 ...

  3. C#实现JSON序列化与反序列化介绍

    方法一:引入System.Web.Script.Serialization命名空间使用 JavaScriptSerializer类实现简单的序列化 序列化类:Personnel public clas ...

  4. JDK重要包和Java学习方法论

    以下内容摘自:万能的林萧说:一篇文章教会你,如何做到简历中要求的“要有扎实的Java基础”    第一级别:精读源码 该级别包含的包如下: java.io java.lang java.util 第二 ...

  5. c#集合解析

    什么是集合(collection)? 提供了一种结构化组织任意对象的方式,从.NET 的角度看,所谓的集合可以定义为一种对象,这种对象实现一个或者多个System.Collections.IColle ...

  6. (转载)DBGridEh导出Excel等格式文件

    DBGridEh导出Excel等格式文件 uses DBGridEhImpExp; {--------------------------------------------------------- ...

  7. pyes-elasticsearch的python客户端使用笔记

    elasticsearch入门:  http://www.qwolf.com/?p=1387 一.重要的概念 http://834945712.iteye.com/blog/1915432 这篇文章很 ...

  8. ORA-01033 ORA-01109 ORA-01034 ORA-12514 ORA-24324 ORA-01041 ORA-01157 ORA-01110

    客户数据库挂掉了 在plsql客户端使用普通账号登录时提示如下错误 因为好久没弄数据库了,慌了一小下. 接下来搜索过往的知识,回忆.在cli下输入了以下命令 sqlplus system/system ...

  9. WebComponent

    WebComponent 前言  最近加入到新项目组负责前端技术预研和选型,一直偏向于以Polymer为代表的WebComponent技术线,于是查阅各类资料想说服老大向这方面靠,最后得到的结果是:& ...

  10. PADS故障解决

    1. 点击PADS后就会出现以下: "The directory pointed by the FileDir INI file entry cannot be found.Aborting ...