#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct edgee
{
int from, to, cost;
edgee(int f, int t, int c) :from(f), to(t), cost(c)
{}
edgee()
{}
};
edgee edge[200000];
int first[100000], nextt[200000],edgetot,n,k;
int value[1000000];
void addedge(int from, int to, int cost)
{
edge[edgetot] = edgee(from, to, cost);
nextt[edgetot] = first[from];
first[from] = edgetot++;
edge[edgetot] = edgee(to, from, cost);
nextt[edgetot] = first[to];
first[to] = edgetot++;
}
int size[100000],msize[100000];
bool iscenter[100000];
int center;
void getsize(int now,int f,int total)
{
size[now] = 1; msize[now] = -1;
for (int i = first[now]; i != -1; i = nextt[i])
{
int to = edge[i].to;
if (to == f||iscenter[to])continue;
getsize(to, now, total);
size[now] += size[to];
msize[now] = max(msize[now], size[to]);
}
msize[now] = max(msize[now], total - size[now]);
if (msize[now] < msize[center])center = now;
}
void getdpth(int num, int fa, int rvalue,int &tot)
{
value[tot++] = rvalue; size[num] = 1;
for (int i = first[num]; i != -1; i = nextt[i])
{
int to = edge[i].to;
if (to == fa || iscenter[to])continue;
getdpth(to, num, rvalue + edge[i].cost, tot);
size[num] += size[to];
}
}
int cal(int num,int rvalue)//这分两种一个是从center开始一个是从center的子节点开始,并且每次开始的时候size都是更新了的
{
int tot = 0;
getdpth(num, -1, rvalue, tot);
sort(value, value + tot);
int l, r; l = 0; r = tot-1;
int ans = 0;
while (l < r)
{
while (value[r] + value[l] <= k&&l<r)ans += r-l, l++;
r--;
}
return ans;
}
int work(int s,int total,int fa)
{
center = 0; msize[0] = 1000000000;
getsize(s, -1, total);//这个时候就找centerl了;
iscenter[center] = 1;
int ans = 0;
ans+=cal(center, 0);
for (int i = first[center]; i != -1; i = nextt[i])
{
int to = edge[i].to;
if (to == fa || iscenter[to])continue;
ans -= cal(to, edge[i].cost);//这的时候重新优化了size
ans += work(to, size[to],center);//这我先开始写错了。。。。注意fa是center而不是s
}
return ans;
}
int main()
{
scanf("%d", &n);
for (int i = 0; i <= n; i++)first[i] = -1;
for (int i = 1; i < n; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
//cout << "I:" << i << " " << "a:" << a << " " << "b:" << b << " " << "c:" << c << endl;
addedge(a, b, c);
}
scanf("%d", &k);
int ans = 0;
if(n)ans=work(1, n, -1);
printf("%d\n", ans);
}
树上点分治模板bzoj1468
最新推荐文章于 2021-09-18 17:33:27 发布