您的位置:首页 > 其它

POJ 1741 Tree (点分治)

2017-09-20 18:37 357 查看

Tree

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 24238 Accepted: 8053

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.

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.

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

  这是一道点分治(树分治)的经典题目。

  考虑一条路径

  如果它完全位于子树B,C,D中,那么这条路径可以在向下层递归时被覆盖到

  所以我们只需要考虑过根节点A的路径:

  设deep[x]表示x到root的距离,则我们要求deep[x]+deep[y]<=k&&(x,y不属于同一子树)的点对(x,y)的个数

  再考虑一下则转化成:[deep[x]+deep[y]<=k]的个数-[deep[x]+deep[y]<=k&&(x,y在同一子树)]的个数

  这样递归分治求解即可

  

  POJ传送门:http://poj.org/problem?id=1741

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf=1e6;
int n,k,root,a,b,vv,tot,cnt,sum,record,ans;
int head[10005],nxt[20005],point[20005],weight[20005],dis[10005],deep[10005],size[10005];
bool vis[10005];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void clear(){
tot=0;cnt=0;root=0;record=inf;ans=0;
memset(head,0,sizeof(head));
memset(vis,0,sizeof(vis));
}
void addedge(int x,int y,int value){
tot++;nxt[tot]=head[x];head[x]=tot;point[tot]=y;weight[tot]=value;
tot++;nxt[tot]=head[y];head[y]=tot;point[tot]=x;weight[tot]=value;
}
void findroot(int now,int father){
size[now]=1;
int maxx=0;
for(int tmp=head[now];tmp;tmp=nxt[tmp]){
int v=point[tmp];
if(!vis[v]&&v!=father){
findroot(v,now);
size[now]+=size[v];
maxx=max(maxx,size[v]);
}
}
maxx=max(maxx,sum-size[now]);
if(maxx<record)record=maxx,root=now;
}
void getdeep(int now,int father){
deep[++cnt]=dis[now];
for(int tmp=head[now];tmp;tmp=nxt[tmp]){
int v=point[tmp];
if(!vis[v]&&v!=father){
dis[v]=dis[now]+weight[tmp];
getdeep(v,now);
}
}
}
int query(int now,int value){
cnt=0;dis[now]=value;
getdeep(now,0);
sort(deep+1,deep+cnt+1);
int t=0,l=1,r=cnt;
while(l<r){
if(deep[l]+deep[r]<=k)t+=(r-l),l++;
else r--;
}
return t;
}
void slove(int now){
vis[now]=true;
ans+=query(now,0);
for(int tmp=head[now];tmp;tmp=nxt[tmp]){
int v=point[tmp];
if(!vis[v]){
ans-=query(v,weight[tmp]);
sum=size[v];
root=0;
record=inf;
findroot(v,root);
slove(root);
}
}
}
int main()
{
while(1){
n=read();k=read();
clear();
if(n==0)break;
for(int i=1;i<n;i++)
{
a=read(),b=read(),vv=read();
addedge(a,b,vv);
}
sum=n;
findroot(1,0);
slove(root);
printf("%d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 点分治