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.
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.
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.
4 4 6
1 3
1 4
1 3
4
6 2 17
1 3
2 5
2 13
1 6
5 9
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 ;
}