题面:

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. PL/SQL 训练08--触发器

    --什么是触发器呢?--一触即发,某个事件发生时,执行的程序块?--数据库触发器是一个当数据库发生某种事件时作为对这个事件的响应而执行的一个被命名的程序单元 --适合场景--对表的修改做验证--数据库 ...

  2. 为工具箱添加CSKin选项卡

    如何使用CSKin 项目的引用→右键→添加; 找到SCKin.dll; 添加引用 工具箱新建一个选项卡; 工具箱的空白处→右键→添加选项卡→SKinControl, 将刚才的CSKin.dll 直接拖 ...

  3. 第一章 为什么使用NoSQL

    1.1 关系型数据库的价值 1.1.1 获取持久化数据 1.1.2 并发 通过”事务“ 来控制,出错有“回滚”机制. 1.1.3 集成                共享数据库集成,多个应用程序将数据 ...

  4. VMware80端口映射

    目标是外网访问80端口,然后映射到虚拟机的80端口,80映射到80. 1.首先80端口是最常用的端口,要确认主机80端口是否被占用,如果被占用,停止或者修改占用80端口程序. 2.80端口默认防火墙是 ...

  5. Java之常用类及方法

    下面我们介绍Java类库所提供的常用类及类的常用方法 一.java.lang.String 1. String类常用的构造函数 public String(String original) 使用串对象 ...

  6. 【转】3D图形引擎(DX9): FX

    做图形程序已经很多年了,积累了一些经验.来这个论坛也有好几年了,遇到问题的时候,也会在这里发帖子问,也很感谢大家的回答与帮助,希望能多认识一些朋友,大家多交流.分享.做这款项目也有蛮多年了,终于上线了 ...

  7. java通过反射了解集合泛型的本质

  8. codeforce 462DIV2 C题

    题意 给出一个只含有1和2的序列,有n个元素,可以选择一段区间进行翻转操作,求再反转后的最大非递减子序列的长度 分析 太菜了只想出了N^2的做法.序列只有1和2,那么每个非递减子序列都会有一个分界点, ...

  9. angular.js简单入门。

    小弟刚接触angular js  就写了一个简单的入门.后续慢慢补... 首先看 html 页面. <html> <meta charset="UTF-8"> ...

  10. 使用Layered Window遇到的一些问题及解决方法

    1. 使用Layered Window需要设置 WS_EX_LAYERED 属性 2.  Layered Window不能作为Child Window 3. 它也不能包含子窗口,为什么呢,因为它收不到 ...