poj 1741 Tree

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

8

Source

 

 

题目大意:给定一棵有权的树,定义两点之间的距离为路径的边权和。给定一个安全值,求距离小于安全值的点对数目。

//================================================================================================

树的点分治。

考虑经过根的路径和没有经过根的路径。后者明显可以递归处理而且做法同前者。

那么经过根的合法点对的路径怎么求呢?

 

先O(N)深搜处理出每个点到根的距离,然后排序。这样可以做到O(N)的复杂度找出距离和小于安全值的对数。但是这里面包括从一个儿子里面伸上来的两条不合法的路径。额。。。再深搜每个儿子同理去掉不合法的部分。。

 

总感觉看上去各种暴力啊有木有。。。可是找到了树的重心了以后每次分治都能保证去掉一半的点,然后重复的次数就是log级别的,处理又是log的,所以跑出来的结果相当优秀啊、、

 

树只要一开始建一遍就好了。到后面分治的时候相当于切出若干个子树直接用就行了、

 

每次分治后都要调整根到当前子树的重心。

 

感觉这次敲的代码还是很可读的~~~算敲的比较清楚的一次了~~~

 

 

 

 

 

 

program pku_1741;
var line,g,next:array[1..20000] of longint;
    en,fa,stack,pi,a,q:array[1..10000] of longint;
    son:array[0..10000] of longint;
    t:array[1..10000] of boolean;
    tot,all,n,k:longint;
    ans:int64;
//============================================================================
procedure prepare;
begin tot:=0;
  fillchar(en,sizeof(en),0);
  fillchar(fa,sizeof(fa),0);
  fillchar(son,sizeof(son),0);
  fillchar(t,sizeof(t),true);
end;
//============================================================================
procedure ins(x,y,z:longint);
begin
  inc(tot); line[tot]:=y; g[tot]:=z;
  next[tot]:=en[x]; en[x]:=tot;
end;
//============================================================================
procedure qsort(l,r:longint);
var k,i,j,tt:longint;
begin
  k:=a[(l+r) shr 1]; i:=l; j:=r;
  repeat
    while a[i]
    while a[j]>k do dec(j);
    if i<=j then
    begin
      tt:=a[i]; a[i]:=a[j]; a[j]:=tt;
      inc(i); dec(j);
    end;
  until i>j;
  if l
  if i
end;
//============================================================================
procedure init;
var i,x,y,z:longint;
begin
  prepare;
  readln(n,k); ans:=0;
  if n=0 then halt;
  for i:=1 to n-1 do
  begin
    readln(x,y,z);
    ins(x,y,z); ins(y,x,z);
  end;
end;
//============================================================================
procedure build;
var u,v,i,top:longint;
begin
  stack[1]:=1; top:=1; pi[1]:=en[1];    //最近人工栈写得各种顺啊、、都不想写递归了。。。
  son[1]:=1; fa[1]:=0;
  while top>0 do
  begin
    u:=stack[top]; i:=pi[top];
    if i<>0 then
    begin
      pi[top]:=next[i]; v:=line[i];
      if fa[u]=v then continue;
      inc(top); stack[top]:=v; pi[top]:=en[v];
      son[v]:=1; fa[v]:=u;
    end else
    begin
      inc(son[fa[u]],son[u]); dec(top);
    end;
  end;
end;
//============================================================================
procedure adjust(var root:longint);    //找到重心。每次分治后都要再找。
var max,heavy,i,v:longint;
begin
  while true do
  begin i:=en[root];
    max:=0; heavy:=0;
    while i<>0 do
    begin v:=line[i];
      if t[v] and (v<>root) and (son[v]>max) then
      begin
        max:=son[v]; heavy:=v;
      end; i:=next[i];
    end;
    if max>all shr 1 then
    begin
      dec(son[root],son[heavy]);
      fa[root]:=heavy; fa[heavy]:=0;
      son[heavy]:=all; root:=heavy;
    end else break;
  end;
end;
//============================================================================
function find(root,ori:longint):longint;    //求点到根的距离。
var l,r,u,v,i,len:longint;
begin
  l:=1; r:=1; find:=0;
  q[1]:=root; a[1]:=ori;
  while l<=r do
  begin u:=q[l]; i:=en[u];
    while i<>0 do
    begin v:=line[i];
      if t[v] and (fa[u]<>v) then
      begin
        inc(r); q[r]:=v;
        a[r]:=a[l]+g[i];
      end; i:=next[i];
    end; inc(l);
  end; len:=r;
  if len>1 then qsort(1,len);
  l:=1; r:=len;
  while l
  begin
    while (lk) do dec(r);
    if l=r then break;
    inc(find,r-l); inc(l);
  end;
end;
//============================================================================
procedure tree(root:longint);    //分治,以root为根。
var i,v:longint;
begin
  if all=1 then exit;
  adjust(root);
  inc(ans,find(root,0));
  i:=en[root];
  while i<>0 do
  begin v:=line[i];
    if t[v] then
      dec(ans,find(v,g[i]));
    i:=next[i];
  end; t[root]:=false;
  i:=en[root];
  while i<>0 do
  begin v:=line[i];
    if t[v] then
    begin
      all:=son[v]; tree(v);
    end; i:=next[i];
  end;
end;
//============================================================================
begin
  while true do
  begin
    init; build;
    all:=n; tree(1);
    writeln(ans);
  end;
end.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值