BZOJ1468:Tree(点分治)

时间:2023-03-09 03:20:00
BZOJ1468:Tree(点分治)

Description

给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K

Input

N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是k

Output

一行,有多少对点之间的距离小于等于k

Sample Input

7
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10

Sample Output

5

Solution

点分治模板

Code

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N (40000+10)
using namespace std;
struct node
{
int to,next,len;
}edge[N*];
int n,k,sum,root,ans;
int head[N],num_edge;
int depth[N],d[N],size[N],maxn[N];
bool vis[N];
void add(int u,int v,int l)
{
edge[++num_edge].to=v;
edge[num_edge].len=l;
edge[num_edge].next=head[u];
head[u]=num_edge;
} void Get_root(int x,int fa)
{
size[x]=; maxn[x]=;
for (int i=head[x];i!=;i=edge[i].next)
if (edge[i].to!=fa && !vis[edge[i].to])
{
Get_root(edge[i].to,x);
size[x]+=size[edge[i].to];
maxn[x]=max(maxn[x],size[edge[i].to]);
}
maxn[x]=max(maxn[x],sum-size[x]);
if (maxn[x]<maxn[root]) root=x;
} void Get_depth(int x,int fa)
{
depth[++depth[]]=d[x];
for (int i=head[x];i!=;i=edge[i].next)
if (edge[i].to!=fa && !vis[edge[i].to])
{
d[edge[i].to]=d[x]+edge[i].len;
Get_depth(edge[i].to,x);
}
} int Calc(int x,int cost)
{
d[x]=cost; depth[]=;
Get_depth(x,);
sort(depth+,depth+depth[]+);
int l=,r=depth[],cnt=;
while (l<r)
if (depth[l]+depth[r]<=k)
cnt+=r-l,l++;
else
r--;
return cnt;
} void Solve(int x)
{
ans+=Calc(x,);
vis[x]=true;
for (int i=head[x];i!=;i=edge[i].next)
if (!vis[edge[i].to])
{
ans-=Calc(edge[i].to,edge[i].len);
sum=size[edge[i].to];
root=;
Get_root(edge[i].to,);
Solve(root);
}
} int main()
{
int u,v,l;
scanf("%d",&n);
sum=maxn[]=n;
for (int i=;i<=n-;++i)
{
scanf("%d%d%d",&u,&v,&l);
add(u,v,l); add(v,u,l);
}
scanf("%d",&k);
Get_root(,);
Solve(root);
printf("%d",ans);
}