点分治和树dp的关联还是有点大。。所以这锅还是窝的。。
点分治主要用于处理点对问题。。
点分治比较套路。。做几道题应该就没什么问题了?
基本思想就是选取一个点(为重心时复杂度最佳)为根,然后把问题分解成处理经过根的链和去掉根后的子树中的子问题。。
然后最关键的就是怎么求经过根的链了。。。
这里用的是dfs处理出深度然后将深度和小于k的记录下来,然后排序之后就可以直接用双指针计数了。。复杂度是O(n)
然而会发现同一子树上的点对实际上并不经过根。。所以要将同一子树的点对去掉。。
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━━━━━━━━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 10005
#define nm 100005
#define pi 3.1415926535897931
const ll inf=1000000007;
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,k,b[NM],d[NM],tot,cnt,size[NM],_x,_y,_t,ans,root,smin;
bool v[NM];
struct edge{int t,v;edge*next;}e[nm],*h[NM],*o=e;
void add(int x,int y,int v){o->t=y;o->v=v;o->next=h[x];h[x]=o++;}
void dfs1(int x,int f){size[x]=1;link(x)if(!v[j->t]&&j->t!=f)dfs1(j->t,x),size[x]+=size[j->t];}
void getroot(int x,int f){int s=tot-size[x];link(x)if(!v[j->t]&&j->t!=f)getroot(j->t,x),s=max(s,size[j->t]);if(s<smin)smin=s,root=x;}
void dfs2(int x,int f){
b[++cnt]=d[x];
link(x)if(j->t!=f&&!v[j->t])d[j->t]=d[x]+j->v,dfs2(j->t,x);
}
void div(int x){
//求重心
dfs1(x,0);
smin=inf;tot=size[x];
getroot(x,0);
v[root]++;
//计数
d[root]=0;cnt=0;
dfs2(root,0);
sort(b+1,b+cnt+1);
for(int l=1,r=cnt;l<r;ans+=r-l++)
while(b[l]+b[r]>k&&l<r)r--;
link(root)if(!v[j->t]){
//计数
d[j->t]=j->v;cnt=0;
dfs2(j->t,root);
sort(b+1,b+1+cnt);
for(int l=1,r=cnt;l<r;ans-=r-l++)
while(b[l]+b[r]>k&&l<r)r--;
//继续分治
div(j->t);
}
}
int main(){
while(~scanf("%d%d",&n,&k)&&n&&k){
ans=0;mem(e);mem(h);o=e;mem(v);tot=cnt=0;mem(size);
inc(i,1,n-1){_x=read();_y=read();_t=read();add(_x,_y,_t);add(_y,_x,_t);}
div(1);
printf("%d\n",ans);
}
return 0;
}
Tree
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 28286 | Accepted: 9428 |
Description
Give a tree with n vertices,each edge has a length(positive integer less than 1001).
Define dist(u,v)=The min distance between node u and v.
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k.
Write a program that will count how many pairs which are valid for a given tree.
Define dist(u,v)=The min distance between node u and v.
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k.
Write a program that will count how many pairs which are valid for a given tree.
Input
The input contains several test cases. The first line of each test case contains two integers n, k. (n<=10000) The following n-1 lines each contains three integers u,v,l, which means there is an edge between node u and v of length l.
The last test case is followed by two zeros.
The last test case is followed by two zeros.
Output
For each test case output the answer on a single line.
Sample Input
5 4 1 2 3 1 3 1 1 4 2 3 5 1 0 0
Sample Output
8
Source
[Submit] [Go Back] [Status] [Discuss]
![](https://i-blog.csdnimg.cn/blog_migrate/15fff056cacefaae7b94c2f7c71ca684.gif)