题面:

Tree

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

题意:

给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K

输入格式: 每次输入n,k(如果n0&&k0停止),接着n-1行,每行3个数描述边

这道题就是一道淀粉质点分治的模板题,具体不多讲,如果不会看看这个

接下来直接上代码

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=200001;
int read() {
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')c=='-'?f=-1,c=getchar():c=getchar();
while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x*f;
}
int n,k;
int dep[N];/*从当前节点i到枚举当前树的根节点父亲的距离*/
int f[N];/*当i为根节点时最大字数大小*/
int vis[N];/*i节点是否被当根使用过*/
int siz[N];/*以i节点为根时,其子树(包括本身)的节点个数*/
int root;/*根节点*/
struct node {
int next,to,v;
} a[N<<1];
int head[N],cnt,sum/*这棵当前递归的这棵树的大小*/;
void add(int x,int y,int c) {
a[++cnt].to=y;
a[cnt].next=head[x];
a[cnt].v=c;
head[x]=cnt;
}
void findroot(int k,int fa) {
f[k]=0,siz[k]=1;
for(int i=head[k]; i; i=a[i].next) {
int v=a[i].to;
if(vis[v]||v==fa)
continue;
findroot(v,k);
siz[k]+=siz[v];
f[k]=max(f[k],siz[v]);
}
f[k]=max(f[k],sum-siz[k]);
if(f[k]<f[root])
root=k;
}
int tot;
void finddep(int k,int fa,int l) {
dep[++tot]=l;
for(int i=head[k]; i; i=a[i].next) {
int v=a[i].to;
if(v==fa||vis[v])
continue;
finddep(v,k,l+a[i].v);
}
}
int K;
int calc(int k,int L) {
tot=0;
finddep(k,0,L);
sort(dep+1,dep+1+tot);
int l=1,r=tot,ans=0;
while(l<r){
if(dep[l]+dep[r]<=K)
l++,ans+=r-l+1;
else
r--;
}
return ans;
}
int js;
void devide(int k) {
vis[k]=1;
js+=calc(k,0);
for(int i=head[k]; i; i=a[i].next) {
int v=a[i].to;
if(vis[v])
continue;
js-=calc(v,a[i].v);
root=0,sum=siz[v];
findroot(v,0);
devide(root);
}
}
int main() {
int n=read(),x,y,z;
K=read();
while(n&&K){
memset(head,0,sizeof(head)),cnt=0;//head
memset(vis,0,sizeof(vis));//标记数组
for (int i=1; i<n; i++)
x=read(),y=read(),z=read(),add(x,y,z),add(y,x,z);
sum=f[0]=n,js=0;//总数
findroot(1,0);
devide(root);
printf("%d\n",js);
n=read(),K=read();
}
return 0;
}

注意初始化!!!注意初始化!!!注意初始化!!!

重要的事情说三遍

「POJ 1741」Tree的更多相关文章

  1. 「POJ 3666」Making the Grade 题解(两种做法)

    0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...

  2. 【POJ 1741】 Tree (树的点分治)

    Tree   Description Give a tree with n vertices,each edge has a length(positive integer less than 100 ...

  3. 【POJ 1741】Tree

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11570   Accepted: 3626 Description ...

  4. 【POJ 1741】 Tree

    [题目链接] http://poj.org/problem?id=1741 [算法] 点分治 要求距离不超过k的点对个数,不妨将路径分成两类 : 1. 经过根节点 2. 不经过根节点 考虑第1类路径, ...

  5. 「POJ Challenge」生日礼物

    Tag 堆,贪心,链表 Solution 把连续的符号相同的数缩成一个数,去掉两端的非正数,得到一个正负交替的序列,把该序列中所有数的绝对值扔进堆中,用所有正数的和减去一个最小值,这个最小值的求法与「 ...

  6. 「POJ 3268」Silver Cow Party

    更好的阅读体验 Portal Portal1: POJ Portal2: Luogu Description One cow from each of N farms \((1 \le N \le 1 ...

  7. Solution -「HDU 5498」Tree

    \(\mathcal{Description}\)   link.   给定一个 \(n\) 个结点 \(m\) 条边的无向图,\(q\) 次操作每次随机选出一条边.问 \(q\) 条边去重后构成生成 ...

  8. 「POJ 1135」Domino Effect(dfs)

    BUPT 2017 Summer Training (for 16) #3G 题意 摆好的多米诺牌中有n个关键牌,两个关键牌之间有边代表它们之间有一排多米诺牌.从1号关键牌开始推倒,问最后倒下的牌在哪 ...

  9. 「POJ - 1003」Hangover

    BUPT 2017 summer training (16) #2C 题意 n个卡片可以支撑住的长度是1/2+1/3+1/4+..+1/(n+1)个卡片长度.现在给出需要达到总长度,求最小的n. 题解 ...

随机推荐

  1. Sql server 2008 R2 正在关闭[0x80041033]

    1. 事件起因, 昨天还访问的好好的, 然后系统一更新, 今天访问的时候, 就报什么 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是 ...

  2. U-boot分析与移植(2)----U-boot stage1分析

    我们要生成u-boot.bin文件,它首先依赖于很多.o文件和.lds链接脚本文件 我们只要找到对应的.lds链接脚本文件就可以分析u-boot的启动流程. 1.打开u-boot-1.1.6\u-bo ...

  3. Py修行路 python基础 (二十五)线程与进程

    操作系统是用户和硬件沟通的桥梁 操作系统,位于底层硬件与应用软件之间的一层 工作方式:向下管理硬件,向上提供接口 操作系统进行切换操作: 把CPU的使用权切换给不同的进程. 1.出现IO操作 2.固定 ...

  4. js日期date对象

    js日期 日期对象的一些属性和方法 var date = new Date() date.toString() // "Tue Jan 29 2019 22:58:13 GMT+0800 ( ...

  5. java的mysql初探

    在实现如下demo之前,要安装mysql的驱动mysql-connector-java-gpl-5.1.26.msi DEMO: /* * 简单数据库测试 * @李志杰 * 2013-8-4 */ p ...

  6. 14-jQuery的ajax

    什么是ajax AJAX  =  异步的JavaScript 和 XML (Asynchronous Javascript and XML) 简言之,在不重载整个网页的情况下,AJAX通过后台加载数据 ...

  7. Oracle行转列LISTAGG函数

    工作过程中需要将查询的数据分组并显示在一行.以往的工作经验,在sql server中可以用for xml path来实现. 现提供Oracle数据库的行转列方式 oracle11g官方文档简介如下: ...

  8. linux 修改openfiles

    使用ulimit -a 可以查看当前系统的所有限制值,使用ulimit -n 可以查看当前的最大打开文件数. 新装的linux默认只有1024,当作负载较大的服务器时,很容易遇到error: too ...

  9. Linux&nbsp;ALSA声卡驱动之一:ALS…

    声明:本博内容均由http://blog.csdn.net/droidphone原创,转载请注明出处,谢谢! 一.  概述 ALSA是Advanced Linux Sound Architecture ...

  10. poj2104 主席树模板题

    题意 给出n个数字组成的数字序列,有m组询问.每次询问包含三个数字l,r,k.对于每个询问输出序列区间[l,r]中第k大的数字. 分析 这是主席树的模板题,套板子就可以 #include <cs ...