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
题目大意:求树上距离小于k的点对个数
裸题,树的点分治
const
maxn=;
var
n,k,cut,cuts,ans,num,tot:longint;
next,last,w:array[..maxn*]of longint;
first,size,a:array[..maxn]of longint;
flag:array[..maxn]of boolean; function max(x,y:longint):longint;
begin
if x>y then exit(x);
exit(y);
end; procedure dfs1(x:longint);
var
i,s:longint;
begin
size[x]:=;
i:=first[x];
flag[x]:=false;
s:=;
while i<> do
begin
if flag[last[i]] then
begin
dfs1(last[i]);
inc(size[x],size[last[i]]);
s:=max(s,size[last[i]]);
end;
i:=next[i];
end;
if max(num-size[x],s)<cuts then
begin
cut:=x;
cuts:=max(num-size[x],s);
end;
flag[x]:=true;
end; procedure swap(var x,y:longint);
var
t:longint;
begin
t:=x;x:=y;y:=t;
end; procedure sort(l,r:longint);
var
i,j,y:longint;
begin
i:=l;
j:=r;
y:=a[(l+r)>>];
repeat
while a[i]<y do
inc(i);
while a[j]>y do
dec(j);
if i<=j then
begin
swap(a[i],a[j]);
inc(i);
dec(j);
end;
until i>j;
if i<r then sort(i,r);
if j>l then sort(l,j);
end; procedure dfs2(x,d:longint);
var
i:longint;
begin
inc(a[]);
a[a[]]:=d;
flag[x]:=false;
i:=first[x];
while i<> do
begin
if flag[last[i]] then dfs2(last[i],d+w[i]);
i:=next[i];
end;
flag[x]:=true;
end; function calc(x,d:longint):longint;
var
l,r:longint;
begin
calc:=;
a[]:=;
dfs2(x,d);
sort(,a[]);
l:=a[];
for r:= to a[] do
begin
while (a[l]+a[r]>k) and (l>) do
dec(l);
if l<r then inc(calc,l)
else inc(calc,r-);
end;
end; procedure work;
var
i:longint;
begin
inc(ans,calc(cut,));
flag[cut]:=false;
i:=first[cut];
while i<> do
begin
if flag[last[i]] then dec(ans,calc(last[i],w[i]));
i:=next[i];
end;
i:=first[cut];
while i<> do
begin
if flag[last[i]] then
begin
dfs1(last[i]);
num:=last[i];
cuts:=num;
cut:=last[i];
dfs1(last[i]);
work;
end;
i:=next[i];
end;
end; procedure insert(x,y,z:longint);
begin
inc(tot);
last[tot]:=y;
next[tot]:=first[x];
first[x]:=tot;
w[tot]:=z;
end; procedure init;
var
i,x,y,z:longint;
begin
read(n,k);
if n= then halt;
tot:=;
for i:= to n do
begin
first[i]:=;
flag[i]:=true;
end;
ans:=;
for i:= to n- do
begin
read(x,y,z);
insert(x,y,z);
insert(y,x,z);
end;
cut:=;
cuts:=n;
dfs1();
work;
writeln(ans);
end; begin
while true do
init;
end.
POJ - 1741 Tree的更多相关文章
- POJ 1741 Tree 求树上路径小于k的点对个数)
POJ 174 ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
- POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量
POJ 1741. Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 34141 Accepted: 11420 ...
- POJ 1741 Tree(树的点分治,入门题)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21357 Accepted: 7006 Description ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- poj 1741 Tree(点分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15548 Accepted: 5054 Description ...
- ●POJ 1741 Tree
题链: http://poj.org/problem?id=1741题解: 树上点分治. 入门题,不多说了. 代码: #include<cstdio> #include<cstrin ...
- POJ 1741 Tree 树上点分治
题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...
- POJ 1741 Tree (树分治入门)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8554 Accepted: 2545 Description ...
- POJ 1741 Tree (点分治)
Tree Time Limit: 1000MS Memory ...
随机推荐
- CSS行高--line-height
遇到的问题:在css中,不理解line-height:1与line-height:1px的区别 发现的过程:最近在学做一个网站的过程中,设置两行文字之间的行高时需要用到line-height,发现了这 ...
- Bootstrap插件之Carousel轮播效果(2015年-05月-21日)
<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"& ...
- Web.config配置详解【转 】
一.认识Web.config文件 Web.config 文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web ...
- Jackson - Date Handling
Handling dates on Java platform is complex business. Jackson tries not to make it any harder than it ...
- HttpClient(4.3.5) - 简单示例
HttpClient 是一个客户端的 HTTP 传输库,而不是浏览器.HttpClient 的目的是传输和接收 HTTP 报文.HttpClient 不会尝试去处理报文内容,执行嵌入 HTML 内的 ...
- 在Ubuntu系统中解压rar和zip文件的方法
大家在以前的windows系统中会存有很多rar和zip格式的压缩文件,Ubuntu系统默认情况下对这些文件的支持不是很好,如果直接用"归档管理器"打开会提示错误,因此今天跟大家分 ...
- Ajax-Demo
index.jsp 1 <%@ page language="java" contentType="text/html; charset=UTF-8" p ...
- SQL_CURSOR_游标循环
) DECLARE My_Cursor CURSOR --定义游标 FOR (SELECT column1 FROM #temp1) --查出需要的集合放到游标中 OPEN My_Cursor; -- ...
- 数组和ArrayList 相互转换
Dim params(10) As DbParameter params(0) = DBHelper.CreateDbParameter("@RegName", Jz_RegInf ...
- Javascript Error: 11233 Content-Length mismatch
Today I got a error in fiddler: Failed to obtain request body. System.IO.InvalidDataException The re ...