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 ...
随机推荐
- Spring(3.2.3) - Beans(2): 属性注入 & 构造注入
依赖注入是指程序运行过程中们如果需要另外的对象协作(访问它的属性或调用它的方法)时,无须在代码中创建被调用者,而是依赖于外部容器的注入. 属性注入(Setter Injection) 属性注入是指 I ...
- C#算法基础之希尔排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Android平台使用SQLite数据库存储数据
创建一个DataBaseHelper的类,这个类是继承SQLiteOpenHelper类的,这个类中包含创建数据库.打开数据库.创建表.添加数据和查询数据的方法.代码如下: package com.e ...
- android控件的属性
android控件的属性 本节描述android空间的位置,内容等相关属性及属性的含义 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 ( ...
- Cocos2d-x优化中多线程并发访问
多线程并发访问在Cocos2d-x引擎中用的不是很多,这主要是因为中整个结构设计没有采用多线程.源自于Objective-C的Ref对象,需要使用AutoreleasePool进行内存管理,Autor ...
- IOS UI 笔记整理回顾
注意手势会冒泡上抛,一个view没有实现的手势,如果父类view有实现,父视图就处理,如果不想让父视图处理,就把本视图添加到底层window上 setMasksToBounds:YES imageVi ...
- 使用C#通过调用minitab的COM库自动化生成报表
本文介绍通过C#调用minitab com组建自动化生成报表的方法. 首先需要在minitab中通过手动配置的方式生成报表来得到该报表的命令行,过程如下 选择菜单“编辑器”->“启用命令”启用命 ...
- OC7_单词个数
// // WordManger.h // OC7_单词个数 // // Created by zhangxueming on 15/6/17. // Copyright (c) 2015年 zhan ...
- ListView 复制到剪切板
private void 导出ToolStripMenuItem_Click(object sender, EventArgs e) { Clipboard.SetText(GetListView(l ...
- Qt模拟C#的File类对文件进行操作
其实只是QT菜鸟为了练习而搞出来的 文件头: #include <QFile> #include <QString> #include <iostream> #in ...